From 80e891a8024ac10a60daa790131e04f0326b0c73 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Fri, 1 Jun 2018 09:58:00 +0200 Subject: switch to tcell from termbox This is a simple mostly straight forward switch to tcell in favor of termbox. It uses the tcell native api (not the compat layer) but does not make use of most features. Further changes should include moving to tcell's views.TextArea and the general built in widget behaviour instead of the current ad hoc implementation. Regression: Cursor isn't shown in ex-line --- widgets/aerc.go | 24 +++++++++++------------- widgets/exline.go | 49 +++++++++++++++++++++---------------------------- widgets/status.go | 30 +++++++++++++----------------- 3 files changed, 45 insertions(+), 58 deletions(-) (limited to 'widgets') diff --git a/widgets/aerc.go b/widgets/aerc.go index 5563275..19ddfdd 100644 --- a/widgets/aerc.go +++ b/widgets/aerc.go @@ -5,7 +5,7 @@ import ( "log" "time" - tb "github.com/nsf/termbox-go" + "github.com/gdamore/tcell" libui "git.sr.ht/~sircmpwn/aerc2/lib/ui" ) @@ -35,7 +35,7 @@ func NewAerc(logger *log.Logger) *Aerc { // TODO: move sidebar into tab content, probably grid.AddChild(libui.NewText("aerc"). Strategy(libui.TEXT_CENTER). - Color(tb.ColorBlack, tb.ColorWhite)) + Color(tcell.ColorBlack, tcell.ColorWhite)) // sidebar placeholder: grid.AddChild(libui.NewBordered( libui.NewFill('.'), libui.BORDER_RIGHT)).At(1, 0).Span(2, 1) @@ -75,10 +75,10 @@ func (aerc *Aerc) Draw(ctx *libui.Context) { aerc.grid.Draw(ctx) } -func (aerc *Aerc) Event(event tb.Event) bool { - switch event.Type { - case tb.EventKey: - if event.Ch == ':' { +func (aerc *Aerc) Event(event tcell.Event) bool { + switch event := event.(type) { + case *tcell.EventKey: + if event.Rune() == ':' { exline := NewExLine(func(command string) { aerc.statusline.Push(fmt.Sprintf("TODO: execute %s", command), 3 * time.Second) @@ -92,12 +92,10 @@ func (aerc *Aerc) Event(event tb.Event) bool { aerc.statusbar.Push(exline) return true } - fallthrough - default: - if aerc.interactive != nil { - return aerc.interactive.Event(event) - } else { - return false - } + } + if aerc.interactive != nil { + return aerc.interactive.Event(event) + } else { + return false } } diff --git a/widgets/exline.go b/widgets/exline.go index 0522371..de652ba 100644 --- a/widgets/exline.go +++ b/widgets/exline.go @@ -2,7 +2,7 @@ package widgets import ( "github.com/mattn/go-runewidth" - tb "github.com/nsf/termbox-go" + "github.com/gdamore/tcell" "git.sr.ht/~sircmpwn/aerc2/lib/ui" ) @@ -40,15 +40,10 @@ func (ex *ExLine) Invalidate() { } func (ex *ExLine) Draw(ctx *ui.Context) { - cell := tb.Cell{ - Fg: tb.ColorDefault, - Bg: tb.ColorDefault, - Ch: ' ', - } - ctx.Fill(0, 0, ctx.Width(), ctx.Height(), cell) - ctx.Printf(0, 0, cell, ":%s", string(ex.command)) + ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault) + ctx.Printf(0, 0, tcell.StyleDefault, ":%s", string(ex.command)) cells := runewidth.StringWidth(string(ex.command[:ex.index])) - tb.SetCursor(ctx.X()+cells+1, ctx.Y()) + ctx.SetCursor(cells + 1, 0) } func (ex *ExLine) insert(ch rune) { @@ -93,43 +88,41 @@ func (ex *ExLine) backspace() { } } -func (ex *ExLine) Event(event tb.Event) bool { - switch event.Type { - case tb.EventKey: - switch event.Key { - case tb.KeySpace: - ex.insert(' ') - case tb.KeyBackspace, tb.KeyBackspace2: +func (ex *ExLine) Event(event tcell.Event) bool { + switch event := event.(type) { + case *tcell.EventKey: + switch event.Key() { + case tcell.KeyBackspace, tcell.KeyBackspace2: ex.backspace() - case tb.KeyCtrlD, tb.KeyDelete: + case tcell.KeyCtrlD, tcell.KeyDelete: ex.deleteChar() - case tb.KeyCtrlB, tb.KeyArrowLeft: + case tcell.KeyCtrlB, tcell.KeyLeft: if ex.index > 0 { ex.index-- ex.Invalidate() } - case tb.KeyCtrlF, tb.KeyArrowRight: + case tcell.KeyCtrlF, tcell.KeyRight: if ex.index < len(ex.command) { ex.index++ ex.Invalidate() } - case tb.KeyCtrlA, tb.KeyHome: + case tcell.KeyCtrlA, tcell.KeyHome: ex.index = 0 ex.Invalidate() - case tb.KeyCtrlE, tb.KeyEnd: + case tcell.KeyCtrlE, tcell.KeyEnd: ex.index = len(ex.command) ex.Invalidate() - case tb.KeyCtrlW: + case tcell.KeyCtrlW: ex.deleteWord() - case tb.KeyEnter: - tb.HideCursor() + case tcell.KeyEnter: + //ex.ctx.Screen().HideCursor() ex.commit(string(ex.command)) - case tb.KeyEsc, tb.KeyCtrlC: - tb.HideCursor() + case tcell.KeyEsc, tcell.KeyCtrlC: + //ex.ctx.Screen().HideCursor() ex.cancel() default: - if event.Ch != 0 { - ex.insert(event.Ch) + if event.Rune() != 0 { + ex.insert(event.Rune()) } } } diff --git a/widgets/status.go b/widgets/status.go index bb87d33..3b4dbcc 100644 --- a/widgets/status.go +++ b/widgets/status.go @@ -3,7 +3,7 @@ package widgets import ( "time" - tb "github.com/nsf/termbox-go" + "github.com/gdamore/tcell" "git.sr.ht/~sircmpwn/aerc2/lib/ui" ) @@ -16,16 +16,16 @@ type StatusLine struct { } type StatusMessage struct { - bg tb.Attribute - fg tb.Attribute + bg tcell.Color + fg tcell.Color message string } func NewStatusLine() *StatusLine { return &StatusLine{ fallback: StatusMessage{ - bg: tb.ColorWhite, - fg: tb.ColorBlack, + bg: tcell.ColorWhite, + fg: tcell.ColorBlack, message: "Idle", }, } @@ -46,19 +46,15 @@ func (status *StatusLine) Draw(ctx *ui.Context) { if len(status.stack) != 0 { line = status.stack[len(status.stack)-1] } - cell := tb.Cell{ - Fg: line.fg, - Bg: line.bg, - Ch: ' ', - } - ctx.Fill(0, 0, ctx.Width(), ctx.Height(), cell) - ctx.Printf(0, 0, cell, "%s", line.message) + style := tcell.StyleDefault.Background(line.bg).Foreground(line.fg) + ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', style) + ctx.Printf(0, 0, style, "%s", line.message) } func (status *StatusLine) Set(text string) *StatusMessage { status.fallback = StatusMessage{ - bg: tb.ColorWhite, - fg: tb.ColorBlack, + bg: tcell.ColorWhite, + fg: tcell.ColorBlack, message: text, } status.Invalidate() @@ -67,8 +63,8 @@ func (status *StatusLine) Set(text string) *StatusMessage { func (status *StatusLine) Push(text string, expiry time.Duration) *StatusMessage { msg := &StatusMessage{ - bg: tb.ColorWhite, - fg: tb.ColorBlack, + bg: tcell.ColorWhite, + fg: tcell.ColorBlack, message: text, } status.stack = append(status.stack, msg) @@ -85,7 +81,7 @@ func (status *StatusLine) Push(text string, expiry time.Duration) *StatusMessage return msg } -func (msg *StatusMessage) Color(bg tb.Attribute, fg tb.Attribute) { +func (msg *StatusMessage) Color(bg tcell.Color, fg tcell.Color) { msg.bg = bg msg.fg = fg } -- cgit v1.2.3