50 lines
701 B
Go
50 lines
701 B
Go
// Helper functions
|
|
package editor
|
|
|
|
import "strings"
|
|
|
|
// Truncate a string to a length
|
|
func truncateString(s string, length int) string {
|
|
if length < 1 {
|
|
return ""
|
|
}
|
|
|
|
var buf strings.Builder
|
|
count := 0
|
|
|
|
for _, char := range s {
|
|
if count == length {
|
|
break
|
|
}
|
|
|
|
buf.WriteRune(char)
|
|
count++
|
|
}
|
|
|
|
return buf.String()
|
|
}
|
|
|
|
// Is this an ASCII character?
|
|
func isAscii(char rune) bool {
|
|
return char < 0x80
|
|
}
|
|
|
|
// Is this an ASCII ctrl character?
|
|
func isCtrl(char rune) bool {
|
|
if !isAscii(char) {
|
|
return false
|
|
}
|
|
|
|
return char == 0x7f || char < 0x20
|
|
}
|
|
|
|
// Return the input code of a Ctrl-key chord.
|
|
func ctrl(char rune) rune {
|
|
ch := char & 0x1f
|
|
|
|
if !isCtrl(ch) {
|
|
return 0
|
|
}
|
|
|
|
return ch
|
|
} |