Browse Source

switched to using the custom ansi methods

Benton Edmondson 2 years ago
parent
commit
816a344bdc
7 changed files with 19 additions and 48 deletions
  1. 2 5
      go.mod
  2. 0 7
      go.sum
  3. 2 2
      plaintext/plaintext.go
  4. 4 7
      plaintext/plaintext_test.go
  5. 1 1
      render/render_test.go
  6. 10 17
      style/style.go
  7. 0 9
      util/util.go

+ 2 - 5
go.mod

@@ -3,9 +3,6 @@ module mimicry
 go 1.19
 
 require (
-	github.com/mattn/go-runewidth v0.0.12 // indirect
-	github.com/muesli/reflow v0.3.0 // indirect
-	github.com/rivo/uniseg v0.2.0 // indirect
-	github.com/yuin/goldmark v1.5.4 // indirect
-	golang.org/x/net v0.5.0 // indirect
+	github.com/yuin/goldmark v1.5.4
+	golang.org/x/net v0.5.0
 )

+ 0 - 7
go.sum

@@ -1,10 +1,3 @@
-github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxmAOow=
-github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
-github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
-github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
-github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
-github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
-github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
 github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=
 github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
 golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=

+ 2 - 2
plaintext/plaintext.go

@@ -4,7 +4,7 @@ import (
 	"regexp"
 	"mimicry/style"
 	"strings"
-	"mimicry/util"
+	"mimicry/ansi"
 )
 
 func Render(text string, width int) (string, error) {
@@ -20,6 +20,6 @@ func Render(text string, width int) (string, error) {
 
 	url := regexp.MustCompile(`[A-Za-z][A-Za-z0-9+\-.]*://[A-Za-z0-9.?#/@:%_~!$&'()*+,;=\[\]\-]+`)
 	rendered := url.ReplaceAllStringFunc(text, style.Link)
-	wrapped := util.Wrap(rendered, width)
+	wrapped := ansi.Wrap(rendered, width)
 	return strings.TrimSpace(wrapped), nil
 }

+ 4 - 7
plaintext/plaintext_test.go

@@ -4,6 +4,7 @@ import (
 	"testing"
 	"mimicry/style"
 	"mimicry/util"
+	"mimicry/ansi"
 )
 
 func TestBasic(t *testing.T) {
@@ -15,13 +16,9 @@ Note the warning in <http://www.ics.uci.edu/pub/ietf/uri/historical.html#WARNING
 		panic(err)
 	}
 
-	expected := "Yes, Jim, I found it under\n\"" +
-		style.Link("http://www.w3.org/Addressing/") +
-		"\",\nbut you can probably pick it up from\n<" +
-		style.Link("ftp://foo.example.com/rfc/") +
-		">.\nNote the warning in\n<" +
-		style.Link("http://www.ics.uci.edu/pub/ietf/uri/historical.ht\nml#WARNING") +
-		">."
+	expected := ansi.Wrap("Yes, Jim, I found it under \"" + style.Link("http://www.w3.org/Addressing/") +
+	"\",\nbut you can probably pick it up from <" + style.Link("ftp://foo.example.com/rfc/") +
+	">.\nNote the warning in <" + style.Link("http://www.ics.uci.edu/pub/ietf/uri/historical.html#WARNING") + ">.", 50)
 
 	util.AssertEqual(expected, output, t)
 }

+ 1 - 1
render/render_test.go

@@ -13,7 +13,7 @@ func TestControlCharacterEscapes(t *testing.T) {
 		panic(err)
 	}
 
-	expected := "Yes, Jim, I\nfound it    under " +
+	expected := "Yes, Jim, I\nfound it\tunder " +
 		style.Link("http://www.w3.org/Addressing/")
 
 	util.AssertEqual(expected, output, t)

+ 10 - 17
style/style.go

@@ -3,6 +3,7 @@ package style
 import (
 	"fmt"
 	"strings"
+	"mimicry/ansi"
 )
 
 // TODO: at some point I need to sanitize preexisting escape codes
@@ -10,21 +11,13 @@ import (
 // escape character
 
 func background(text string, r uint8, g uint8, b uint8) string {
-	setter := fmt.Sprintf("\x1b[48;2;%d;%d;%dm", r, g, b)
-	resetter := "\x1b[49m"
-	text = strings.ReplaceAll(text, resetter, setter)
-	return fmt.Sprintf("%s%s%s", setter, text, resetter)
-}
-
-func extendBackground(text string) string {
-	return strings.ReplaceAll(text, "\n", "\x1b[K\n")
+	prefix := fmt.Sprintf("48;2;%d;%d;%d", r, g, b)
+	return ansi.Apply(text, prefix)
 }
 
 func foreground(text string, r uint8, g uint8, b uint8) string {
-	setter := fmt.Sprintf("\x1b[38;2;%d;%d;%dm", r, g, b)
-	resetter := "\x1b[39m"
-	newText := strings.ReplaceAll(text, resetter, setter)
-	return fmt.Sprintf("%s%s%s", setter, newText, resetter)
+	prefix := fmt.Sprintf("38;2;%d;%d;%d", r, g, b)
+	return ansi.Apply(text, prefix)
 }
 
 func display(text string, prependCode int, appendCode int) string {
@@ -35,19 +28,19 @@ func display(text string, prependCode int, appendCode int) string {
 // 22 removes bold and faint, faint is never used
 // so it does the job
 func Bold(text string) string {
-	return display(text, 1, 22)
+	return ansi.Apply(text, "1")
 }
 
 func Strikethrough(text string) string {
-	return display(text, 9, 29)
+	return ansi.Apply(text, "9")
 }
 
 func Underline(text string) string {
-	return display(text, 4, 24)
+	return ansi.Apply(text, "4")
 }
 
 func Italic(text string) string {
-	return display(text, 3, 23)
+	return ansi.Apply(text, "3")
 }
 
 func Code(text string) string {
@@ -55,7 +48,7 @@ func Code(text string) string {
 }
 
 func CodeBlock(text string) string {
-	return extendBackground(Code(text))
+	return Code(text)
 }
 
 func QuoteBlock(text string) string {

+ 0 - 9
util/util.go

@@ -2,8 +2,6 @@ package util
 
 import (
 	"testing"
-	"github.com/muesli/reflow/wordwrap"
-	"github.com/muesli/reflow/wrap"
 )
 
 func AssertEqual(expected string, output string, t *testing.T) {
@@ -11,10 +9,3 @@ func AssertEqual(expected string, output string, t *testing.T) {
 		t.Fatalf("Expected `%s` not `%s`\n", expected, output)
 	}
 }
-
-func Wrap(text string, width int) string {
-	if width < 1 {
-		width = 1
-	}
-	return wrap.String(wordwrap.String(text, width), width)
-}