2021-03-30 18:00:06 -04:00
|
|
|
// Helper functions
|
2021-03-30 16:22:35 -04:00
|
|
|
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 {
|
2021-03-30 16:22:35 -04:00
|
|
|
if length < 1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:43:46 -04:00
|
|
|
if len(s) < length {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-03-30 16:22:35 -04:00
|
|
|
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 {
|
2021-03-30 20:22:11 -04:00
|
|
|
if !isAscii(char) {
|
2021-03-19 16:36:02 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-03-30 20:22:11 -04:00
|
|
|
ch := char & 0x1f
|
|
|
|
|
2021-03-30 16:35:03 -04:00
|
|
|
return ch
|
2021-03-30 16:22:35 -04:00
|
|
|
}
|