Skip to content

Commit

Permalink
double resolution with half characters fg/bg. fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
trashhalo committed Oct 25, 2020
1 parent 8fa33b7 commit 1f9ef8c
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ type model struct {
selected int
urls []string
image string
height uint
}

func (m model) Init() tea.Cmd {
return load(m.urls[m.selected])
return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.height = uint(msg.Height)
return m, load(m.urls[m.selected])
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
Expand All @@ -66,15 +70,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
url := m.urls[m.selected]
if msg.resp != nil {
defer msg.resp.Body.Close()
img, err := readerToImage(url, msg.resp.Body)
img, err := readerToImage(m.height, url, msg.resp.Body)
if err != nil {
return m, func() tea.Msg { return errMsg(err) }
}
m.image = img
return m, nil
}
defer msg.file.Close()
img, err := readerToImage(url, msg.file)
img, err := readerToImage(m.height, url, msg.file)
if err != nil {
return m, func() tea.Msg { return errMsg(err) }
}
Expand Down Expand Up @@ -117,24 +121,27 @@ func load(url string) tea.Cmd {
}
}

func readerToImage(url string, r io.Reader) (string, error) {
func readerToImage(height uint, url string, r io.Reader) (string, error) {
img, _, err := image.Decode(r)
if err != nil {
return "", err
}

img = resize.Resize(50, 0, img, resize.Lanczos3)
img = resize.Resize(0, height*2, img, resize.Lanczos3)
b := img.Bounds()
w := b.Max.X
h := b.Max.Y
p := termenv.ColorProfile()
str := strings.Builder{}
for y := 0; y < h; y++ {
for y := 0; y < h; y += 2 {
for x := 0; x < w; x++ {
c, _ := colorful.MakeColor(img.At(x, y))
color := p.Color(c.Hex())
str.WriteString(termenv.String(" ").
Background(color).
c1, _ := colorful.MakeColor(img.At(x, y))
color1 := p.Color(c1.Hex())
c2, _ := colorful.MakeColor(img.At(x, y+1))
color2 := p.Color(c2.Hex())
str.WriteString(termenv.String("▀").
Foreground(color1).
Background(color2).
String())
}
str.WriteString("\n")
Expand Down

0 comments on commit 1f9ef8c

Please sign in to comment.