diff --git a/internal/editor/editor.go b/internal/editor/editor.go new file mode 100644 index 0000000..323df60 --- /dev/null +++ b/internal/editor/editor.go @@ -0,0 +1 @@ +package editor diff --git a/internal/rune/rune.go b/internal/rune/rune.go new file mode 100644 index 0000000..6213577 --- /dev/null +++ b/internal/rune/rune.go @@ -0,0 +1,29 @@ +package rune + +func isAscii(char rune) bool { + ord := int(char) + + return ord < 0x80 +} + +func IsCtrl(char rune) bool { + if !isAscii(char) { + return false + } + + ord := int(char) + + return ord == 0x7f || ord < 0x20 +} + +// Return the input code of a Ctrl-key chord. +func Ctrl(char rune) rune { + ord := int(char) + raw := ord & 0x1f + + if !IsCtrl(rune(raw)) { + return 0 + } + + return rune(raw) +} diff --git a/internal/terminal/terminal.go b/internal/terminal/terminal.go new file mode 100644 index 0000000..df66c86 --- /dev/null +++ b/internal/terminal/terminal.go @@ -0,0 +1,32 @@ +package terminal + +import ( + "fmt" + "golang.org/x/term" + "os" +) + +// Put the terminal in raw mode +func RawOn() *term.State { + oldState, err := term.MakeRaw(int(os.Stdin.Fd())) + if err != nil { + panic(err) + } + + return oldState +} + +// Restore the terminal to canonical mode +func RawOff(oldState *term.State) { + err := term.Restore(int(os.Stdin.Fd()), oldState) + if err != nil { + panic(err) + } +} + +// Print a formatted string to stdout, with CRLF line endings for proper terminal formatting +func OutLn(format string, a ...interface{}) { + formatted := fmt.Sprintf(format, a...) + + fmt.Printf("%s\r\n", formatted) +} \ No newline at end of file