input.go (1892B)
1 package editor 2 3 import ( 4 "fmt" 5 6 "code.dwrz.net/src/pkg/editor/event" 7 "code.dwrz.net/src/pkg/terminal/input" 8 ) 9 10 func (e *Editor) bufferInput(in *input.Event) error { 11 size, err := e.terminal.Size() 12 if err != nil { 13 return fmt.Errorf("failed to get terminal size: %w", err) 14 } 15 16 if in.Rune != input.Null { 17 switch in.Rune { 18 case input.Delete: 19 e.active.Backspace() 20 21 case 'q' & input.Control: 22 e.prompt.Prompt("Quit? (y/n)", e.quit) 23 24 case 's' & input.Control: 25 go func() { 26 if err := e.active.Save(); err != nil { 27 e.events <- event.NewMessage( 28 fmt.Sprintf( 29 "failed to save: %v", 30 err, 31 ), 32 ) 33 } 34 e.events <- event.NewMessage("saved file") 35 }() 36 37 case 'x' & input.Control: 38 e.prompt.Prompt("Save as:", e.saveAs) 39 40 default: 41 e.active.Insert(in.Rune) 42 } 43 44 return nil 45 } 46 47 switch in.Key { 48 case input.Down: 49 e.active.CursorDown() 50 51 case input.Left: 52 e.active.CursorLeft() 53 54 case input.Right: 55 e.active.CursorRight() 56 57 case input.Up: 58 e.active.CursorUp() 59 60 case input.End: 61 e.active.CursorEnd() 62 63 case input.Home: 64 e.active.CursorHome() 65 66 case input.PageDown: 67 e.active.PageDown(int(size.Rows)) 68 69 case input.PageUp: 70 e.active.PageUp(int(size.Rows)) 71 72 default: 73 e.log.Debug.Printf("unrecognized input: %#v", in) 74 } 75 76 return nil 77 } 78 79 func (e *Editor) promptInput(event *input.Event) error { 80 if event.Rune != input.Null { 81 switch event.Rune { 82 case input.Escape: 83 e.prompt.Escape() 84 85 case input.CarriageReturn: 86 e.prompt.Complete() 87 88 case input.Delete: 89 e.prompt.Backspace() 90 91 default: 92 e.prompt.Insert(event.Rune) 93 } 94 95 return nil 96 } 97 98 switch event.Key { 99 case input.Left: 100 e.prompt.CursorLeft() 101 102 case input.Right: 103 e.prompt.CursorRight() 104 105 case input.End: 106 e.prompt.CursorEnd() 107 108 case input.Home: 109 e.prompt.CursorHome() 110 111 default: 112 e.log.Debug.Printf("unrecognized input: %#v", event) 113 } 114 115 return nil 116 }