boltbrowser/ui/screen_about.go

173 lines
4.6 KiB
Go

package ui
import (
"fmt"
"git.bullercodeworks.com/brian/wandle"
"github.com/nsf/termbox-go"
"github.com/spf13/viper"
)
const (
ABT_CmdGotoBrowse = AboutId | iota
)
type AboutMsg struct {
source int
data interface{}
err error
}
type aboutScreen struct {
ui *Ui
commands1 []Command
commands2 []Command
}
type Command struct {
key string
description string
}
func NewAboutScreen(u *Ui) *aboutScreen { return &aboutScreen{ui: u} }
func (s *aboutScreen) Init() wandle.Cmd {
s.commands1 = []Command{
{"h,←", "close parent"},
{"j,↓", "down"},
{"k,↑", "up"},
{"l,→", "open item"},
{"J", "scroll right pane down"},
{"K", "scroll right pane up"},
{"", ""},
{"g", "goto top"},
{"G", "goto bottom"},
{"", ""},
{"ctrl+f", "jump down"},
{"ctrl+b", "jump up"},
}
s.commands2 = []Command{
{"p,P", "create pair/at parent"},
{"b,B", "create bucket/at parent"},
{"e", "edit value of pair"},
{"r", "rename pair/bucket"},
{"", ""},
{"", ""},
{"D", "delete item"},
{"x,X", "export as string/json to file"},
{"", ""},
{"?", "this screen"},
{"q", "quit program"},
}
return nil
}
func (s *aboutScreen) Update(msg wandle.Msg) wandle.Cmd {
switch msg := msg.(type) {
case AboutMsg:
return s.handleAboutMsg(msg)
case termbox.Event:
return s.handleTermboxEvent(msg)
}
return nil
}
func (s *aboutScreen) handleAboutMsg(msg AboutMsg) wandle.Cmd {
switch msg.source {
case ABT_CmdGotoBrowse:
return wandle.SwitchScreenCmd(s.ui.browseScreen)
}
return nil
}
func (s *aboutScreen) handleTermboxEvent(msg termbox.Event) wandle.Cmd {
if msg.Type == termbox.EventKey {
return wandle.SwitchScreenCmd(s.ui.browseScreen)
}
return nil
}
func (s *aboutScreen) View(style wandle.Style) {
width, height := termbox.Size()
template := [...]string{
" _______ _______ ___ _______ _______ ______ _______ _ _ _______ _______ ______ ",
"| _ || || | | || _ || _ | | || | _ | || || || _ | ",
"| |_| || _ || | |_ _|| |_| || | || | _ || || || || _____|| ___|| | || ",
"| || | | || | | | | || |_||_ | | | || || |_____ | |___ | |_||_ ",
"| _ | | |_| || |___ | | | _ | | __ || |_| || ||_____ || ___|| __ |",
"| |_| || || || | | |_| || | | || || _ | _____| || |___ | | | |",
"|_______||_______||_______||___| |_______||___| |_||_______||__| |__||_______||_______||___| |_|",
}
if width < 100 {
template = [...]string{
" ____ ____ _ _____ ____ ____ ____ _ _ ____ ___ ____ ",
"| _ || || | | || _ || _ | | || | _ | || || || _ | ",
"| |_||| _ || | |_ _|| |_||| | || | _ || || || || ___|| _|| | || ",
"| ||| | || | | | | || |_|| || | || |||___ | |_ | |_||_ ",
"| _ |||_| || |___| | | _ || _ |||_| || ||__ || _|| __ |",
"| |_||| || | | | |_||| | | || || _ | __| || |_ | | | |",
"|____||____||_____|_| |____||_| |_||____||__| |__||____||___||_| |_|",
}
}
firstLine := template[0]
startX := (width - len(firstLine)) / 2
//startX := (width - len(firstLine)) / 2
startY := ((height - 2*len(template)) / 2) - 2
xPos := startX
yPos := startY
if height <= 20 {
title := "BoltBrowser"
startY = 0
yPos = 0
wandle.Print((width-len(title))/2, startY, style, title)
} else {
if height < 25 {
startY = 0
yPos = 0
}
for _, line := range template {
xPos = startX
for _, runeValue := range line {
displayRune := ' '
if runeValue != ' ' {
displayRune = runeValue
termbox.SetCell(xPos, yPos, displayRune, style.Foreground, style.Background)
}
xPos++
}
yPos++
}
}
yPos++
versionString := fmt.Sprintf("Version: %0.1f", viper.GetFloat64("version"))
wandle.Print((width-len(versionString))/2, yPos, style, versionString)
var maxCmd1 int
for k := range s.commands1 {
tst := len(s.commands1[k].key) + 1 + len(s.commands1[k].description)
if tst > maxCmd1 {
maxCmd1 = tst
}
}
var maxCmd2 int
for k := range s.commands2 {
tst := len(s.commands2[k].key) + 1 + len(s.commands2[k].description)
if tst > maxCmd2 {
maxCmd2 = tst
}
}
xPos = (width / 2) - ((maxCmd1 + maxCmd2) / 2)
yPos++
for k, cmd := range s.commands1 {
wandle.Print(xPos, yPos+1+k, style, fmt.Sprintf("%6s %s", cmd.key, cmd.description))
}
for k, cmd := range s.commands2 {
wandle.Print(xPos+40, yPos+1+k, style, fmt.Sprintf("%6s %s", cmd.key, cmd.description))
}
exitTxt := "Press any key to return to browser"
st := style.Bold(true)
wandle.Print((width-len(exitTxt))/2, height-1, st, exitTxt)
}