Implemented TUI draft, testing stuff
parent
e1dbc4ba2e
commit
fdb98ee403
@ -0,0 +1,50 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image/color"
|
||||
|
||||
"github.com/eliukblau/pixterm/pkg/ansimage"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
|
||||
type Splashscreen struct {
|
||||
Canvas *tview.TextView
|
||||
View
|
||||
ImageBytes []byte
|
||||
}
|
||||
|
||||
func(t *TUI) NewSplashscreen(logo *[]byte) (*Splashscreen) {
|
||||
splashscreen := new(Splashscreen)
|
||||
|
||||
canvas := tview.NewTextView().
|
||||
SetDynamicColors(true).
|
||||
SetRegions(true).
|
||||
SetWrap(true)
|
||||
canvas.SetBorder(false)
|
||||
canvas.Clear()
|
||||
|
||||
splashscreen.ImageBytes = *logo
|
||||
|
||||
splashscreen.Canvas = canvas
|
||||
return splashscreen
|
||||
}
|
||||
|
||||
func (splashscreen *Splashscreen) GetCanvas() (tview.Primitive) {
|
||||
return splashscreen.Canvas
|
||||
}
|
||||
|
||||
func(splashscreen *Splashscreen) Draw() {
|
||||
canvas := splashscreen.Canvas
|
||||
_, _, w, h := canvas.Box.GetRect()
|
||||
|
||||
logoImage, err := ansimage.NewScaledFromReader(bytes.NewReader(splashscreen.ImageBytes), h, w, color.Black, ansimage.ScaleModeFill, ansimage.NoDithering)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
canvas.Clear()
|
||||
fmt.Fprint(canvas, tview.TranslateANSI(logoImage.RenderExt(false, false)))
|
||||
}
|
||||
|
@ -0,0 +1,75 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"log"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
type TUI struct {
|
||||
App *tview.Application
|
||||
Views map[string]View
|
||||
ActiveView string
|
||||
}
|
||||
|
||||
type View interface {
|
||||
GetCanvas() (tview.Primitive)
|
||||
Draw()
|
||||
}
|
||||
|
||||
func Init(embedfs *embed.FS) (*TUI) {
|
||||
t := new(TUI)
|
||||
|
||||
tview.Styles = tview.Theme{
|
||||
PrimitiveBackgroundColor: tcell.ColorDefault,
|
||||
ContrastBackgroundColor: tcell.ColorTeal,
|
||||
MoreContrastBackgroundColor: tcell.ColorTeal,
|
||||
BorderColor: tcell.ColorWhite,
|
||||
TitleColor: tcell.ColorWhite,
|
||||
GraphicsColor: tcell.ColorWhite,
|
||||
PrimaryTextColor: tcell.ColorDefault,
|
||||
SecondaryTextColor: tcell.ColorBlue,
|
||||
TertiaryTextColor: tcell.ColorGreen,
|
||||
InverseTextColor: tcell.ColorBlack,
|
||||
ContrastSecondaryTextColor: tcell.ColorDarkCyan,
|
||||
}
|
||||
|
||||
t.App = tview.NewApplication()
|
||||
|
||||
logoBytes, err := embedfs.ReadFile("superhighway84.png")
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
t.Views = make(map[string]View)
|
||||
t.Views["splashscreen"] = t.NewSplashscreen(&logoBytes)
|
||||
|
||||
t.initInput()
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *TUI) initInput() {
|
||||
t.App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch event.Key() {
|
||||
case tcell.KeyCtrlR:
|
||||
t.Draw()
|
||||
return nil
|
||||
case tcell.KeyCtrlQ:
|
||||
t.App.Stop()
|
||||
}
|
||||
|
||||
return event
|
||||
})
|
||||
}
|
||||
|
||||
func(t *TUI) SetView(name string) {
|
||||
t.App.SetRoot(t.Views[name].GetCanvas(), true)
|
||||
t.ActiveView = name
|
||||
}
|
||||
|
||||
func (t *TUI) Draw() {
|
||||
t.Views[t.ActiveView].Draw()
|
||||
}
|
||||
|
Loading…
Reference in New Issue