From c455562703977feafbe3c37cc3cf8130328f0e9f Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 30 Mar 2021 16:35:03 -0400 Subject: [PATCH] Simplify ascii helpers --- editor/draw.go | 2 +- editor/editor.go | 11 +++++++---- editor/fn.go | 21 ++++++++------------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/editor/draw.go b/editor/draw.go index 2967bdc..f1601d9 100644 --- a/editor/draw.go +++ b/editor/draw.go @@ -30,7 +30,7 @@ func (e *editor) drawRows(ab *buffer) { if y == e.screen.Rows / 3 { welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion) 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 diff --git a/editor/editor.go b/editor/editor.go index 42f0260..7e401ea 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -31,11 +31,14 @@ func (e *editor) ProcessKeypress() bool { var str string ch, _ := terminal.ReadKey() - if ch == Ctrl('q') { - // Clean up on exit - terminal.Write(terminal.ClearScreen + terminal.ResetCursor) + if isCtrl(ch) { + switch ch { + case ctrl('q'): + // Clean up on exit + terminal.Write(terminal.ClearScreen + terminal.ResetCursor) - return false + return false + } } if ch == '\x1b' { diff --git a/editor/fn.go b/editor/fn.go index 59eb552..11faaf9 100644 --- a/editor/fn.go +++ b/editor/fn.go @@ -2,7 +2,7 @@ package editor import "strings" -func TruncateString(s string, length int) string { +func truncateString(s string, length int) string { if length < 1 { return "" } @@ -23,29 +23,24 @@ func TruncateString(s string, length int) string { } func isAscii(char rune) bool { - ord := int(char) - - return ord < 0x80 + return char < 0x80 } -func IsCtrl(char rune) bool { +func isCtrl(char rune) bool { if !isAscii(char) { return false } - ord := int(char) - - return ord == 0x7f || ord < 0x20 + return char == 0x7f || char < 0x20 } // Return the input code of a Ctrl-key chord. -func Ctrl(char rune) rune { - ord := int(char) - raw := ord & 0x1f +func ctrl(char rune) rune { + ch := char & 0x1f - if !IsCtrl(rune(raw)) { + if !isCtrl(ch) { return 0 } - return rune(raw) + return ch } \ No newline at end of file