Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose lines in view buffer, generate key descriptions #148

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module "github.com/jroimartin/gocui"
103 changes: 103 additions & 0 deletions keybinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,106 @@ const (
ModNone Modifier = Modifier(0)
ModAlt = Modifier(termbox.ModAlt)
)

// DescribeKey generates a human-readable description of a key combo.
func DescribeKey(key interface{}, mod Modifier) string {

var k string

switch mod {
case ModNone:
case ModAlt:
k = "Alt+"
default:
k = "<unknown modifier>+"
}

switch t := key.(type) {
case Key:
s,ok := keysymbols[t]
if !ok {
k += "<unknown key>"
} else {
k += s
}
case rune:
k += string(t)
default:
k += "<unknown key type>"
}

return k

}

var keysymbols = map[Key]string{
// Entries commented out duplicate codes with a more prominent combo.
KeyF1: "F1",
KeyF2: "F2",
KeyF3: "F3",
KeyF4: "F4",
KeyF5: "F5",
KeyF6: "F6",
KeyF7: "F7",
KeyF8: "F8",
KeyF9: "F9",
KeyF10: "F10",
KeyF11: "F11",
KeyF12: "F12",
KeyInsert: "Insert",
KeyDelete: "Delete",
KeyHome: "Home",
KeyEnd: "End",
KeyPgup: "PgUp",
KeyPgdn: "PgDn",
KeyArrowUp: "Up",
KeyArrowDown: "Down",
KeyArrowLeft: "Left",
KeyArrowRight: "Right",
// KeyCtrlTilde: "Ctrl+~",
// KeyCtrl2: "Ctrl+2",
KeyCtrlSpace: "Ctrl+Space",
KeyCtrlA: "Ctrl+a",
KeyCtrlB: "Ctrl+b",
KeyCtrlC: "Ctrl+c",
KeyCtrlD: "Ctrl+d",
KeyCtrlE: "Ctrl+e",
KeyCtrlF: "Ctrl+f",
KeyCtrlG: "Ctrl+g",
KeyBackspace: "Backspace",
// KeyCtrlH: "Ctrl+h",
KeyTab: "Ctrl+Tab",
// KeyCtrlI: "Ctrl+i",
KeyCtrlJ: "Ctrl+j",
KeyCtrlK: "Ctrl+k",
KeyCtrlL: "Ctrl+l",
KeyEnter: "Ctrl+Enter",
// KeyCtrlM: "Ctrl+m",
KeyCtrlN: "Ctrl+n",
KeyCtrlO: "Ctrl+o",
KeyCtrlP: "Ctrl+p",
KeyCtrlQ: "Ctrl+q",
KeyCtrlR: "Ctrl+r",
KeyCtrlS: "Ctrl+s",
KeyCtrlT: "Ctrl+t",
KeyCtrlU: "Ctrl+u",
KeyCtrlV: "Ctrl+v",
KeyCtrlW: "Ctrl+w",
KeyCtrlX: "Ctrl+x",
KeyCtrlY: "Ctrl+y",
KeyCtrlZ: "Ctrl+z",
KeyEsc: "Esc",
// KeyCtrlLsqBracket: "Ctrl+[",
// KeyCtrl3: "Ctrl+3",
// KeyCtrl4: "Ctrl+4",
KeyCtrlBackslash: "Ctrl+\\",
// KeyCtrl5: "Ctrl+5",
KeyCtrlRsqBracket: "Ctrl+]",
KeyCtrl6: "Ctrl+6",
// KeyCtrl7: "Ctrl+7",
KeyCtrlSlash: "Ctrl+/",
// KeyCtrlUnderscore: "Ctrl+_",
KeySpace: "Ctrl+Space",
KeyBackspace2: "Ctrl+Backspace",
// KeyCtrl8: "Ctrl+8",
}
21 changes: 16 additions & 5 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ type View struct {
// If Mask is true, the View will display the mask instead of the real
// content
Mask rune

// If CarriageReturn is true, then '\r' characters will reset the
// prior line.
CarriageReturn bool
}

type viewLine struct {
Expand Down Expand Up @@ -207,11 +211,13 @@ func (v *View) Write(p []byte) (n int, err error) {
case '\n':
v.lines = append(v.lines, nil)
case '\r':
nl := len(v.lines)
if nl > 0 {
v.lines[nl-1] = nil
} else {
v.lines = make([][]cell, 1)
if v.CarriageReturn {
nl := len(v.lines)
if nl > 0 {
v.lines[nl-1] = nil
} else {
v.lines = make([][]cell, 1)
}
}
default:
cells := v.parseInput(ch)
Expand Down Expand Up @@ -477,3 +483,8 @@ func (v *View) Word(x, y int) (string, error) {
func indexFunc(r rune) bool {
return r == ' ' || r == 0
}

// NumLines returns the number of lines in the view's buffer.
func (v *View) NumLines() int {
return len(v.lines)
}