gilo/editor/fn.go

50 lines
701 B
Go
Raw Normal View History

2021-03-30 18:00:06 -04:00
// Helper functions
package editor
import "strings"
2021-03-30 18:00:06 -04:00
// Truncate a string to a length
2021-03-30 16:35:03 -04:00
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()
}
2021-03-19 16:36:02 -04:00
2021-03-30 18:00:06 -04:00
// Is this an ASCII character?
2021-03-19 16:36:02 -04:00
func isAscii(char rune) bool {
2021-03-30 16:35:03 -04:00
return char < 0x80
2021-03-19 16:36:02 -04:00
}
2021-03-30 18:00:06 -04:00
// Is this an ASCII ctrl character?
2021-03-30 16:35:03 -04:00
func isCtrl(char rune) bool {
2021-03-19 16:36:02 -04:00
if !isAscii(char) {
return false
}
2021-03-30 16:35:03 -04:00
return char == 0x7f || char < 0x20
2021-03-19 16:36:02 -04:00
}
// Return the input code of a Ctrl-key chord.
2021-03-30 16:35:03 -04:00
func ctrl(char rune) rune {
ch := char & 0x1f
2021-03-19 16:36:02 -04:00
2021-03-30 16:35:03 -04:00
if !isCtrl(ch) {
2021-03-19 16:36:02 -04:00
return 0
}
2021-03-30 16:35:03 -04:00
return ch
}