Skip to content

Commit

Permalink
fix(terminal): poll and wait termbox event so that we get the output …
Browse files Browse the repository at this point in the history
…of termbox buffer. (yeqown#95)

Co-authored-by: Yeqown.Y <[email protected]>
  • Loading branch information
yeqown and Yeqown.Y authored Nov 15, 2023
1 parent 847954f commit 11f70a2
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions writer/terminal/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terminal
import (
"github.com/yeqown/go-qrcode/v2"

"github.com/mattn/go-runewidth"
termbox "github.com/nsf/termbox-go"
)

Expand Down Expand Up @@ -57,20 +58,50 @@ func (w Writer) Write(mat qrcode.Matrix) error {
bg := termbox.ColorWhite
fg := termbox.ColorBlack

padding := 1
padding, curRow := 1, 0
w.preDraw(ww, hh, padding, bg)
mat.Iterate(qrcode.IterDirection_ROW, func(x int, y int, state qrcode.QRValue) {

if state.IsSet() {
fg = termbox.ColorBlack
} else {
fg = termbox.ColorWhite
}

w.drawBlock(x, y, padding, fg, bg)
curRow = y
})

return termbox.Flush()
printTip(curRow + 2*padding + 1 + 1)
return hold()
}

func printTip(y int) {
tip := "Press any key to quit."
x := 0
for _, r := range tip {
w := runewidth.RuneWidth(r)
if w == 0 || (w == 2 && runewidth.IsAmbiguousWidth(r)) {
w = 1
}
termbox.SetCell(x, y, r, termbox.ColorDefault, termbox.ColorDefault)
x += w
}
}

func hold() error {
if err := termbox.Flush(); err != nil {
return err
}

wait:
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
break wait
}
}

return nil
}

func (w Writer) Close() error {
Expand Down

0 comments on commit 11f70a2

Please sign in to comment.