Add modules

This commit is contained in:
Timothy Warren 2021-03-19 16:36:02 -04:00
parent 1b94f7fa94
commit 0b05522b05
3 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1 @@
package editor

29
internal/rune/rune.go Normal file
View File

@ -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)
}

View File

@ -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)
}