diff options
| author | nsfisis <nsfisis@gmail.com> | 2023-03-10 00:15:37 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2023-03-10 00:15:37 +0900 |
| commit | f7f8ec295f568313312bc4243150cb472cfe8a12 (patch) | |
| tree | 743947e3e3e602d4ab96d7e9308abc076e69fbc1 /internal | |
| parent | d3bed78b099eba23f5c037d3de9561acddfc1ee1 (diff) | |
| download | term-clock-f7f8ec295f568313312bc4243150cb472cfe8a12.tar.gz term-clock-f7f8ec295f568313312bc4243150cb472cfe8a12.tar.zst term-clock-f7f8ec295f568313312bc4243150cb472cfe8a12.zip | |
refactor: move common stuff to internal/term package
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/term/draw.go | 112 | ||||
| -rw-r--r-- | internal/term/screen.go | 70 |
2 files changed, 182 insertions, 0 deletions
diff --git a/internal/term/draw.go b/internal/term/draw.go new file mode 100644 index 0000000..c6f297f --- /dev/null +++ b/internal/term/draw.go @@ -0,0 +1,112 @@ +package term + +import ( + "github.com/gdamore/tcell/v2" +) + +type Style tcell.Style; + +var ( + BgStyle Style + FgStyle Style +) + +func init() { + BgStyle = Style(tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)) + FgStyle = Style(tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.ColorOlive)) +} + +func DrawSquare(scr *Screen, xOffset, yOffset, w, h int, style Style) { + for dx := 0; dx < w; dx++ { + x := xOffset + dx + for dy := 0; dy < h; dy++ { + y := yOffset + dy + scr.scr.SetContent(x, y, ' ', nil, tcell.Style(style)) + } + } +} + +func DrawNumber(scr *Screen, n, xOffset, yOffset, squareW, squareH int, style Style) { + defs := [...][15]bool{ + { + true, true, true, + true, false, true, + true, false, true, + true, false, true, + true, true, true, + }, + { + false, false, true, + false, false, true, + false, false, true, + false, false, true, + false, false, true, + }, + { + true, true, true, + false, false, true, + true, true, true, + true, false, false, + true, true, true, + }, + { + true, true, true, + false, false, true, + true, true, true, + false, false, true, + true, true, true, + }, + { + true, false, true, + true, false, true, + true, true, true, + false, false, true, + false, false, true, + }, + { + true, true, true, + true, false, false, + true, true, true, + false, false, true, + true, true, true, + }, + { + true, true, true, + true, false, false, + true, true, true, + true, false, true, + true, true, true, + }, + { + true, true, true, + false, false, true, + false, false, true, + false, false, true, + false, false, true, + }, + { + true, true, true, + true, false, true, + true, true, true, + true, false, true, + true, true, true, + }, + { + true, true, true, + true, false, true, + true, true, true, + false, false, true, + true, true, true, + }, + } + + squares := defs[n] + for i, draw := range squares { + if !draw { + continue + } + x := i % 3 + y := i / 3 + DrawSquare(scr, xOffset+squareW*x, yOffset+squareH*y, squareW, squareH, style) + } +} diff --git a/internal/term/screen.go b/internal/term/screen.go new file mode 100644 index 0000000..14bfcfb --- /dev/null +++ b/internal/term/screen.go @@ -0,0 +1,70 @@ +package term + +import ( + "github.com/gdamore/tcell/v2" +) + +type Screen struct { + scr tcell.Screen + onResize func() bool + QuitC chan struct{} +} + +func NewScreen() (*Screen, error) { + scr, err := tcell.NewScreen() + if err != nil{ + return nil, err + } + err = scr.Init() + if err != nil{ + return nil, err + } + return &Screen{ + scr: scr, + QuitC: make(chan struct{}), + }, nil +} + +func (scr *Screen) Close() { + scr.scr.Fini() +} + +func (scr *Screen) Size() (int, int) { + return scr.scr.Size() +} + +func (scr *Screen) Clear(style Style) { + scr.scr.SetStyle(tcell.Style(style)) + scr.scr.Clear() +} + +func (scr *Screen) OnResize(handler func() bool) { + scr.onResize = handler +} + +func (scr *Screen) Show() { + scr.scr.Show() +} + +func (scr *Screen) DoEventLoop() { + for { + scr.scr.Show() + + ev := scr.scr.PollEvent() + switch ev := ev.(type) { + case *tcell.EventResize: + if scr.onResize != nil { + if quit := scr.onResize(); quit { + return + } + } + scr.scr.Sync() + case *tcell.EventKey: + if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC || ev.Rune() == 'q' { + close(scr.QuitC) + return + } + scr.scr.Sync() + } + } +} |
