2017-03-04 20:29:42 +00:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
2019-01-24 16:42:13 +00:00
|
|
|
"log"
|
2017-03-04 20:29:42 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2019-03-07 10:50:20 +00:00
|
|
|
ui "github.com/gizak/termui/v3"
|
|
|
|
"github.com/gizak/termui/v3/widgets"
|
2017-03-04 20:29:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var images []image.Image
|
|
|
|
for _, arg := range os.Args[1:] {
|
|
|
|
resp, err := http.Get(arg)
|
|
|
|
if err != nil {
|
2019-01-24 17:39:50 +00:00
|
|
|
log.Fatalf("failed to fetch image: %v", err)
|
2017-03-04 20:29:42 +00:00
|
|
|
}
|
2019-01-24 17:39:50 +00:00
|
|
|
image, _, err := image.Decode(resp.Body)
|
2017-03-04 20:29:42 +00:00
|
|
|
if err != nil {
|
2019-01-24 17:39:50 +00:00
|
|
|
log.Fatalf("failed to decode fetched image: %v", err)
|
2017-03-04 20:29:42 +00:00
|
|
|
}
|
2019-01-24 17:39:50 +00:00
|
|
|
images = append(images, image)
|
2017-03-04 20:29:42 +00:00
|
|
|
}
|
|
|
|
if len(images) == 0 {
|
2019-01-24 17:39:50 +00:00
|
|
|
image, _, err := image.Decode(base64.NewDecoder(base64.StdEncoding, strings.NewReader(GOPHER_IMAGE)))
|
2017-03-04 20:29:42 +00:00
|
|
|
if err != nil {
|
2019-01-24 17:39:50 +00:00
|
|
|
log.Fatalf("failed to decode gopher image: %v", err)
|
2017-03-04 20:29:42 +00:00
|
|
|
}
|
2019-01-24 17:39:50 +00:00
|
|
|
images = append(images, image)
|
2017-03-04 20:29:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 16:42:13 +00:00
|
|
|
if err := ui.Init(); err != nil {
|
|
|
|
log.Fatalf("failed to initialize termui: %v", err)
|
2017-03-04 20:29:42 +00:00
|
|
|
}
|
2019-01-24 16:42:13 +00:00
|
|
|
defer ui.Close()
|
2017-03-04 20:29:42 +00:00
|
|
|
|
2019-01-24 16:42:13 +00:00
|
|
|
img := widgets.NewImage(nil)
|
2019-01-24 17:39:50 +00:00
|
|
|
img.SetRect(0, 0, 100, 50)
|
2017-03-04 20:29:42 +00:00
|
|
|
index := 0
|
|
|
|
render := func() {
|
|
|
|
img.Image = images[index]
|
|
|
|
if !img.Monochrome {
|
2019-01-24 16:42:13 +00:00
|
|
|
img.Title = fmt.Sprintf("Color %d/%d", index+1, len(images))
|
2017-03-04 20:29:42 +00:00
|
|
|
} else if !img.MonochromeInvert {
|
2019-01-24 16:42:13 +00:00
|
|
|
img.Title = fmt.Sprintf("Monochrome(%d) %d/%d", img.MonochromeThreshold, index+1, len(images))
|
2017-03-04 20:29:42 +00:00
|
|
|
} else {
|
2019-01-24 16:42:13 +00:00
|
|
|
img.Title = fmt.Sprintf("InverseMonochrome(%d) %d/%d", img.MonochromeThreshold, index+1, len(images))
|
2017-03-04 20:29:42 +00:00
|
|
|
}
|
2019-01-24 16:42:13 +00:00
|
|
|
ui.Render(img)
|
2017-03-04 20:29:42 +00:00
|
|
|
}
|
|
|
|
render()
|
|
|
|
|
2019-01-24 16:42:13 +00:00
|
|
|
uiEvents := ui.PollEvents()
|
|
|
|
for {
|
|
|
|
e := <-uiEvents
|
|
|
|
switch e.ID {
|
|
|
|
case "q", "<C-c>":
|
|
|
|
return
|
2019-01-24 17:39:50 +00:00
|
|
|
case "<Left>", "h":
|
2019-01-24 16:42:13 +00:00
|
|
|
index = (index + len(images) - 1) % len(images)
|
2019-01-24 17:39:50 +00:00
|
|
|
case "<Right>", "l":
|
2017-03-04 20:29:42 +00:00
|
|
|
index = (index + 1) % len(images)
|
2019-01-24 17:39:50 +00:00
|
|
|
case "<Up>", "k":
|
2019-01-24 16:42:13 +00:00
|
|
|
img.MonochromeThreshold++
|
2019-01-24 17:39:50 +00:00
|
|
|
case "<Down>", "j":
|
2019-01-24 16:42:13 +00:00
|
|
|
img.MonochromeThreshold--
|
|
|
|
case "<Enter>":
|
2017-03-04 20:29:42 +00:00
|
|
|
img.Monochrome = !img.Monochrome
|
2019-01-24 17:39:50 +00:00
|
|
|
case "<Tab>":
|
2017-03-04 20:29:42 +00:00
|
|
|
img.MonochromeInvert = !img.MonochromeInvert
|
|
|
|
}
|
2019-01-24 17:39:50 +00:00
|
|
|
render()
|
2019-01-24 16:42:13 +00:00
|
|
|
}
|
2017-03-04 20:29:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 17:39:50 +00:00
|
|
|
const GOPHER_IMAGE = `iVBORw0KGgoAAAANSUhEUgAAAEsAAAA8CAAAAAALAhhPAAAFfUlEQVRYw62XeWwUVRzHf2+OPbo9d7tsWyiyaZti6eWGAhISoIGKECEKCAiJJkYTiUgTMYSIosYYBBIUIxoSPIINEBDi2VhwkQrVsj1ESgu9doHWdrul7ba73WNm3vOPtsseM9MdwvvrzTs+8/t95ze/33sI5BqiabU6m9En8oNjduLnAEDLUsQXFF8tQ5oxK3vmnNmDSMtrncks9Hhtt/qeWZapHb1ha3UqYSWVl2ZmpWgaXMXGohQAvmeop3bjTRtv6SgaK/Pb9/bFzUrYslbFAmHPp+3WhAYdr+7GN/YnpN46Opv55VDsJkoEpMrY/vO2BIYQ6LLvm0ThY3MzDzzeSJeeWNyTkgnIE5ePKsvKlcg/0T9QMzXalwXMlj54z4c0rh/mzEfr+FgWEz2w6uk8dkzFAgcARAgNp1ZYef8bH2AgvuStbc2/i6CiWGj98y2tw2l4FAXKkQBIf+exyRnte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