-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples): add img2term example
Only writes Sixel images for now
- Loading branch information
1 parent
e46a2b7
commit fdb1448
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"image" | ||
"io" | ||
"log" | ||
"os" | ||
|
||
_ "image/jpeg" | ||
_ "image/png" | ||
|
||
"github.com/charmbracelet/x/ansi" | ||
"github.com/charmbracelet/x/ansi/sixel" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
args := flag.Args() | ||
if len(args) == 0 { | ||
flag.Usage() | ||
os.Exit(1) | ||
} | ||
|
||
f, err := os.Open(args[0]) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
defer f.Close() //nolint:errcheck | ||
img, _, err := image.Decode(f) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if _, err := writeSixel(os.Stdout, img); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func writeSixel(w io.Writer, img image.Image) (int, error) { | ||
var buf bytes.Buffer | ||
var e sixel.Encoder | ||
if err := e.Encode(&buf, img); err != nil { | ||
return 0, err | ||
} | ||
|
||
return io.WriteString(w, ansi.SixelGraphics(0, 1, 0, buf.Bytes())) | ||
} |