github.com/grzegorz-zur/bm@v0.0.0-20240312214136-6fc133e3e2c0/input.go (about) 1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 // Input is a mode for typing. 8 type Input struct { 9 editor *Editor 10 } 11 12 // Show updates mode when switched to. 13 func (mode *Input) Show() error { 14 return nil 15 } 16 17 // Hide updates mode when switched from. 18 func (mode *Input) Hide() error { 19 return nil 20 } 21 22 // Key handles input events. 23 func (mode *Input) Key(key Key) error { 24 switch key { 25 case KeyLeft: 26 mode.editor.MoveLeft() 27 case KeyRight: 28 mode.editor.MoveRight() 29 case KeyUp: 30 mode.editor.MoveUp() 31 case KeyDown: 32 mode.editor.MoveDown() 33 case KeyHome: 34 mode.editor.MoveLineStart() 35 case KeyEnd: 36 mode.editor.MoveLineEnd() 37 case KeyPageUp: 38 mode.editor.MoveParagraphPrevious() 39 case KeyPageDown: 40 mode.editor.MoveParagraphNext() 41 case KeyTab: 42 mode.editor.Insert(string(Tab)) 43 case KeyEnter: 44 mode.editor.Insert(string(EOL)) 45 case KeyBackspace: 46 mode.editor.Backspace() 47 case KeyDelete: 48 mode.editor.Delete() 49 case KeyCtrlSpace: 50 mode.editor.SwitchMode(mode.editor.Command) 51 } 52 return nil 53 } 54 55 // Rune handles rune input. 56 func (mode *Input) Rune(rune rune) error { 57 mode.editor.Insert(string(rune)) 58 return nil 59 } 60 61 // Render renders mode to the screen. 62 func (mode *Input) Render(view *View) error { 63 err := mode.editor.File.Render(view) 64 if err != nil { 65 return fmt.Errorf("error rendering input mode: %w", err) 66 } 67 view.Color = ColorRed 68 return nil 69 }