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 {
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

View File

@ -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' {

View File

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