Add help text

This commit is contained in:
Zachary Yedidia
2016-03-27 17:53:00 -04:00
parent d167902796
commit ec42140241
4 changed files with 97 additions and 1 deletions

86
src/help.go Normal file
View File

@@ -0,0 +1,86 @@
package main
import (
"github.com/gdamore/tcell"
"strings"
)
const helpTxt = `Press Ctrl-q to quit help
Micro keybindings:
Ctrl-q: Quit
Ctrl-s: Save
Ctrl-o: Open file
Ctrl-z: Undo
Ctrl-y: Redo
Ctrl-a: Select all
Ctrl-c: Copy
Ctrl-x: Cut
Ctrl-v: Paste
Ctrl-h: Open help
Ctrl-u: Half page up
Ctrl-d: Half page down
PageUp: Page up
PageDown: Page down
Ctrl-e: Set option
Micro options:
colorscheme: loads the colorscheme stored in ~/.micro/colorschemes/'option'.micro
default value: 'default'
tabsize: sets the tab size to 'option'
default value: '4'
`
// DisplayHelp displays the help txt
// It blocks the main loop
func DisplayHelp() {
topline := 0
_, height := screen.Size()
screen.HideCursor()
totalLines := strings.Split(helpTxt, "\n")
for {
screen.Clear()
lineEnd := topline + height
if lineEnd > len(totalLines) {
lineEnd = len(totalLines)
}
lines := totalLines[topline:lineEnd]
for y, line := range lines {
for x, ch := range line {
st := tcell.StyleDefault
screen.SetContent(x, y, ch, nil, st)
}
}
screen.Show()
event := screen.PollEvent()
switch e := event.(type) {
case *tcell.EventResize:
_, height = e.Size()
case *tcell.EventKey:
switch e.Key() {
case tcell.KeyUp:
if topline > 0 {
topline--
}
case tcell.KeyDown:
if topline < len(totalLines)-height {
topline++
}
case tcell.KeyCtrlQ, tcell.KeyCtrlW, tcell.KeyEscape, tcell.KeyCtrlC:
return
}
}
}
}

View File

@@ -147,6 +147,10 @@ func main() {
}
case tcell.KeyCtrlE:
SetOption(view)
case tcell.KeyCtrlH:
DisplayHelp()
// Make sure to resize the view if the user resized the terminal while looking at the help text
view.Resize(screen.Size())
}
}

View File

@@ -41,6 +41,8 @@ func (sline *Statusline) Display() {
// Add the filetype
file += " " + sline.view.buf.filetype
centerText := "Press Ctrl-h for help"
statusLineStyle := tcell.StyleDefault.Reverse(true)
if style, ok := colorscheme["statusline"]; ok {
statusLineStyle = style
@@ -51,6 +53,8 @@ func (sline *Statusline) Display() {
for x := 0; x < sline.view.width; x++ {
if x < len(fileRunes) {
screen.SetContent(x, y, fileRunes[x], nil, statusLineStyle)
} else if x >= sline.view.width/2-len(centerText)/2 && x < len(centerText)+sline.view.width/2-len(centerText)/2 {
screen.SetContent(x, y, []rune(centerText)[x-sline.view.width/2+len(centerText)/2], nil, statusLineStyle)
} else {
screen.SetContent(x, y, ' ', nil, statusLineStyle)
}

View File

@@ -159,7 +159,9 @@ func (v *View) PageDown() {
if len(v.buf.lines)-(v.topline+v.height) > v.height {
v.ScrollDown(v.height)
} else {
v.topline = len(v.buf.lines) - v.height
if len(v.buf.lines) >= v.height {
v.topline = len(v.buf.lines) - v.height
}
}
}