Skip to content

Commit

Permalink
image: add sixel image output to lui -a
Browse files Browse the repository at this point in the history
This implements a stdimg for lui in console mode.
Images are sixel encoded and written to stdout, if they are not
assigned to a variable.

Example: A blue rectangle
	`img ⌶20 20⍴0xFF
  • Loading branch information
ktye committed Feb 17, 2019
1 parent 169d6ee commit 815e06e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
9 changes: 9 additions & 0 deletions apl/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ func (a *Apl) Eval(p Program) (err error) {
for e := range v[0] {
fmt.Fprintln(a.stdout, e.String(a))
}
case Image:
if a.stdimg != nil {
err = a.stdimg.WriteImage(v)
if err != nil {
return err
}
} else {
fmt.Println(a.stdout, v.String(a))
}
default:
fmt.Fprintln(a.stdout, val.String(a))
}
Expand Down
4 changes: 1 addition & 3 deletions apl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,5 @@ func colorValue(c color.Color) Int {
}
func toColor(i int) color.Color {
u := uint32(i)
c := color.RGBA{uint8(u & 0xFF0000 >> 16), uint8(u & 0xFF00 >> 8), uint8(u & 0xFF), ^uint8(u >> 24)}
fmt.Println(c)
return c
return color.RGBA{uint8(u & 0xFF0000 >> 16), uint8(u & 0xFF00 >> 8), uint8(u & 0xFF), ^uint8(u >> 24)}
}
20 changes: 20 additions & 0 deletions aplextra/u/sixel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package u

import (
"io"

"github.com/ktye/iv/apl"
sixel "github.com/mattn/go-sixel"
)

// Sxl implements an apl.ImageWriter to serve as a stdimg device in a sixel capable terminal.
type Sxl struct {
io.Writer
}

func (dev Sxl) WriteImage(m apl.Image) error {
// TODO animations

e := sixel.NewEncoder(dev.Writer)
return e.Encode(m.Im[0])
}
5 changes: 4 additions & 1 deletion cmd/lui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ func main() {
case "-a":
a := newApl()
a.SetOutput(os.Stdout)
a.SetImage(u.Sxl{os.Stdout})
err = cmd.Apl(a, os.Stdin, os.Args[2:])
case "-i":
err = cmd.Iv(newApl(), strings.Join(os.Args[2:], " "), os.Stdout)
a := newApl()
a.SetImage(u.Sxl{os.Stdout})
err = cmd.Iv(a, strings.Join(os.Args[2:], " "), os.Stdout)
case "-z":
fmt.Println("TODO: attach zip file")
default:
Expand Down

0 comments on commit 815e06e

Please sign in to comment.