From 17b09fb3279fe316bb8e47dd946776c8448d2109 Mon Sep 17 00:00:00 2001 From: peperoncino <2wua4nlyi@gmail.com> Date: Sat, 1 Jan 2022 22:27:32 +0900 Subject: [PATCH] iterm --- go.mod | 5 ++++- go.sum | 36 +++++++++++++++++++++++++++++++++ main.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 96 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 4e26721..f44ddb4 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,9 @@ go 1.13 require ( github.com/mattn/go-sixel v0.0.1 + github.com/mattn/longcat v0.0.1 github.com/rakyll/statik v0.1.7 - github.com/soniakeys/quant v1.0.0 // indirect + golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect + golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect + golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 ) diff --git a/go.sum b/go.sum index 8f21efe..0c4ff33 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,42 @@ +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= +github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= +github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e/go.mod h1:kLgvv7o6UM+0QSf0QjAse3wReFDsb9qbZJdfexWlrQw= +github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-sixel v0.0.1 h1:rhJSpux2xjsmXdXqY694uiEC0Rwxt6jYoq7Bahqo2xs= github.com/mattn/go-sixel v0.0.1/go.mod h1:zlzhYSuMbLdRdrxfutExxGpC+Pf2uUTJ6GpVQ4LB5dc= +github.com/mattn/longcat v0.0.1 h1:ExyQ1ZAr/DxxZLD7QEyHqfP34P/bs5Jv7ue2C5IkDXk= +github.com/mattn/longcat v0.0.1/go.mod h1:sUG/u32l+8LdKQmUGi49ju0kN5Eq2ozAwAgazcnjzbM= +github.com/mattn/pixterm v1.2.5-0.20191008162044-72ff621ffbff/go.mod h1:vTyMUJdLhdKEPZWDo6JxMHiCnqzi6BgELKw7qnCLrqo= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ= github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc= github.com/soniakeys/quant v1.0.0 h1:N1um9ktjbkZVcywBVAAYpZYSHxEfJGzshHCxx/DaI0Y= github.com/soniakeys/quant v1.0.0/go.mod h1:HI1k023QuVbD4H8i9YdfZP2munIHU4QpjsImz6Y6zds= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/tomnomnom/xtermcolor v0.0.0-20160428124646-b78803f00a7e/go.mod h1:fSIW/szJHsRts/4U8wlMPhs+YqJC+7NYR+Qqb1uJVpA= +github.com/zyxar/image2ascii v0.0.0-20180912034614-460a04e371ae/go.mod h1:Md4Hcw0pmYWDCo1o/fHeOC2Gdhc6oDRwLim8V+SMvI0= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/image v0.0.0-20200119044424-58c23975cae1/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ= +golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +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/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go index 41e14f1..f83b561 100644 --- a/main.go +++ b/main.go @@ -14,10 +14,14 @@ import ( "log" "net/http" "os" + "strings" + "time" + "golang.org/x/term" //_ "./statik" _ "github.com/Doarakko/bigburger/statik" "github.com/mattn/go-sixel" + "github.com/mattn/longcat/iterm" "github.com/rakyll/statik/fs" ) @@ -58,8 +62,15 @@ func init() { log.Fatal(err) } - top.Img, _ = loadImage(fs, "/top.png") - bottom.Img, _ = loadImage(fs, "/bottom.png") + top.Img, err = loadImage(fs, "/top.png") + if err != nil { + log.Fatal(err) + } + + bottom.Img, err = loadImage(fs, "/bottom.png") + if err != nil { + log.Fatal(err) + } toppings[0].Name = "putty" toppings[0].Count = 1 @@ -108,10 +119,10 @@ func main() { flag.Parse() // Count number of unique toppings - nunique := 0 + uniqueToppingCount := 0 for i := 0; i < len(toppings); i++ { if toppings[i].Count > 0 { - nunique++ + uniqueToppingCount++ } } @@ -132,7 +143,7 @@ func main() { // toppings count := 0 y := top.Img.Bounds().Dy() - for i := 0; count != nunique; i++ { + for i := 0; count != uniqueToppingCount; i++ { for j := 0; j < len(toppings); j++ { if toppings[j].Count == 0 || toppings[j].Count-i < 0 { continue @@ -164,11 +175,49 @@ func main() { } var buf bytes.Buffer - var enc interface{ Encode(image.Image) error } - enc = sixel.NewEncoder(&buf) + var enc interface { + Encode(image.Image) error + } + if checkIterm() { + enc = iterm.NewEncoder(&buf) + } else { + enc = sixel.NewEncoder(&buf) + } + if err := enc.Encode(output); err != nil { log.Fatal(err) } os.Stdout.Write(buf.Bytes()) os.Stdout.Sync() } + +// https://github.com/mattn/longcat +func checkIterm() bool { + if strings.HasPrefix(os.Getenv("TERM_PROGRAM"), "iTerm") { + return true + } + return getDA2() == "\x1b[>0;95;0c" // iTerm2 version 3 +} + +// https://github.com/mattn/longcat +func getDA2() string { + s, err := term.MakeRaw(1) + if err != nil { + return "" + } + defer term.Restore(1, s) + _, err = os.Stdout.Write([]byte("\x1b[>c")) // DA2 host request + if err != nil { + return "" + } + defer os.Stdout.SetReadDeadline(time.Time{}) + + time.Sleep(10 * time.Millisecond) + + var b [100]byte + n, err := os.Stdout.Read(b[:]) + if err != nil { + return "" + } + return string(b[:n]) +}