gocui/gui.go

459 lines
11 KiB
Go
Raw Normal View History

2014-01-14 20:11:12 +01:00
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2013-12-27 21:36:26 +01:00
package gocui
import (
"errors"
2013-12-27 22:02:56 +01:00
2013-12-31 21:23:28 +01:00
"github.com/nsf/termbox-go"
2013-12-27 21:36:26 +01:00
)
var (
2014-01-19 17:03:52 +01:00
// ErrorQuit is used to decide if the MainLoop finished succesfully.
ErrorQuit error = errors.New("quit")
// ErrorUnkView allows to assert if a View must be initialized.
2014-01-10 20:25:37 +01:00
ErrorUnkView error = errors.New("unknown view")
)
2013-12-27 21:36:26 +01:00
2014-01-19 17:03:52 +01:00
// Gui represents the whole User Interface, including the views, layouts
// and keybindings.
2013-12-27 21:36:26 +01:00
type Gui struct {
2014-01-16 23:01:53 +01:00
events chan termbox.Event
views []*View
currentView *View
layout func(*Gui) error
keybindings []*keybinding
maxX, maxY int
// BgColor and FgColor allow to configure the background and foreground
// colors of the GUI.
BgColor, FgColor Attribute
// SelBgColor and SelFgColor are used to configure the background and
// foreground colors of the selected line, when it is highlighted.
2014-01-10 12:38:08 +01:00
SelBgColor, SelFgColor Attribute
// If ShowCursor is true then the cursor is enabled.
ShowCursor bool
2013-12-27 21:36:26 +01:00
}
2014-01-19 17:03:52 +01:00
// NewGui returns a new Gui object.
func NewGui() *Gui {
2013-12-27 21:36:26 +01:00
return &Gui{}
}
2014-01-19 17:03:52 +01:00
// Init initializes the library. This function must be called before
// any other functions.
func (g *Gui) Init() error {
if err := termbox.Init(); err != nil {
2013-12-31 21:23:28 +01:00
return err
}
2013-12-27 21:36:26 +01:00
g.events = make(chan termbox.Event, 20)
2013-12-31 21:23:28 +01:00
g.maxX, g.maxY = termbox.Size()
2014-01-10 12:38:08 +01:00
g.BgColor = ColorBlack
g.FgColor = ColorWhite
2013-12-31 21:23:28 +01:00
return nil
2013-12-27 21:36:26 +01:00
}
2014-01-19 17:03:52 +01:00
// Close finalizes the library. It should be called after a successful
// initialization and when gocui is not needed anymore.
2013-12-27 21:36:26 +01:00
func (g *Gui) Close() {
termbox.Close()
}
2014-01-19 17:03:52 +01:00
// Size returns the terminal's size.
2013-12-27 21:36:26 +01:00
func (g *Gui) Size() (x, y int) {
2013-12-31 21:23:28 +01:00
return g.maxX, g.maxY
2013-12-27 21:36:26 +01:00
}
2014-01-19 17:03:52 +01:00
// SetRune writes a rune at the given point, relative to the top-left
// corner of the terminal. It checks if the position is valid and applies
// the gui's colors.
func (g *Gui) SetRune(x, y int, ch rune) error {
2014-01-02 18:34:58 +01:00
if x < 0 || y < 0 || x >= g.maxX || y >= g.maxY {
2014-01-04 02:50:49 +01:00
return errors.New("invalid point")
2014-01-02 18:34:58 +01:00
}
2014-01-09 21:55:23 +01:00
termbox.SetCell(x, y, ch, termbox.Attribute(g.FgColor), termbox.Attribute(g.BgColor))
2014-01-02 18:34:58 +01:00
return nil
}
2014-01-19 17:03:52 +01:00
// Rune returns the rune contained in the cell at the given position.
// It checks if the position is valid.
2014-01-16 23:01:53 +01:00
func (g *Gui) Rune(x, y int) (rune, error) {
2014-01-02 18:34:58 +01:00
if x < 0 || y < 0 || x >= g.maxX || y >= g.maxY {
2014-01-20 23:28:08 +01:00
return ' ', errors.New("invalid point")
2014-01-02 18:34:58 +01:00
}
c := termbox.CellBuffer()[y*g.maxX+x]
return c.Ch, nil
}
2014-01-19 17:03:52 +01:00
// SetView creates a new view with its top-left corner at (x0, y0)
// and the bottom-right one at (x1, y1). If a view with the same name
// already exists, its dimensions are updated; otherwise, the error
// ErrorUnkView is returned, which allows to assert if the View must
// be initialized. It checks if the position is valid.
func (g *Gui) SetView(name string, x0, y0, x1, y1 int) (*View, error) {
2014-01-04 02:50:49 +01:00
if x0 >= x1 || y0 >= y1 {
return nil, errors.New("invalid dimensions")
2013-12-27 21:36:26 +01:00
}
if name == "" {
return nil, errors.New("invalid name")
}
2014-01-16 23:01:53 +01:00
if v := g.View(name); v != nil {
v.x0 = x0
v.y0 = y0
v.x1 = x1
v.y1 = y1
2014-01-04 02:50:49 +01:00
return v, nil
}
v := newView(name, x0, y0, x1, y1)
2014-05-03 15:20:46 +02:00
v.BgColor, v.FgColor = g.BgColor, g.FgColor
v.SelBgColor, v.SelFgColor = g.SelBgColor, g.SelFgColor
2013-12-27 21:36:26 +01:00
g.views = append(g.views, v)
return v, ErrorUnkView
2013-12-27 21:36:26 +01:00
}
2014-01-19 17:03:52 +01:00
// View returns a pointer to the view with the given name, or nil if
// a view with that name does not exist.
2014-01-16 23:01:53 +01:00
func (g *Gui) View(name string) *View {
2014-01-04 03:40:45 +01:00
for _, v := range g.views {
2014-01-16 23:01:53 +01:00
if v.name == name {
2014-01-04 02:50:49 +01:00
return v
}
}
return nil
}
2014-01-19 17:03:52 +01:00
// DeleteView deletes a view by name.
func (g *Gui) DeleteView(name string) error {
2014-01-04 03:40:45 +01:00
for i, v := range g.views {
2014-01-16 23:01:53 +01:00
if v.name == name {
2014-01-04 03:40:45 +01:00
g.views = append(g.views[:i], g.views[i+1:]...)
return nil
}
}
2014-01-10 20:25:37 +01:00
return ErrorUnkView
}
2014-01-19 17:03:52 +01:00
// SetCurrentView gives the focus to a given view.
func (g *Gui) SetCurrentView(name string) error {
for _, v := range g.views {
2014-01-16 23:01:53 +01:00
if v.name == name {
g.currentView = v
return nil
}
}
2014-01-10 20:25:37 +01:00
return ErrorUnkView
}
// CurrentView returns the currently focused view, or nil if no view
// owns the focus.
func (g *Gui) CurrentView() *View {
return g.currentView
}
2014-01-19 17:03:52 +01:00
// SetKeybinding creates a new keybinding. If viewname equals to ""
// (empty string) then the keybinding will apply to all views. key must
// be a rune or a Key.
func (g *Gui) SetKeybinding(viewname string, key interface{}, mod Modifier, h KeybindingHandler) error {
2014-01-11 20:29:16 +01:00
var kb *keybinding
switch k := key.(type) {
case Key:
2014-01-19 17:03:52 +01:00
kb = newKeybinding(viewname, k, 0, mod, h)
case rune:
2014-01-19 17:03:52 +01:00
kb = newKeybinding(viewname, 0, k, mod, h)
default:
return errors.New("unknown type")
}
g.keybindings = append(g.keybindings, kb)
return nil
2014-01-04 03:40:45 +01:00
}
2014-01-19 17:03:52 +01:00
// SetLayout sets the current layout. A layout is a function that
// will be called everytime the gui is re-drawed, it must contain
// the base views and its initializations.
func (g *Gui) SetLayout(layout func(*Gui) error) {
g.layout = layout
g.currentView = nil
g.views = nil
go func() { g.events <- termbox.Event{Type: termbox.EventResize} }()
}
2014-01-19 17:03:52 +01:00
// MainLoop runs the main loop until an error is returned. A successful
// finish should return ErrorQuit.
func (g *Gui) MainLoop() error {
2013-12-27 21:36:26 +01:00
go func() {
for {
g.events <- termbox.PollEvent()
}
}()
termbox.SetInputMode(termbox.InputAlt)
2014-01-18 13:47:08 +01:00
if err := g.Flush(); err != nil {
2013-12-27 21:36:26 +01:00
return err
}
for {
2014-01-10 12:38:08 +01:00
ev := <-g.events
if err := g.handleEvent(&ev); err != nil {
return err
}
if err := g.consumeevents(); err != nil {
return err
}
2014-01-18 13:47:08 +01:00
if err := g.Flush(); err != nil {
2014-01-10 12:38:08 +01:00
return err
2013-12-27 21:36:26 +01:00
}
}
return nil
}
2014-01-19 17:03:52 +01:00
// consumeevents handles the remaining events in the events pool.
func (g *Gui) consumeevents() error {
2013-12-27 21:36:26 +01:00
for {
select {
case ev := <-g.events:
if err := g.handleEvent(&ev); err != nil {
return err
}
default:
return nil
}
}
}
2014-01-19 17:03:52 +01:00
// handleEvent handles an event, based on its type (key-press, error,
// etc.)
func (g *Gui) handleEvent(ev *termbox.Event) error {
2013-12-27 21:36:26 +01:00
switch ev.Type {
case termbox.EventKey:
return g.onKey(ev)
case termbox.EventError:
return ev.Err
default:
return nil
}
}
2014-01-19 17:03:52 +01:00
// Flush updates the gui, re-drawing frames and buffers.
2014-01-18 13:47:08 +01:00
func (g *Gui) Flush() error {
if g.layout == nil {
2014-01-04 02:50:49 +01:00
return errors.New("Null layout")
}
2014-01-10 12:38:08 +01:00
termbox.Clear(termbox.Attribute(g.FgColor), termbox.Attribute(g.BgColor))
2014-01-04 02:50:49 +01:00
g.maxX, g.maxY = termbox.Size()
if err := g.layout(g); err != nil {
return err
}
2014-01-18 13:47:08 +01:00
for _, v := range g.views {
2014-10-18 15:47:24 +02:00
if v.Frame {
2014-10-17 17:22:28 -04:00
if err := g.drawFrame(v); err != nil {
return err
}
2014-01-18 13:47:08 +01:00
}
2014-10-17 17:22:28 -04:00
2014-01-18 13:47:08 +01:00
if err := g.draw(v); err != nil {
return err
}
}
2013-12-30 14:19:22 +01:00
if err := g.drawIntersections(); err != nil {
return err
}
2014-01-18 13:47:08 +01:00
termbox.Flush()
return nil
}
2014-01-19 17:03:52 +01:00
// drawFrame draws the horizontal and vertical edges of a view.
2014-01-18 13:47:08 +01:00
func (g *Gui) drawFrame(v *View) error {
for x := v.x0 + 1; x < v.x1 && x < g.maxX; x++ {
if x < 0 {
continue
}
if v.y0 > -1 && v.y0 < g.maxY {
if err := g.SetRune(x, v.y0, '─'); err != nil {
return err
}
}
2014-01-18 13:47:08 +01:00
if v.y1 > -1 && v.y1 < g.maxY {
if err := g.SetRune(x, v.y1, '─'); err != nil {
return err
}
2014-01-18 13:47:08 +01:00
}
}
for y := v.y0 + 1; y < v.y1 && y < g.maxY; y++ {
if y < 0 {
continue
}
if v.x0 > -1 && v.x0 < g.maxX {
if err := g.SetRune(v.x0, y, '│'); err != nil {
return err
2014-01-02 18:34:58 +01:00
}
2014-01-18 13:47:08 +01:00
}
if v.x1 > -1 && v.x1 < g.maxX {
if err := g.SetRune(v.x1, y, '│'); err != nil {
return err
}
2013-12-27 21:36:26 +01:00
}
}
return nil
}
2014-01-19 17:03:52 +01:00
// draw manages the cursor and calls the draw function of a view.
func (g *Gui) draw(v *View) error {
if g.ShowCursor {
if v := g.currentView; v != nil {
maxX, maxY := v.Size()
cx, cy := v.cx, v.cy
2014-01-19 17:03:52 +01:00
if v.cx >= maxX {
cx = maxX - 1
}
if v.cy >= maxY {
cy = maxY - 1
}
if err := v.SetCursor(cx, cy); err != nil {
return err
2014-01-19 17:03:52 +01:00
}
termbox.SetCursor(v.x0+v.cx+1, v.y0+v.cy+1)
}
} else {
termbox.HideCursor()
}
v.clearRunes()
if err := v.draw(); err != nil {
return err
}
return nil
}
// drawIntersections draws the corners of each view, based on the type
// of the edges that converge at these points.
func (g *Gui) drawIntersections() error {
for _, v := range g.views {
2014-01-16 23:01:53 +01:00
if ch, ok := g.intersectionRune(v.x0, v.y0); ok {
if err := g.SetRune(v.x0, v.y0, ch); err != nil {
2014-01-02 18:34:58 +01:00
return err
}
}
2014-01-16 23:01:53 +01:00
if ch, ok := g.intersectionRune(v.x0, v.y1); ok {
if err := g.SetRune(v.x0, v.y1, ch); err != nil {
2014-01-02 18:34:58 +01:00
return err
}
}
2014-01-16 23:01:53 +01:00
if ch, ok := g.intersectionRune(v.x1, v.y0); ok {
if err := g.SetRune(v.x1, v.y0, ch); err != nil {
2014-01-02 18:34:58 +01:00
return err
}
}
2014-01-16 23:01:53 +01:00
if ch, ok := g.intersectionRune(v.x1, v.y1); ok {
if err := g.SetRune(v.x1, v.y1, ch); err != nil {
2014-01-02 18:34:58 +01:00
return err
}
}
}
return nil
}
2014-01-19 17:03:52 +01:00
// intersectionRune returns the correct intersection rune at a given
// point.
2014-01-16 23:01:53 +01:00
func (g *Gui) intersectionRune(x, y int) (rune, bool) {
2013-12-31 21:23:28 +01:00
if x < 0 || y < 0 || x >= g.maxX || y >= g.maxY {
2014-01-20 23:28:08 +01:00
return ' ', false
2013-12-30 14:19:22 +01:00
}
2014-01-16 23:01:53 +01:00
chTop, _ := g.Rune(x, y-1)
2013-12-31 01:37:34 +01:00
top := verticalRune(chTop)
2014-01-16 23:01:53 +01:00
chBottom, _ := g.Rune(x, y+1)
2013-12-31 01:37:34 +01:00
bottom := verticalRune(chBottom)
2014-01-16 23:01:53 +01:00
chLeft, _ := g.Rune(x-1, y)
2013-12-31 01:37:34 +01:00
left := horizontalRune(chLeft)
2014-01-16 23:01:53 +01:00
chRight, _ := g.Rune(x+1, y)
2013-12-31 01:37:34 +01:00
right := horizontalRune(chRight)
2013-12-30 14:19:22 +01:00
var ch rune
2013-12-30 14:19:22 +01:00
switch {
2013-12-31 01:37:34 +01:00
case !top && bottom && !left && right:
2013-12-30 14:45:02 +01:00
ch = '┌'
2013-12-31 01:37:34 +01:00
case !top && bottom && left && !right:
2013-12-30 14:45:02 +01:00
ch = '┐'
2013-12-31 01:37:34 +01:00
case top && !bottom && !left && right:
2013-12-30 14:45:02 +01:00
ch = '└'
2013-12-31 01:37:34 +01:00
case top && !bottom && left && !right:
2013-12-30 14:45:02 +01:00
ch = '┘'
2013-12-31 01:37:34 +01:00
case top && bottom && left && right:
2013-12-30 14:45:02 +01:00
ch = '┼'
2013-12-31 01:37:34 +01:00
case top && bottom && !left && right:
2013-12-30 14:45:02 +01:00
ch = '├'
2013-12-31 01:37:34 +01:00
case top && bottom && left && !right:
2013-12-30 14:45:02 +01:00
ch = '┤'
2013-12-31 01:37:34 +01:00
case !top && bottom && left && right:
2013-12-30 14:45:02 +01:00
ch = '┬'
2013-12-31 01:37:34 +01:00
case top && !bottom && left && right:
2013-12-30 14:45:02 +01:00
ch = '┴'
2013-12-30 14:19:22 +01:00
default:
2014-01-20 23:28:08 +01:00
return ' ', false
2013-12-30 14:19:22 +01:00
}
return ch, true
}
2014-01-19 17:03:52 +01:00
// verticalRune returns if the given character is a vertical rune.
2013-12-30 14:37:27 +01:00
func verticalRune(ch rune) bool {
2013-12-30 14:45:02 +01:00
if ch == '│' || ch == '┼' || ch == '├' || ch == '┤' {
2013-12-30 14:37:27 +01:00
return true
}
return false
}
2014-01-19 17:03:52 +01:00
// verticalRune returns if the given character is a horizontal rune.
2013-12-30 14:37:27 +01:00
func horizontalRune(ch rune) bool {
2013-12-30 14:45:02 +01:00
if ch == '─' || ch == '┼' || ch == '┬' || ch == '┴' {
2013-12-30 14:37:27 +01:00
return true
}
return false
}
2014-01-19 17:03:52 +01:00
// onKey manages key-press events. A keybinding handler is called when
2014-01-20 23:10:16 +01:00
// a key-press event satisfies a configured keybinding. Furthermore,
// currentView's internal buffer is modified if currentView.Editable is true.
func (g *Gui) onKey(ev *termbox.Event) error {
if g.currentView != nil && g.currentView.Editable {
if err := g.handleEdit(g.currentView, ev); err != nil {
return err
}
}
for _, kb := range g.keybindings {
2014-01-22 23:44:08 +01:00
if kb.h != nil && ev.Ch == kb.ch && Key(ev.Key) == kb.key && Modifier(ev.Mod) == kb.mod &&
2014-01-19 17:03:52 +01:00
(kb.viewName == "" || (g.currentView != nil && kb.viewName == g.currentView.name)) {
return kb.h(g, g.currentView)
}
2013-12-27 21:36:26 +01:00
}
return nil
2013-12-27 21:36:26 +01:00
}
2014-01-22 23:44:08 +01:00
// handleEdit manages the edition mode.
func (g *Gui) handleEdit(v *View, ev *termbox.Event) error {
switch {
case ev.Ch != 0 && ev.Mod == 0:
2014-01-22 23:44:08 +01:00
return v.editWrite(ev.Ch)
case ev.Key == termbox.KeySpace:
2014-01-22 23:44:08 +01:00
return v.editWrite(' ')
2014-01-22 22:37:53 +01:00
case ev.Key == termbox.KeyBackspace || ev.Key == termbox.KeyBackspace2:
2014-01-22 23:44:08 +01:00
return v.editDelete(true)
2014-01-22 22:37:53 +01:00
case ev.Key == termbox.KeyDelete:
2014-01-22 23:44:08 +01:00
return v.editDelete(false)
2014-01-22 22:37:53 +01:00
case ev.Key == termbox.KeyInsert:
v.overwrite = !v.overwrite
case ev.Key == termbox.KeyEnter:
2014-01-22 23:44:08 +01:00
return v.editLine()
}
return nil
}