This commit is contained in:
156
ui/mock.go
156
ui/mock.go
@@ -1,156 +0,0 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
type coord struct {
|
||||
x, y int
|
||||
}
|
||||
|
||||
type MockScreen struct {
|
||||
x, y, h, w int
|
||||
content map[coord]rune
|
||||
}
|
||||
|
||||
func (m *MockScreen) Init() error {
|
||||
m.content = map[coord]rune{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockScreen) Fini() {}
|
||||
|
||||
func (m *MockScreen) Clear() {
|
||||
m.content = map[coord]rune{}
|
||||
}
|
||||
|
||||
func (m *MockScreen) Fill(rune, tcell.Style) {}
|
||||
|
||||
func (m *MockScreen) SetCell(x int, y int, style tcell.Style, ch ...rune) {}
|
||||
|
||||
func (m *MockScreen) GetContent(x, y int) (mainc rune, combc []rune, style tcell.Style, width int) {
|
||||
return m.content[coord{x, y}], nil, tcell.StyleDefault, 1
|
||||
}
|
||||
|
||||
func (m *MockScreen) SetContent(x int, y int, mainc rune, combc []rune, style tcell.Style) {
|
||||
m.content[coord{x, y}] = mainc
|
||||
}
|
||||
|
||||
func (m *MockScreen) Suspend() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockScreen) Resume() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockScreen) SetStyle(style tcell.Style) {}
|
||||
|
||||
func (m *MockScreen) SetCursorStyle(style tcell.CursorStyle, colors ...tcell.Color) {}
|
||||
|
||||
func (m *MockScreen) ShowCursor(x int, y int) {}
|
||||
|
||||
func (m *MockScreen) HideCursor() {}
|
||||
|
||||
func (m *MockScreen) Size() (int, int) {
|
||||
return m.h, m.w
|
||||
}
|
||||
|
||||
func (m *MockScreen) PollEvent() tcell.Event {
|
||||
return tcell.NewEventError(fmt.Errorf("mock error"))
|
||||
}
|
||||
|
||||
func (m *MockScreen) ChannelEvents(ch chan<- tcell.Event, quit <-chan struct{}) {}
|
||||
|
||||
func (m *MockScreen) HasPendingEvent() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (m *MockScreen) PostEvent(ev tcell.Event) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockScreen) PostEventWait(ev tcell.Event) {}
|
||||
|
||||
func (m *MockScreen) EnableMouse(...tcell.MouseFlags) {}
|
||||
|
||||
func (m *MockScreen) DisableMouse() {}
|
||||
|
||||
func (m *MockScreen) EnablePaste() {}
|
||||
|
||||
func (m *MockScreen) DisablePaste() {}
|
||||
|
||||
func (m *MockScreen) EnableFocus() {}
|
||||
|
||||
func (m *MockScreen) DisableFocus() {}
|
||||
|
||||
func (m *MockScreen) HasMouse() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (m *MockScreen) Colors() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *MockScreen) Show() {}
|
||||
|
||||
func (m *MockScreen) Sync() {}
|
||||
|
||||
func (m *MockScreen) CharacterSet() string {
|
||||
return "UTF-8"
|
||||
}
|
||||
|
||||
func (m *MockScreen) RegisterRuneFallback(r rune, subst string) {}
|
||||
|
||||
func (m *MockScreen) UnregisterRuneFallback(r rune) {}
|
||||
|
||||
func (m *MockScreen) CanDisplay(r rune, checkFallbacks bool) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *MockScreen) Resize(x, y, h, w int) {
|
||||
m.x, m.y, m.h, m.w = x, y, h, w
|
||||
}
|
||||
|
||||
func (m *MockScreen) SetSize(h, w int) {
|
||||
m.h, m.w = h, w
|
||||
}
|
||||
|
||||
func (m *MockScreen) HasKey(tcell.Key) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *MockScreen) Beep() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockScreen) SetTitle(string) {}
|
||||
|
||||
func (m *MockScreen) GetClipboard() {}
|
||||
|
||||
func (m *MockScreen) SetClipboard([]byte) {}
|
||||
|
||||
func (m *MockScreen) LockRegion(x, y, width, height int, lock bool) {}
|
||||
|
||||
func (m *MockScreen) Tty() (tcell.Tty, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (m *MockScreen) DumpContents() string {
|
||||
var res string
|
||||
for i := m.y; i < m.h; i++ {
|
||||
str := []rune{}
|
||||
for j := m.x; j < m.w; j++ {
|
||||
r, ok := m.content[coord{x: j, y: i}]
|
||||
if ok {
|
||||
str = append(str, r)
|
||||
} else {
|
||||
str = append(str, ' ')
|
||||
}
|
||||
}
|
||||
res = res + string(str) + "\n"
|
||||
}
|
||||
return res
|
||||
}
|
616
ui/ui.go
616
ui/ui.go
@@ -1,616 +0,0 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.yetaga.in/alazyreader/library/media"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
type Drawable interface {
|
||||
Draw(tcell.Screen)
|
||||
SetSize(x, y, h, w int)
|
||||
SetStyle(tcell.Style)
|
||||
SetVisible(bool)
|
||||
}
|
||||
|
||||
type Offsets struct {
|
||||
Top int
|
||||
Bottom int
|
||||
Left int
|
||||
Right int
|
||||
Percent int
|
||||
}
|
||||
|
||||
type Contents []struct {
|
||||
Offsets Offsets
|
||||
Container Drawable
|
||||
}
|
||||
|
||||
const (
|
||||
LayoutUnmanaged = iota
|
||||
LayoutHorizontalEven
|
||||
LayoutVerticalEven
|
||||
LayoutHorizontalPercent
|
||||
LayoutVerticalPercent
|
||||
)
|
||||
|
||||
var (
|
||||
StyleActive = tcell.Style{}.Foreground(tcell.ColorWhite).Background(tcell.ColorBlack)
|
||||
StyleInactive = tcell.Style{}.Foreground(tcell.ColorGray).Background(tcell.ColorBlack)
|
||||
)
|
||||
|
||||
// A Container has no visible UI of its own, but arranges sub-components on the screen.
|
||||
// layoutMethod manages how subcomponents are organized. If `LayoutUnmanaged`, it just uses the offsets
|
||||
// in contents to paint on the screen. Otherwise, `LayoutHorizontalEven` and `LayoutVerticalEven` will
|
||||
// have it compute even distributions of space for all components either horizontally or vertically,
|
||||
// filling the container.
|
||||
type Container struct {
|
||||
x, y int
|
||||
h, w int
|
||||
layoutMethod int
|
||||
contents Contents
|
||||
visible bool
|
||||
}
|
||||
|
||||
func NewContainer(contents Contents, layoutMethod int) *Container {
|
||||
return &Container{
|
||||
layoutMethod: layoutMethod,
|
||||
contents: contents,
|
||||
visible: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Container) Draw(s tcell.Screen) {
|
||||
if !c.visible {
|
||||
return
|
||||
}
|
||||
for i := range c.contents {
|
||||
c.contents[i].Container.Draw(s)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Container) SetSize(x, y, h, w int) {
|
||||
c.x, c.y, c.h, c.w = x, y, h, w
|
||||
carry := 0
|
||||
if c.layoutMethod == LayoutVerticalEven {
|
||||
num := len(c.contents)
|
||||
extra := c.h % num
|
||||
for r := range c.contents {
|
||||
w := c.w
|
||||
h := c.h / num
|
||||
x := c.x
|
||||
y := c.y + (h * r) + carry
|
||||
if extra > 0 { // distribute "extra" space to containers as we have some left
|
||||
h++
|
||||
extra--
|
||||
carry++
|
||||
}
|
||||
c.contents[r].Container.SetSize(x, y, h, w)
|
||||
}
|
||||
} else if c.layoutMethod == LayoutHorizontalEven {
|
||||
num := len(c.contents)
|
||||
extra := c.w % num
|
||||
for r := range c.contents {
|
||||
w := c.w / num
|
||||
h := c.h
|
||||
x := c.x + (w * r) + carry
|
||||
y := c.y
|
||||
if extra > 0 { // distribute "extra" space to containers as we have some left
|
||||
w++
|
||||
extra--
|
||||
carry++
|
||||
}
|
||||
c.contents[r].Container.SetSize(x, y, h, w)
|
||||
}
|
||||
} else if c.layoutMethod == LayoutHorizontalPercent {
|
||||
// first, work out overall distribution
|
||||
total := 0
|
||||
for r := range c.contents {
|
||||
// `0` or negatives are set as minimum
|
||||
if c.contents[r].Offsets.Percent < 1 {
|
||||
total += 1
|
||||
} else {
|
||||
total += c.contents[r].Offsets.Percent
|
||||
}
|
||||
}
|
||||
carry := 0
|
||||
// push around containers
|
||||
for r := range c.contents {
|
||||
ratio := (float64(c.contents[r].Offsets.Percent) / float64(total))
|
||||
w := int(float64(c.w) * ratio)
|
||||
h := c.h
|
||||
x := c.x + carry
|
||||
y := c.y
|
||||
carry += w
|
||||
// and add any remaining space to the last container
|
||||
if r == len(c.contents)-1 {
|
||||
w += (c.w - carry)
|
||||
}
|
||||
c.contents[r].Container.SetSize(x, y, h, w)
|
||||
}
|
||||
} else if c.layoutMethod == LayoutVerticalPercent {
|
||||
// first, work out overall distribution
|
||||
total := 0
|
||||
for r := range c.contents {
|
||||
// `0` or negatives are set as minimum
|
||||
if c.contents[r].Offsets.Percent < 1 {
|
||||
total += 1
|
||||
} else {
|
||||
total += c.contents[r].Offsets.Percent
|
||||
}
|
||||
}
|
||||
carry := 0
|
||||
// push around containers
|
||||
for r := range c.contents {
|
||||
ratio := (float64(c.contents[r].Offsets.Percent) / float64(total))
|
||||
w := c.w
|
||||
h := int(float64(c.h) * ratio)
|
||||
x := c.x
|
||||
y := c.y + carry
|
||||
carry += h
|
||||
// and add any remaining space to the last container
|
||||
if r == len(c.contents)-1 {
|
||||
h += (c.h - carry)
|
||||
}
|
||||
c.contents[r].Container.SetSize(x, y, h, w)
|
||||
}
|
||||
} else {
|
||||
for r := range c.contents {
|
||||
x := c.x + c.contents[r].Offsets.Left
|
||||
y := c.y + c.contents[r].Offsets.Top
|
||||
h := c.h - c.contents[r].Offsets.Bottom
|
||||
w := c.w - c.contents[r].Offsets.Right
|
||||
c.contents[r].Container.SetSize(x, y, h, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Container) SetStyle(s tcell.Style) {
|
||||
// containers have no visible elements to style
|
||||
}
|
||||
|
||||
func (c *Container) SetVisible(b bool) {
|
||||
c.visible = b
|
||||
}
|
||||
|
||||
func (c *Container) Contents() Contents {
|
||||
return c.contents
|
||||
}
|
||||
|
||||
func (c *Container) SetContents(con Contents) {
|
||||
c.contents = con
|
||||
}
|
||||
|
||||
// A Box draws a ASCII box around its contents, with an optional title and footer.
|
||||
type Box struct {
|
||||
x, y int
|
||||
h, w int
|
||||
title Drawable
|
||||
menuItems Drawable
|
||||
contents Contents
|
||||
style tcell.Style
|
||||
cascade bool
|
||||
visible bool
|
||||
transparent bool
|
||||
}
|
||||
|
||||
func NewBox(title string, menuItems []string, contents Contents, initialStyle tcell.Style, cascade bool) *Box {
|
||||
return &Box{
|
||||
title: NewPaddedText(title),
|
||||
menuItems: NewPaddedText(strings.Join(menuItems, " ")),
|
||||
contents: contents,
|
||||
style: initialStyle,
|
||||
cascade: cascade,
|
||||
visible: true,
|
||||
transparent: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Box) SetSize(x, y, h, w int) {
|
||||
b.x, b.y, b.h, b.w = x, y, h, w
|
||||
b.title.SetSize(b.x+2, b.y, 0, 0)
|
||||
b.menuItems.SetSize(b.x+2, b.y+b.h-1, 0, 0)
|
||||
for c := range b.contents {
|
||||
x := b.x + b.contents[c].Offsets.Left
|
||||
y := b.y + b.contents[c].Offsets.Top
|
||||
h := b.h - b.contents[c].Offsets.Bottom
|
||||
w := b.w - b.contents[c].Offsets.Right
|
||||
b.contents[c].Container.SetSize(x, y, h, w)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Box) Draw(s tcell.Screen) {
|
||||
if !b.visible {
|
||||
return
|
||||
}
|
||||
// blank out inner area
|
||||
if !b.transparent {
|
||||
for m := b.x + 1; m < b.x+b.w-1; m++ {
|
||||
for n := b.y + 1; n < b.y+b.h-1; n++ {
|
||||
s.SetContent(m, n, ' ', nil, b.style)
|
||||
}
|
||||
}
|
||||
}
|
||||
// draw outside bars
|
||||
for m := b.x + 1; m < b.x+b.w-1; m++ {
|
||||
s.SetContent(m, b.y, tcell.RuneHLine, nil, b.style)
|
||||
s.SetContent(m, b.y+b.h-1, tcell.RuneHLine, nil, b.style)
|
||||
}
|
||||
for m := b.y + 1; m < b.y+b.h-1; m++ {
|
||||
s.SetContent(b.x, m, tcell.RuneVLine, nil, b.style)
|
||||
s.SetContent(b.x+b.w-1, m, tcell.RuneVLine, nil, b.style)
|
||||
}
|
||||
s.SetContent(b.x, b.y, tcell.RuneULCorner, nil, b.style)
|
||||
s.SetContent(b.x+b.w-1, b.y, tcell.RuneURCorner, nil, b.style)
|
||||
s.SetContent(b.x, b.y+b.h-1, tcell.RuneLLCorner, nil, b.style)
|
||||
s.SetContent(b.x+b.w-1, b.y+b.h-1, tcell.RuneLRCorner, nil, b.style)
|
||||
|
||||
if b.title != nil {
|
||||
b.title.Draw(s)
|
||||
}
|
||||
if b.menuItems != nil {
|
||||
b.menuItems.Draw(s)
|
||||
}
|
||||
for c := range b.contents {
|
||||
b.contents[c].Container.Draw(s)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Box) SetStyle(s tcell.Style) {
|
||||
b.style = s
|
||||
b.title.SetStyle(s)
|
||||
b.menuItems.SetStyle(s)
|
||||
if b.cascade {
|
||||
for c := range b.contents {
|
||||
b.contents[c].Container.SetStyle(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Box) SetVisible(v bool) {
|
||||
b.visible = v
|
||||
}
|
||||
|
||||
func (b *Box) SetTransparent(v bool) {
|
||||
b.transparent = v
|
||||
}
|
||||
|
||||
func (b *Box) Contents() Contents {
|
||||
return b.contents
|
||||
}
|
||||
|
||||
func (b *Box) SetContents(c Contents) {
|
||||
b.contents = c
|
||||
}
|
||||
|
||||
// A List is a scrollable, pageable list with a selector token.
|
||||
type List struct {
|
||||
x, y int
|
||||
h, w int
|
||||
selected int
|
||||
listItems []ListKeyValue
|
||||
style tcell.Style
|
||||
visible bool
|
||||
}
|
||||
|
||||
type ListKeyValue struct {
|
||||
Key int
|
||||
Value string
|
||||
}
|
||||
|
||||
func NewList(listItems []ListKeyValue, initialSelected int) *List {
|
||||
return &List{
|
||||
listItems: listItems,
|
||||
selected: initialSelected,
|
||||
visible: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *List) SetSize(x, y, h, w int) {
|
||||
l.x, l.y, l.h, l.w = x, y, h, w
|
||||
}
|
||||
|
||||
func (l *List) Draw(s tcell.Screen) {
|
||||
if !l.visible {
|
||||
return
|
||||
}
|
||||
for i := range l.listItems {
|
||||
for j, r := range l.listItems[i].Value {
|
||||
s.SetContent(l.x+j, l.y+i, r, nil, l.style)
|
||||
}
|
||||
if i == l.selected {
|
||||
s.SetContent(l.x+len(l.listItems[i].Value)+1, l.y+i, '<', nil, l.style)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (l *List) SetVisible(b bool) {
|
||||
l.visible = b
|
||||
}
|
||||
|
||||
func (l *List) SetStyle(s tcell.Style) {
|
||||
l.style = s
|
||||
}
|
||||
|
||||
func (l *List) Selected() int {
|
||||
return l.selected
|
||||
}
|
||||
|
||||
func (l *List) SelectedID() int {
|
||||
if l.listItems == nil || len(l.listItems) == 0 {
|
||||
return 0
|
||||
}
|
||||
return l.listItems[l.selected].Key
|
||||
}
|
||||
|
||||
func (l *List) SetSelected(i int) {
|
||||
l.selected = i
|
||||
}
|
||||
|
||||
func (l *List) ListMembers() []ListKeyValue {
|
||||
return l.listItems
|
||||
}
|
||||
|
||||
func (l *List) SetMembers(lkv []ListKeyValue) {
|
||||
l.listItems = lkv
|
||||
}
|
||||
|
||||
// BookDetails displays an editable list of book details
|
||||
type BookDetails struct {
|
||||
x, y int
|
||||
h, w int
|
||||
book *media.Book
|
||||
style tcell.Style
|
||||
visible bool
|
||||
}
|
||||
|
||||
func NewBookDetails(b *media.Book) *BookDetails {
|
||||
return &BookDetails{
|
||||
book: b,
|
||||
visible: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *BookDetails) SetBook(b *media.Book) {
|
||||
l.book = b
|
||||
}
|
||||
|
||||
func (l *BookDetails) SetSize(x, y, h, w int) {
|
||||
l.x, l.y, l.h, l.w = x, y, h, w
|
||||
}
|
||||
|
||||
func (l *BookDetails) Draw(s tcell.Screen) {
|
||||
if l.book == nil {
|
||||
return
|
||||
}
|
||||
if !l.visible {
|
||||
return
|
||||
}
|
||||
items := []struct {
|
||||
label string
|
||||
value string
|
||||
}{
|
||||
{"Title", l.book.Title},
|
||||
{"Authors", strings.Join(l.book.Authors, ", ")},
|
||||
{"Sort Author", l.book.SortAuthor},
|
||||
{"ISBN-10", l.book.ISBN10},
|
||||
{"ISBN-13", l.book.ISBN13},
|
||||
{"Format", l.book.Format},
|
||||
{"Genre", l.book.Genre},
|
||||
{"Publisher", l.book.Publisher},
|
||||
{"Series", l.book.Series},
|
||||
{"Volume", l.book.Volume},
|
||||
{"Year", l.book.Year},
|
||||
{"Signed", strconv.FormatBool(l.book.Signed)},
|
||||
{"Cover URL", l.book.CoverURL},
|
||||
{"Notes", l.book.Notes},
|
||||
{"Description", l.book.Description},
|
||||
}
|
||||
for i := range items {
|
||||
if i < l.h-2 {
|
||||
kv := NewKeyValue(items[i].label, ": ", items[i].value)
|
||||
kv.SetSize(l.x, l.y+i, 0, 0)
|
||||
kv.SetStyle(l.style)
|
||||
kv.Draw(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (l *BookDetails) SetVisible(b bool) {
|
||||
l.visible = b
|
||||
}
|
||||
|
||||
func (l *BookDetails) SetStyle(s tcell.Style) {
|
||||
l.style = s
|
||||
}
|
||||
|
||||
// PaddedText outputs strings with a space on both sides.
|
||||
// Useful for generating headings, footers, etc. Used by Box.
|
||||
type PaddedText struct {
|
||||
x, y int
|
||||
h, w int
|
||||
text string
|
||||
style tcell.Style
|
||||
visible bool
|
||||
}
|
||||
|
||||
func NewPaddedText(text string) *PaddedText {
|
||||
return &PaddedText{text: text, visible: true}
|
||||
}
|
||||
|
||||
func (p *PaddedText) SetSize(x, y, _, _ int) {
|
||||
p.x, p.y, p.h, p.w = x, y, 1, len(p.text)+2
|
||||
}
|
||||
|
||||
func (p *PaddedText) SetStyle(s tcell.Style) {
|
||||
p.style = s
|
||||
}
|
||||
|
||||
func (p *PaddedText) Draw(s tcell.Screen) {
|
||||
if p.text == "" {
|
||||
return
|
||||
}
|
||||
if !p.visible {
|
||||
return
|
||||
}
|
||||
t := p.x
|
||||
s.SetContent(t, p.y, ' ', nil, p.style)
|
||||
t++
|
||||
for _, r := range p.text {
|
||||
s.SetContent(t, p.y, r, nil, p.style)
|
||||
t++
|
||||
}
|
||||
s.SetContent(t, p.y, ' ', nil, p.style)
|
||||
}
|
||||
|
||||
func (p *PaddedText) SetVisible(b bool) {
|
||||
p.visible = b
|
||||
}
|
||||
|
||||
type KeyValue struct {
|
||||
x, y int
|
||||
h, w int
|
||||
key string
|
||||
value string
|
||||
separator string
|
||||
style tcell.Style
|
||||
visible bool
|
||||
}
|
||||
|
||||
func NewKeyValue(key, separator, value string) *KeyValue {
|
||||
return &KeyValue{
|
||||
key: key,
|
||||
separator: separator,
|
||||
value: value,
|
||||
visible: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *KeyValue) SetSize(x, y, _, _ int) {
|
||||
p.x, p.y, p.h, p.w = x, y, 1, len(p.key)+len(p.separator)+len(p.value)
|
||||
}
|
||||
|
||||
func (p *KeyValue) SetStyle(s tcell.Style) {
|
||||
p.style = s
|
||||
}
|
||||
|
||||
func (p *KeyValue) Draw(s tcell.Screen) {
|
||||
if !p.visible {
|
||||
return
|
||||
}
|
||||
for j, r := range p.key {
|
||||
s.SetContent(p.x+j, p.y, r, nil, p.style)
|
||||
}
|
||||
for j, r := range p.separator {
|
||||
s.SetContent(p.x+len(p.key)+j, p.y, r, nil, p.style)
|
||||
}
|
||||
for j, r := range p.value {
|
||||
s.SetContent(p.x+len(p.key)+len(p.separator)+j, p.y, r, nil, p.style)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *KeyValue) SetVisible(b bool) {
|
||||
p.visible = b
|
||||
}
|
||||
|
||||
func (p *KeyValue) GetValue() string {
|
||||
return p.value
|
||||
}
|
||||
|
||||
type EditableTextLine struct {
|
||||
x, y int
|
||||
h, w int
|
||||
text string
|
||||
style tcell.Style
|
||||
visible bool
|
||||
cursorPos int
|
||||
showCursor bool
|
||||
}
|
||||
|
||||
func NewEditableTextLine(initialText string) *EditableTextLine {
|
||||
return &EditableTextLine{
|
||||
text: initialText,
|
||||
visible: true,
|
||||
showCursor: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *EditableTextLine) SetSize(x, y, _, _ int) {
|
||||
p.x, p.y, p.h, p.w = x, y, 1, len(p.text)
|
||||
}
|
||||
|
||||
func (p *EditableTextLine) SetStyle(s tcell.Style) {
|
||||
p.style = s
|
||||
}
|
||||
|
||||
func (p *EditableTextLine) Draw(s tcell.Screen) {
|
||||
if !p.visible {
|
||||
return
|
||||
}
|
||||
for j, r := range p.text {
|
||||
s.SetContent(p.x+j, p.y, r, nil, p.style)
|
||||
}
|
||||
s.ShowCursor(p.x+p.cursorPos, p.y)
|
||||
}
|
||||
|
||||
func (p *EditableTextLine) SetVisible(b bool) {
|
||||
p.visible = b
|
||||
}
|
||||
|
||||
func (p *EditableTextLine) SetCursorVisible(b bool) {
|
||||
p.showCursor = b
|
||||
}
|
||||
|
||||
func (p *EditableTextLine) SetText(t string) {
|
||||
p.text = t
|
||||
if len(p.text) == 0 {
|
||||
p.ResetCursor(true)
|
||||
return
|
||||
}
|
||||
p.ResetCursor(false)
|
||||
}
|
||||
|
||||
func (p *EditableTextLine) Text() string {
|
||||
return p.text
|
||||
}
|
||||
|
||||
func (p *EditableTextLine) ResetCursor(beginning bool) {
|
||||
if beginning {
|
||||
p.cursorPos = 0
|
||||
} else {
|
||||
p.cursorPos = len(p.text)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *EditableTextLine) InsertAtCursor(r rune) {
|
||||
if len(p.text) == 0 {
|
||||
p.text = string(r)
|
||||
p.cursorPos = 1
|
||||
return
|
||||
}
|
||||
p.text = p.text[0:p.cursorPos] + string(r) + p.text[p.cursorPos:len(p.text)]
|
||||
p.cursorPos = p.cursorPos + 1
|
||||
}
|
||||
|
||||
func (p *EditableTextLine) MoveCursor(i int) {
|
||||
if p.cursorPos+i < 0 {
|
||||
p.cursorPos = 0
|
||||
return
|
||||
}
|
||||
if p.cursorPos+i > len(p.text) {
|
||||
p.cursorPos = len(p.text)
|
||||
return
|
||||
}
|
||||
p.cursorPos = p.cursorPos + i
|
||||
}
|
||||
|
||||
func (p *EditableTextLine) DeleteAtCursor() {
|
||||
if len(p.text) == 0 {
|
||||
p.cursorPos = 0
|
||||
return
|
||||
}
|
||||
p.text = p.text[0:p.cursorPos-1] + p.text[p.cursorPos:len(p.text)]
|
||||
p.cursorPos = p.cursorPos - 1
|
||||
}
|
241
ui/ui_test.go
241
ui/ui_test.go
@@ -1,241 +0,0 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
func TestContainerOneBox(t *testing.T) {
|
||||
expect := `┌─ box one ────────┐
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
└──────────────────┘
|
||||
`
|
||||
m := &MockScreen{}
|
||||
one := NewBox("box one", nil, Contents{}, tcell.Style{}, false)
|
||||
container := NewContainer(
|
||||
Contents{{Container: one}},
|
||||
LayoutHorizontalEven,
|
||||
)
|
||||
m.Init()
|
||||
m.Resize(0, 0, 5, 20)
|
||||
container.SetSize(0, 0, 5, 20)
|
||||
container.Draw(m)
|
||||
result := m.DumpContents()
|
||||
if result != expect {
|
||||
fmt.Printf("expected:\n%+v", expect)
|
||||
fmt.Printf("actual:\n%+v", result)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerTwoBoxesHStack(t *testing.T) {
|
||||
expect := `┌─ one ──┐┌─ two ──┐
|
||||
│ ││ │
|
||||
│ ││ │
|
||||
│ ││ │
|
||||
└────────┘└────────┘
|
||||
`
|
||||
m := &MockScreen{}
|
||||
one := NewBox("one", nil, Contents{}, tcell.Style{}, false)
|
||||
two := NewBox("two", nil, Contents{}, tcell.Style{}, false)
|
||||
container := NewContainer(
|
||||
Contents{{Container: one}, {Container: two}},
|
||||
LayoutHorizontalEven,
|
||||
)
|
||||
m.Init()
|
||||
m.Resize(0, 0, 5, 20)
|
||||
container.SetSize(0, 0, 5, 20)
|
||||
container.Draw(m)
|
||||
result := m.DumpContents()
|
||||
if result != expect {
|
||||
fmt.Printf("expected:\n%+v", expect)
|
||||
fmt.Printf("actual:\n%+v", result)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerThreeBoxesUnevenHStack(t *testing.T) {
|
||||
expect := `┌─ one ──┐┌─ two ──┐┌─ three
|
||||
│ ││ ││ │
|
||||
│ ││ ││ │
|
||||
│ ││ ││ │
|
||||
└────────┘└────────┘└───────┘
|
||||
`
|
||||
m := &MockScreen{}
|
||||
one := NewBox("one", nil, Contents{}, tcell.Style{}, false)
|
||||
two := NewBox("two", nil, Contents{}, tcell.Style{}, false)
|
||||
three := NewBox("three", nil, Contents{}, tcell.Style{}, false)
|
||||
container := NewContainer(
|
||||
Contents{{Container: one}, {Container: two}, {Container: three}},
|
||||
LayoutHorizontalEven,
|
||||
)
|
||||
m.Init()
|
||||
m.Resize(0, 0, 5, 29)
|
||||
container.SetSize(0, 0, 5, 29)
|
||||
container.Draw(m)
|
||||
result := m.DumpContents()
|
||||
if result != expect {
|
||||
fmt.Printf("expected:\n%+v", expect)
|
||||
fmt.Printf("actual:\n%+v", result)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerTwoBoxesHPercentStack(t *testing.T) {
|
||||
expect := `┌─ one ──────┐┌─ two ┐
|
||||
│ ││ │
|
||||
│ ││ │
|
||||
│ ││ │
|
||||
└────────────┘└──────┘
|
||||
`
|
||||
m := &MockScreen{}
|
||||
one := NewBox("one", nil, Contents{}, tcell.Style{}, false)
|
||||
two := NewBox("two", nil, Contents{}, tcell.Style{}, false)
|
||||
container := NewContainer(
|
||||
Contents{
|
||||
{Container: one, Offsets: Offsets{Percent: 2}},
|
||||
{Container: two, Offsets: Offsets{Percent: 1}}},
|
||||
LayoutHorizontalPercent,
|
||||
)
|
||||
m.Init()
|
||||
m.Resize(0, 0, 5, 22)
|
||||
container.SetSize(0, 0, 5, 22)
|
||||
container.Draw(m)
|
||||
result := m.DumpContents()
|
||||
if result != expect {
|
||||
fmt.Printf("expected:\n%+v", expect)
|
||||
fmt.Printf("actual:\n%+v", result)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerTwoBoxesVStack(t *testing.T) {
|
||||
expect := `┌─ one ──┐
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
└────────┘
|
||||
┌─ two ──┐
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
└────────┘
|
||||
`
|
||||
m := &MockScreen{}
|
||||
one := NewBox("one", nil, Contents{}, tcell.Style{}, false)
|
||||
two := NewBox("two", nil, Contents{}, tcell.Style{}, false)
|
||||
container := NewContainer(
|
||||
Contents{{Container: one}, {Container: two}},
|
||||
LayoutVerticalEven,
|
||||
)
|
||||
m.Init()
|
||||
m.Resize(0, 0, 10, 10)
|
||||
container.SetSize(0, 0, 10, 10)
|
||||
container.Draw(m)
|
||||
result := m.DumpContents()
|
||||
if result != expect {
|
||||
fmt.Printf("expected:\n%+v", expect)
|
||||
fmt.Printf("actual:\n%+v", result)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerTwoBoxesPercentageVStack(t *testing.T) {
|
||||
expect := `┌─ one ──┐
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
└────────┘
|
||||
┌─ two ──┐
|
||||
│ │
|
||||
│ │
|
||||
└────────┘
|
||||
`
|
||||
m := &MockScreen{}
|
||||
one := NewBox("one", nil, Contents{}, tcell.Style{}, false)
|
||||
two := NewBox("two", nil, Contents{}, tcell.Style{}, false)
|
||||
container := NewContainer(
|
||||
Contents{
|
||||
{Container: one, Offsets: Offsets{Percent: 2}},
|
||||
{Container: two, Offsets: Offsets{Percent: 1}}},
|
||||
LayoutVerticalPercent,
|
||||
)
|
||||
m.Init()
|
||||
m.Resize(0, 0, 10, 10)
|
||||
container.SetSize(0, 0, 10, 10)
|
||||
container.Draw(m)
|
||||
result := m.DumpContents()
|
||||
if result != expect {
|
||||
fmt.Printf("expected:\n%+v", expect)
|
||||
fmt.Printf("actual:\n%+v", result)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewEditableTextLine(t *testing.T) {
|
||||
e := NewEditableTextLine("")
|
||||
e.InsertAtCursor('a')
|
||||
e.InsertAtCursor('b')
|
||||
e.InsertAtCursor('c')
|
||||
if e.text != "abc" {
|
||||
fmt.Printf("expected: 'abc', actual: '%+v'", e.text)
|
||||
t.Fail()
|
||||
}
|
||||
e.MoveCursor(-1)
|
||||
e.InsertAtCursor('d')
|
||||
if e.text != "abdc" {
|
||||
fmt.Printf("expected: 'abdc', actual: '%+v'", e.text)
|
||||
t.Fail()
|
||||
}
|
||||
e.MoveCursor(-20)
|
||||
e.InsertAtCursor('e')
|
||||
if e.text != "eabdc" {
|
||||
fmt.Printf("expected: 'eabdc', actual: '%+v'", e.text)
|
||||
t.Fail()
|
||||
}
|
||||
e.MoveCursor(20)
|
||||
e.InsertAtCursor('f')
|
||||
if e.text != "eabdcf" {
|
||||
fmt.Printf("expected: 'eabdcf', actual: '%+v'", e.text)
|
||||
t.Fail()
|
||||
}
|
||||
e.MoveCursor(1)
|
||||
e.InsertAtCursor('g')
|
||||
if e.text != "eabdcfg" {
|
||||
fmt.Printf("expected: 'eabdcfg', actual: '%+v'", e.text)
|
||||
t.Fail()
|
||||
}
|
||||
e.DeleteAtCursor()
|
||||
e.DeleteAtCursor()
|
||||
e.MoveCursor(-1)
|
||||
e.DeleteAtCursor()
|
||||
if e.text != "eabc" {
|
||||
fmt.Printf("expected: 'eabc', actual: '%+v'", e.text)
|
||||
t.Fail()
|
||||
}
|
||||
e.ResetCursor(false)
|
||||
e.InsertAtCursor('h')
|
||||
e.ResetCursor(true)
|
||||
e.InsertAtCursor('g')
|
||||
if e.text != "geabch" {
|
||||
fmt.Printf("expected: 'geabch', actual: '%+v'", e.text)
|
||||
t.Fail()
|
||||
}
|
||||
e.SetText("the rain in spain")
|
||||
e.InsertAtCursor('s')
|
||||
if e.text != "the rain in spains" {
|
||||
fmt.Printf("expected: 'the rain in spains', actual: '%+v'", e.text)
|
||||
t.Fail()
|
||||
}
|
||||
e.SetText("")
|
||||
e.InsertAtCursor('s')
|
||||
if e.text != "s" {
|
||||
fmt.Printf("expected: 's', actual: '%+v'", e.text)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user