From a7f159bddce3b54f16969de6522fb5eeefdb69f1 Mon Sep 17 00:00:00 2001 From: boombuler Date: Tue, 13 Sep 2016 08:53:20 +0200 Subject: [PATCH] Load help files when needed --- cmd/micro/autocomplete.go | 3 +-- cmd/micro/help.go | 36 ++++++++++++++++-------------------- cmd/micro/micro.go | 3 --- cmd/micro/view.go | 17 ++++++++++------- 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/cmd/micro/autocomplete.go b/cmd/micro/autocomplete.go index 941f0fc5..84904252 100644 --- a/cmd/micro/autocomplete.go +++ b/cmd/micro/autocomplete.go @@ -81,9 +81,8 @@ func CommandComplete(input string) (string, []string) { func HelpComplete(input string) (string, []string) { var suggestions []string - for _, topic := range helpFiles { + for topic, _ := range helpPages { if strings.HasPrefix(topic, input) { - suggestions = append(suggestions, topic) } } diff --git a/cmd/micro/help.go b/cmd/micro/help.go index 4cced6ad..eaf9f0ee 100644 --- a/cmd/micro/help.go +++ b/cmd/micro/help.go @@ -1,25 +1,21 @@ package main -var helpPages map[string]string - -var helpFiles = []string{ - "help", - "keybindings", - "plugins", - "colors", - "options", - "commands", - "tutorial", +type HelpPage interface { + HelpFile() ([]byte, error) } -// LoadHelp loads the help text from inside the binary -func LoadHelp() { - helpPages = make(map[string]string) - for _, file := range helpFiles { - data, err := Asset("runtime/help/" + file + ".md") - if err != nil { - TermMessage("Unable to load help text", file) - } - helpPages[file] = string(data) - } +var helpPages map[string]HelpPage = map[string]HelpPage{ + "help": assetHelpPage("help"), + "keybindings": assetHelpPage("keybindings"), + "plugins": assetHelpPage("plugins"), + "colors": assetHelpPage("colors"), + "options": assetHelpPage("options"), + "commands": assetHelpPage("commands"), + "tutorial": assetHelpPage("tutorial"), +} + +type assetHelpPage string + +func (file assetHelpPage) HelpFile() ([]byte, error) { + return Asset("runtime/help/" + string(file) + ".md") } diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 71076089..2081fe2f 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -242,9 +242,6 @@ func main() { // Load the syntax files, including the colorscheme LoadSyntaxFiles() - // Load the help files - LoadHelp() - // Start the screen InitScreen() diff --git a/cmd/micro/view.go b/cmd/micro/view.go index 889bbf9b..dff2b11b 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -529,15 +529,18 @@ func (v *View) ClearAllGutterMessages() { // Opens the given help page in a new horizontal split func (v *View) openHelp(helpPage string) { - if v.Help { - helpBuffer := NewBuffer([]byte(helpPages[helpPage]), helpPage+".md") - helpBuffer.Name = "Help" - v.OpenBuffer(helpBuffer) + if data, err := helpPages[helpPage].HelpFile(); err != nil { + TermMessage("Unable to load help text", helpPage, "\n", err) } else { - helpBuffer := NewBuffer([]byte(helpPages[helpPage]), helpPage+".md") + helpBuffer := NewBuffer(data, helpPage+".md") helpBuffer.Name = "Help" - v.HSplit(helpBuffer) - CurView().Help = true + + if v.Help { + v.OpenBuffer(helpBuffer) + } else { + v.HSplit(helpBuffer) + CurView().Help = true + } } }