func main() { defer display.Reset() bytes, err := ioutil.ReadFile(files[0]) if err != nil { panic(err) } ed = editor.NewEditor(bytes, files[0]) display.Show([]rune(string(bytes)), []rune{}) display.Draw() Poll() }
func parseSlash() { runes := input.Runes() defer func() { input.AddRune('/') input.Draw() }() // input.Reset() if len(runes) == 0 { return } commands := parseLine(string(runes)) ed.SaveDot() if onlyHighlights(commands) { for _, command := range commands { ed.Command(command[0], command[1:]) } display.Highlight(ed.Highlights()) display.Draw() ed.UnSaveDot() return } }
func Poll() { for { e := termbox.PollEvent() // LogS(fmt.Sprintf("%d", e.Key)) switch { case e.Type == termbox.EventResize: display.Resize() display.Draw() case e.Key == termbox.KeyCtrlA: // Control-A input.CursorAtBeginning() case e.Key == termbox.KeyCtrlE: // Control-E input.CursorAtEnd() case e.Key == termbox.KeyCtrlC: // Control-C Exit() // Scrolling case e.Key == termbox.KeyCtrlN: display.ScrollDown() case e.Key == termbox.KeyCtrlP: display.ScrollUp() case e.Key == termbox.KeyEsc: input.Reset() case e.Key == termbox.KeySpace: //Space input.AddRune(' ') input.Draw() case e.Key == termbox.KeyEnter: // Return runes := input.Runes() commandHistory.Add(runes) input.Reset() if len(runes) == 0 { break } commands := parseLine(string(runes)) for _, command := range commands { ed.Command(command[0], command[1:]) } display.ShowFile([]rune(ed.String())) display.Highlight(ed.Highlights()) display.Draw() case e.Ch == '/': parseSlash() // // Arrow keys // Cursor Movement case e.Key == termbox.KeyArrowLeft: input.CursorLeft() case e.Key == termbox.KeyArrowRight: input.CursorRight() case e.Key == termbox.KeyArrowUp: past, err := commandHistory.Pop() if err != nil { // Empty stack. // show alert later. break } input.Reset() input.SetRunes(past.([]rune)) input.Draw() break case e.Key == termbox.KeyArrowDown: input.Reset() past, err := commandHistory.UnPop() if err != nil { // Empty stack. // show alert later. break } input.SetRunes(past.([]rune)) input.Draw() break // Delete Key case e.Key == termbox.KeyBackspace || e.Key == termbox.KeyBackspace2 || e.Key == termbox.KeyDelete: input.Backspace() input.Draw() case e.Key == 0: // All other normal chars input.AddRune(e.Ch) input.Draw() } } }