Simplify ascii helpers

This commit is contained in:
Timothy Warren 2021-03-30 16:35:03 -04:00
parent d8e23080fc
commit c455562703
3 changed files with 16 additions and 18 deletions

View File

@ -30,7 +30,7 @@ func (e *editor) drawRows(ab *buffer) {
if y == e.screen.Rows / 3 { if y == e.screen.Rows / 3 {
welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion) welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion)
if len(welcome) > e.screen.Cols { if len(welcome) > e.screen.Cols {
welcome = TruncateString(welcome, e.screen.Cols) welcome = truncateString(welcome, e.screen.Cols)
} }
padding := (e.screen.Cols - len(welcome)) / 2 padding := (e.screen.Cols - len(welcome)) / 2

View File

@ -31,11 +31,14 @@ func (e *editor) ProcessKeypress() bool {
var str string var str string
ch, _ := terminal.ReadKey() ch, _ := terminal.ReadKey()
if ch == Ctrl('q') { if isCtrl(ch) {
// Clean up on exit switch ch {
terminal.Write(terminal.ClearScreen + terminal.ResetCursor) case ctrl('q'):
// Clean up on exit
terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
return false return false
}
} }
if ch == '\x1b' { if ch == '\x1b' {

View File

@ -2,7 +2,7 @@ package editor
import "strings" import "strings"
func TruncateString(s string, length int) string { func truncateString(s string, length int) string {
if length < 1 { if length < 1 {
return "" return ""
} }
@ -23,29 +23,24 @@ func TruncateString(s string, length int) string {
} }
func isAscii(char rune) bool { func isAscii(char rune) bool {
ord := int(char) return char < 0x80
return ord < 0x80
} }
func IsCtrl(char rune) bool { func isCtrl(char rune) bool {
if !isAscii(char) { if !isAscii(char) {
return false return false
} }
ord := int(char) return char == 0x7f || char < 0x20
return ord == 0x7f || ord < 0x20
} }
// Return the input code of a Ctrl-key chord. // Return the input code of a Ctrl-key chord.
func Ctrl(char rune) rune { func ctrl(char rune) rune {
ord := int(char) ch := char & 0x1f
raw := ord & 0x1f
if !IsCtrl(rune(raw)) { if !isCtrl(ch) {
return 0 return 0
} }
return rune(raw) return ch
} }