Skip to content

Commit 815e06e

Browse files
committed
image: add sixel image output to lui -a
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
1 parent 169d6ee commit 815e06e

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

apl/eval.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ func (a *Apl) Eval(p Program) (err error) {
4343
for e := range v[0] {
4444
fmt.Fprintln(a.stdout, e.String(a))
4545
}
46+
case Image:
47+
if a.stdimg != nil {
48+
err = a.stdimg.WriteImage(v)
49+
if err != nil {
50+
return err
51+
}
52+
} else {
53+
fmt.Println(a.stdout, v.String(a))
54+
}
4655
default:
4756
fmt.Fprintln(a.stdout, val.String(a))
4857
}

apl/image.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,5 @@ func colorValue(c color.Color) Int {
108108
}
109109
func toColor(i int) color.Color {
110110
u := uint32(i)
111-
c := color.RGBA{uint8(u & 0xFF0000 >> 16), uint8(u & 0xFF00 >> 8), uint8(u & 0xFF), ^uint8(u >> 24)}
112-
fmt.Println(c)
113-
return c
111+
return color.RGBA{uint8(u & 0xFF0000 >> 16), uint8(u & 0xFF00 >> 8), uint8(u & 0xFF), ^uint8(u >> 24)}
114112
}

aplextra/u/sixel.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package u
2+
3+
import (
4+
"io"
5+
6+
"github.com/ktye/iv/apl"
7+
sixel "github.com/mattn/go-sixel"
8+
)
9+
10+
// Sxl implements an apl.ImageWriter to serve as a stdimg device in a sixel capable terminal.
11+
type Sxl struct {
12+
io.Writer
13+
}
14+
15+
func (dev Sxl) WriteImage(m apl.Image) error {
16+
// TODO animations
17+
18+
e := sixel.NewEncoder(dev.Writer)
19+
return e.Encode(m.Im[0])
20+
}

cmd/lui/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ func main() {
4242
case "-a":
4343
a := newApl()
4444
a.SetOutput(os.Stdout)
45+
a.SetImage(u.Sxl{os.Stdout})
4546
err = cmd.Apl(a, os.Stdin, os.Args[2:])
4647
case "-i":
47-
err = cmd.Iv(newApl(), strings.Join(os.Args[2:], " "), os.Stdout)
48+
a := newApl()
49+
a.SetImage(u.Sxl{os.Stdout})
50+
err = cmd.Iv(a, strings.Join(os.Args[2:], " "), os.Stdout)
4851
case "-z":
4952
fmt.Println("TODO: attach zip file")
5053
default:

0 commit comments

Comments
 (0)