library/cmd/manage/main.go

107 lines
2.4 KiB
Go
Raw Normal View History

2021-06-26 18:08:35 +00:00
package main
2021-07-03 00:46:58 +00:00
import (
"log"
"git.yetaga.in/alazyreader/library/config"
"github.com/gdamore/tcell"
"github.com/kelseyhightower/envconfig"
)
2021-06-26 18:08:35 +00:00
func main() {
2021-07-03 00:46:58 +00:00
var c config.Config
err := envconfig.Process("library", &c)
if err != nil {
log.Fatalln(err)
}
screen, err := tcell.NewScreen()
if err != nil {
log.Fatalln(err)
}
err = screen.Init()
if err != nil {
log.Fatalln(err)
}
screen.Clear()
inc := 0
for {
e := screen.PollEvent()
screen.Clear()
w, h := screen.Size()
b := &box{h: h, w: w, x: 0, y: 0, title: "library", menuItems: []string{"(e)dit", "(q)uit"}}
switch v := e.(type) {
case *tcell.EventError:
screen.Beep()
b.draw(screen)
screen.SetContent(1, 1, 'E', nil, tcell.StyleDefault)
case *tcell.EventInterrupt:
b.draw(screen)
screen.Sync()
case *tcell.EventKey:
inc += 1
b.draw(screen)
screen.SetContent(1, 1, v.Rune(), nil, tcell.StyleDefault)
screen.SetContent(2, 2, rune(inc+48), nil, tcell.StyleDefault)
if v.Rune() == 'q' {
screen.Fini()
return
}
screen.Sync()
case *tcell.EventMouse:
continue
case *tcell.EventResize:
b.draw(screen)
screen.Sync()
case *tcell.EventTime:
continue
}
}
}
type box struct {
x, y int
h, w int
title string
menuItems []string
}
func (b *box) draw(s tcell.Screen) {
for m := 1; m < b.w-1; m++ {
s.SetContent(m, b.y, tcell.RuneHLine, nil, tcell.StyleDefault)
s.SetContent(m, b.h-1, tcell.RuneHLine, nil, tcell.StyleDefault)
}
for m := 1; m < b.h-1; m++ {
s.SetContent(b.x, m, tcell.RuneVLine, nil, tcell.StyleDefault)
s.SetContent(b.w-1, m, tcell.RuneVLine, nil, tcell.StyleDefault)
}
s.SetContent(b.x, b.y, tcell.RuneULCorner, nil, tcell.StyleDefault)
s.SetContent(b.w-1, b.y, tcell.RuneURCorner, nil, tcell.StyleDefault)
s.SetContent(b.x, b.h-1, tcell.RuneLLCorner, nil, tcell.StyleDefault)
s.SetContent(b.w-1, b.h-1, tcell.RuneLRCorner, nil, tcell.StyleDefault)
if b.title != "" {
t := b.x + 2
s.SetContent(t, b.y, ' ', nil, tcell.StyleDefault)
t++
for _, r := range b.title {
s.SetContent(t, b.y, r, nil, tcell.StyleDefault)
t++
}
s.SetContent(t, b.y, ' ', nil, tcell.StyleDefault)
}
if len(b.menuItems) != 0 {
t := b.x + 2
s.SetContent(t, b.h-1, ' ', nil, tcell.StyleDefault)
t++
for i := range b.menuItems {
for _, r := range b.menuItems[i] {
s.SetContent(t, b.h-1, r, nil, tcell.StyleDefault)
t++
}
s.SetContent(t, b.h-1, ' ', nil, tcell.StyleDefault)
t++
}
}
2021-06-26 18:08:35 +00:00
}