From 9af834bc5dda3cdd37dd034b115ee3fa53d0bcc9 Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Mon, 20 Nov 2023 08:11:16 +0700 Subject: [PATCH] Changing rasterm out to different fork --- go.mod | 8 +++++--- go.sum | 17 +++++++++------- main.go | 60 ++++++++++----------------------------------------------- 3 files changed, 25 insertions(+), 60 deletions(-) diff --git a/go.mod b/go.mod index b6f001a..0c6751d 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/kenshaw/fv go 1.21.3 require ( - github.com/BourgeoisBear/rasterm v1.0.3 + github.com/kenshaw/rasterm v0.1.1 github.com/spf13/cobra v1.8.0 github.com/spf13/pflag v1.0.5 github.com/tdewolff/canvas v0.0.0-20231102134958-6de43c767dbf @@ -20,12 +20,14 @@ require ( github.com/go-text/typesetting v0.0.0-20231013144250-6cc35dbfae7d // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/mattn/go-sixel v0.0.5 // indirect + github.com/soniakeys/quant v1.0.0 // indirect github.com/tdewolff/minify/v2 v2.20.5 // indirect github.com/tdewolff/parse/v2 v2.7.3 // indirect golang.org/x/image v0.13.0 // indirect golang.org/x/net v0.17.0 // indirect - golang.org/x/sys v0.13.0 // indirect - golang.org/x/term v0.13.0 // indirect + golang.org/x/sys v0.14.0 // indirect + golang.org/x/term v0.14.0 // indirect golang.org/x/text v0.13.0 // indirect star-tex.org/x/tex v0.4.0 // indirect ) diff --git a/go.sum b/go.sum index f13e0de..77d4224 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ git.sr.ht/~sbinet/gg v0.5.0 h1:6V43j30HM623V329xA9Ntq+WJrMjDxRjuAB1LFWF5m8= git.sr.ht/~sbinet/gg v0.5.0/go.mod h1:G2C0eRESqlKhS7ErsNey6HHrqU1PwsnCQlekFi9Q2Oo= -github.com/BourgeoisBear/rasterm v1.0.3 h1:k3/mcjyo3ukAkMA2PDdtrBGv16NvJ26ABd9p9hIzbp8= -github.com/BourgeoisBear/rasterm v1.0.3/go.mod h1:wpcJbTo13ssx5lk+7Ovb7MVR6qvgHFW5lrjNXlxBInY= github.com/ByteArena/poly2tri-go v0.0.0-20170716161910-d102ad91854f h1:l7moT9o/v/9acCWA64Yz/HDLqjcRTvc0noQACi4MsJw= github.com/ByteArena/poly2tri-go v0.0.0-20170716161910-d102ad91854f/go.mod h1:vIOkSdX3NDCPwgu8FIuTat2zDF0FPXXQ0RYFRy+oQic= github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b h1:slYM766cy2nI3BwyRiyQj/Ud48djTMtMebDqepE95rw= @@ -34,11 +32,17 @@ github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF0 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/kenshaw/rasterm v0.1.1 h1:l3RP/7nPfUT/ayFhYny/aiHbsQxKJYjcenOM5DVvyyI= +github.com/kenshaw/rasterm v0.1.1/go.mod h1:igPZRm3TkLpiJ1TIP254CfvVA/i8B6UURhhuytKVNcM= github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= +github.com/mattn/go-sixel v0.0.5 h1:55w2FR5ncuhKhXrM5ly1eiqMQfZsnAHIpYNGZX03Cv8= +github.com/mattn/go-sixel v0.0.5/go.mod h1:h2Sss+DiUEHy0pUqcIB6PFXo5Cy8sTQEFr3a9/5ZLNw= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/soniakeys/quant v1.0.0 h1:N1um9ktjbkZVcywBVAAYpZYSHxEfJGzshHCxx/DaI0Y= +github.com/soniakeys/quant v1.0.0/go.mod h1:HI1k023QuVbD4H8i9YdfZP2munIHU4QpjsImz6Y6zds= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= @@ -63,12 +67,11 @@ golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= -golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= -golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/term v0.14.0 h1:LGK9IlZ8T9jvdy6cTdfKUCltatMFOehAQo9SRC46UQ8= +golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= diff --git a/main.go b/main.go index 4913c7a..d5994ef 100644 --- a/main.go +++ b/main.go @@ -8,10 +8,7 @@ import ( _ "embed" "errors" "fmt" - "image" "image/color" - "image/color/palette" - "image/draw" "io" "os" "path/filepath" @@ -24,7 +21,7 @@ import ( "text/template" "unicode" - "github.com/BourgeoisBear/rasterm" + "github.com/kenshaw/rasterm" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/tdewolff/canvas" @@ -89,14 +86,7 @@ func run(ctx context.Context, appName, appVersion string, cliargs []string) erro case match: f = doMatch } - var r func(io.Writer, image.Image) error - var typ string - if all || !(match || list) { - r, typ, _ = renderer() - } return f(os.Stdout, sysfonts, &Params{ - Render: r, - Type: typ, FG: fgColor, BG: bgColor, Size: size, @@ -128,8 +118,6 @@ func run(ctx context.Context, appName, appVersion string, cliargs []string) erro } type Params struct { - Render func(io.Writer, image.Image) error - Type string FG color.Color BG color.Color Size int @@ -170,8 +158,8 @@ func (v *Params) Template() (*template.Template, error) { // do renders the specified font queries to w. func do(w io.Writer, sysfonts *fontpkg.SystemFonts, v *Params) error { - if v.Render == nil || v.Type == "" { - return errors.New("terminal does not support graphics") + if !rasterm.Available() { + return rasterm.ErrTermGraphicsNotAvailable } var fonts []*Font // collect fonts @@ -187,8 +175,8 @@ func do(w io.Writer, sysfonts *fontpkg.SystemFonts, v *Params) error { // doAll renders all system fonts to w. func doAll(w io.Writer, sysfonts *fontpkg.SystemFonts, v *Params) error { - if v.Render == nil || v.Type == "" { - return errors.New("terminal does not support graphics") + if !rasterm.Available() { + return rasterm.ErrTermGraphicsNotAvailable } families := maps.Keys(sysfonts.Fonts) slices.Sort(families) @@ -235,15 +223,12 @@ func render(w io.Writer, fonts []*Font, v *Params) error { return err } for i := 0; i < len(fonts); i++ { - err := fonts[i].Render(w, tpl, v) - if err != nil { + if err := fonts[i].Render(w, tpl, v); err != nil { fmt.Fprintf(os.Stdout, "%s -- error: %v\n", fonts[i], err) } - nl := []byte{'\n'} - if i != len(v.Args)-1 && err == nil { - nl = append(nl, '\n') + if i != len(fonts)-1 { + fmt.Fprintln(w) } - w.Write(nl) } return nil } @@ -255,31 +240,6 @@ type TemplateData struct { SampleText string } -func renderer() (func(io.Writer, image.Image) error, string, bool) { - var s rasterm.Settings - switch { - case rasterm.IsTmuxScreen(): - return nil, "", false - case rasterm.IsTermKitty(): - return s.KittyWriteImage, "kitty", true - case rasterm.IsTermItermWez(): - return s.ItermWriteImage, "iterm", true - default: - if ok, _ := rasterm.IsSixelCapable(); ok { - return func(w io.Writer, src image.Image) error { - if _, ok := src.(*image.Paletted); !ok { - b := src.Bounds() - img := image.NewPaletted(b, palette.Plan9) - draw.FloydSteinberg.Draw(img, b, src, image.Point{}) - src = img - } - return s.SixelWriteImage(w, src.(*image.Paletted)) - }, "sixel", true - } - } - return nil, "", false -} - type Font struct { Path string Family string @@ -438,8 +398,8 @@ func (font *Font) Render(w io.Writer, tpl *template.Template, v *Params) error { ctx.Close() - // rasterize canvas - return v.Render(w, rasterizer.Draw( + // encode + return rasterm.Encode(w, rasterizer.Draw( c, canvas.DPI(float64(v.DPI)), canvas.DefaultColorSpace),