Skip to content

Commit

Permalink
Merge branch 'dev' : v0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebrl committed May 24, 2016
2 parents 8c49240 + 67252e8 commit 9b84ba0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 8 deletions.
8 changes: 6 additions & 2 deletions edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func simpleEditor(v *View, key Key, ch rune, mod Modifier) {
v.EditDelete(false)
case key == KeyInsert:
v.Overwrite = !v.Overwrite
case key == KeyEnter:
v.EditNewLine()
//case key == KeyEnter:
//v.EditNewLine()
case key == KeyArrowDown:
v.MoveCursor(0, 1, false)
case key == KeyArrowUp:
Expand Down Expand Up @@ -124,6 +124,10 @@ func (v *View) MoveCursor(dx, dy int, writeMode bool) {
maxX, maxY := v.Size()
cx, cy := v.cx+dx, v.cy+dy
x, y := v.ox+cx, v.oy+cy
if y > len(v.lines) {
cy -= y - len(v.lines)
y -= y - len(v.lines)
}

var curLineWidth, prevLineWidth int
// get the width of the current line
Expand Down
61 changes: 55 additions & 6 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var (

// ErrUnknownView allows to assert if a View must be initialized.
ErrUnknownView = errors.New("unknown view")

// ErrUnknowMode checks map initialization
ErrUnknowMode = errors.New("unknown mode")
)

// Gui represents the whole User Interface, including the views, layouts
Expand All @@ -34,7 +37,8 @@ type Gui struct {
views []*View
currentView *View
layout Handler
keybindings []*keybinding
modes []*Mode
currentMode *Mode
maxX, maxY int

// BgColor and FgColor allow to configure the background and foreground
Expand Down Expand Up @@ -74,9 +78,49 @@ func (g *Gui) Init() error {
g.BgColor = ColorBlack
g.FgColor = ColorWhite
g.Editor = DefaultEditor

return nil
}

// SetCurrentMode switches to the Mode with the given name
func (g *Gui) SetCurrentMode(name string) error {
for _, m := range g.modes {
if m.name == name {
g.currentMode = m
return nil
}
}
return ErrUnknowMode
}

// CurrentMode returns the current mode
func (g *Gui) CurrentMode() *Mode {
return g.currentMode
}

// Mode returns a pointer to the Mode with the given name, or error
// ErrUnknownMode if a Mode with that name does not exist.
func (g *Gui) Mode(name string) (*Mode, error) {
for _, m := range g.modes {
if m.name == name {
g.currentMode = m
return m, nil
}
}
return nil, ErrUnknowMode
}

// SetMode creates a new mode
// does nothing if there is already a mode for this name
func (g *Gui) SetMode(name string) {
if _, err := g.Mode(name); err == nil {
return
}

m := CreateMode(name)
g.modes = append(g.modes, m)
}

// Close finalizes the library. It should be called after a successful
// initialization and when gocui is not needed anymore.
func (g *Gui) Close() {
Expand Down Expand Up @@ -214,18 +258,21 @@ func (g *Gui) CurrentView() *View {
// 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 {
func (g *Gui) SetKeybinding(modeName string, viewName string, key interface{}, mod Modifier, h KeybindingHandler) error {
var kb *keybinding

switch k := key.(type) {
case Key:
kb = newKeybinding(viewname, k, 0, mod, h)
kb = newKeybinding(viewName, k, 0, mod, h)
case rune:
kb = newKeybinding(viewname, 0, k, mod, h)
kb = newKeybinding(viewName, 0, k, mod, h)
default:
return errors.New("unknown type")
}
g.keybindings = append(g.keybindings, kb)

if m, err := g.Mode(modeName); err == nil {
*m.GetKeyBindings() = append(*m.GetKeyBindings(), kb)
}
return nil
}

Expand Down Expand Up @@ -266,6 +313,7 @@ func (g *Gui) MainLoop() error {
return err
}
for {

select {
case ev := <-g.tbEvents:
if err := g.handleEvent(&ev); err != nil {
Expand All @@ -276,6 +324,7 @@ func (g *Gui) MainLoop() error {
return err
}
}

if err := g.consumeevents(); err != nil {
return err
}
Expand Down Expand Up @@ -560,7 +609,7 @@ func (g *Gui) onKey(ev *termbox.Event) error {
curView = v
}

for _, kb := range g.keybindings {
for _, kb := range g.currentMode.keybindings {
if kb.h == nil {
continue
}
Expand Down
3 changes: 3 additions & 0 deletions keybinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ type keybinding struct {
h KeybindingHandler
}

// kbSet is a set of keybindings representing a mode
type kbSet []*keybinding

// newKeybinding returns a new Keybinding object.
func newKeybinding(viewname string, key Key, ch rune, mod Modifier, h KeybindingHandler) (kb *keybinding) {
kb = &keybinding{
Expand Down
18 changes: 18 additions & 0 deletions mode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package gocui

type Mode struct {
name string
keybindings kbSet
}

func CreateMode(name string) *Mode {
return &Mode{name: name}
}

func (m *Mode) GetKeyBindings() *kbSet {
return &m.keybindings
}

func (m *Mode) Name() string {
return m.name
}
5 changes: 5 additions & 0 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func (v *View) Name() string {
return v.name
}

// Size returns the number of lines contained in the buffer
func (v *View) BufferSize() int {
return len(v.lines)
}

// setRune writes a rune at the given point, relative to the view. It
// checks if the position is valid and applies the view's colors, taking
// into account if the cell must be highlighted.
Expand Down

0 comments on commit 9b84ba0

Please sign in to comment.