-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathansi.go
50 lines (44 loc) · 1015 Bytes
/
ansi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"bufio"
"fmt"
"github.com/mgutz/ansi"
"github.com/qpliu/qrencode-go/qrencode"
"io"
)
func printAnsiArtQr(w_in io.Writer, grid *qrencode.BitGrid, inverse bool) {
// Buffering required for Windows (go-colorable) support
w := bufio.NewWriterSize(w_in, 1024)
reset := ansi.ColorCode("reset")
black := ansi.ColorCode(":black")
white := ansi.ColorCode(":white")
if inverse {
black, white = white, black
}
height := grid.Height()
width := grid.Width()
line := white + fmt.Sprintf("%*s", width*2+2, "") + reset + "\n"
fmt.Fprint(w, line)
for y := 0; y < height; y++ {
fmt.Fprint(w, white, " ")
color_prev := white
for x := 0; x < width; x++ {
if grid.Get(x, y) {
if color_prev != black {
fmt.Fprint(w, black)
color_prev = black
}
} else {
if color_prev != white {
fmt.Fprint(w, white)
color_prev = white
}
}
fmt.Fprint(w, " ")
}
fmt.Fprint(w, white, " ", reset, "\n")
w.Flush()
}
fmt.Fprint(w, line)
w.Flush()
}