114 lines
1.9 KiB
Go
114 lines
1.9 KiB
Go
package editor
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"log/slog"
|
||
|
|
|
||
|
|
"code.chimeric.al/dwrz/wen/internal/editor/canvas"
|
||
|
|
"code.chimeric.al/dwrz/wen/pkg/terminal/input"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (e *Editor) bufferInput(in *input.Event) error {
|
||
|
|
size, err := e.terminal.Size()
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("failed to get terminal size: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if in.Rune != input.Null {
|
||
|
|
switch in.Rune {
|
||
|
|
case input.Delete:
|
||
|
|
e.active.Backspace()
|
||
|
|
|
||
|
|
case 'q' & input.Control:
|
||
|
|
e.prompt.Prompt("Quit? (y/n)", e.quit)
|
||
|
|
|
||
|
|
case 's' & input.Control:
|
||
|
|
if err := e.active.Save(); err != nil {
|
||
|
|
e.prompt.SetText(fmt.Sprintf(
|
||
|
|
"failed to save: %v", err,
|
||
|
|
))
|
||
|
|
} else {
|
||
|
|
e.prompt.SetText("saved file")
|
||
|
|
}
|
||
|
|
|
||
|
|
case 'x' & input.Control:
|
||
|
|
e.prompt.Prompt("Save as:", e.saveAs)
|
||
|
|
|
||
|
|
default:
|
||
|
|
e.active.Insert(in.Rune)
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
switch in.Key {
|
||
|
|
case input.Down:
|
||
|
|
e.active.CursorDown()
|
||
|
|
|
||
|
|
case input.Left:
|
||
|
|
e.active.CursorLeft()
|
||
|
|
|
||
|
|
case input.Right:
|
||
|
|
e.active.CursorRight()
|
||
|
|
|
||
|
|
case input.Up:
|
||
|
|
e.active.CursorUp()
|
||
|
|
|
||
|
|
case input.End:
|
||
|
|
e.active.CursorEnd()
|
||
|
|
|
||
|
|
case input.Home:
|
||
|
|
e.active.CursorHome()
|
||
|
|
|
||
|
|
case input.PageDown:
|
||
|
|
e.active.PageDown(int(size.Rows) - canvas.Bars)
|
||
|
|
|
||
|
|
case input.PageUp:
|
||
|
|
e.active.PageUp(int(size.Rows) - canvas.Bars)
|
||
|
|
|
||
|
|
default:
|
||
|
|
e.log.Debug("unrecognized input", slog.Any("input", in))
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (e *Editor) promptInput(event *input.Event) error {
|
||
|
|
if event.Rune != input.Null {
|
||
|
|
switch event.Rune {
|
||
|
|
case input.Escape:
|
||
|
|
e.prompt.Escape()
|
||
|
|
|
||
|
|
case input.CarriageReturn:
|
||
|
|
e.prompt.Complete()
|
||
|
|
|
||
|
|
case input.Delete:
|
||
|
|
e.prompt.Backspace()
|
||
|
|
|
||
|
|
default:
|
||
|
|
e.prompt.Insert(event.Rune)
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
switch event.Key {
|
||
|
|
case input.Left:
|
||
|
|
e.prompt.CursorLeft()
|
||
|
|
|
||
|
|
case input.Right:
|
||
|
|
e.prompt.CursorRight()
|
||
|
|
|
||
|
|
case input.End:
|
||
|
|
e.prompt.CursorEnd()
|
||
|
|
|
||
|
|
case input.Home:
|
||
|
|
e.prompt.CursorHome()
|
||
|
|
|
||
|
|
default:
|
||
|
|
e.log.Debug("unrecognized input", slog.Any("input", event))
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|