From ec42140241ed3d11a2f91a8a658a1dd30cf39fb5 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 27 Mar 2016 17:53:00 -0400 Subject: [PATCH] Add help text --- src/help.go | 86 +++++++++++++++++++++++++++++++++++++++++++++++ src/micro.go | 4 +++ src/statusline.go | 4 +++ src/view.go | 4 ++- 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/help.go diff --git a/src/help.go b/src/help.go new file mode 100644 index 00000000..a46360ad --- /dev/null +++ b/src/help.go @@ -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 + } + } + } +} diff --git a/src/micro.go b/src/micro.go index e3df3c7a..884272d1 100644 --- a/src/micro.go +++ b/src/micro.go @@ -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()) } } diff --git a/src/statusline.go b/src/statusline.go index e3b1d87e..a62b046a 100644 --- a/src/statusline.go +++ b/src/statusline.go @@ -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) } diff --git a/src/view.go b/src/view.go index 0263d45d..58fe28ba 100644 --- a/src/view.go +++ b/src/view.go @@ -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 + } } }