gilo/key/key.go

47 lines
906 B
Go
Raw Normal View History

2021-04-01 16:17:13 -04:00
package key
2023-10-04 12:50:21 -04:00
import (
"strings"
"unicode"
)
2021-04-01 16:17:13 -04:00
// ----------------------------------------------------------------------------
// !Terminal Input Escape Code Sequences
// ----------------------------------------------------------------------------
const (
2021-04-06 16:37:58 -04:00
Backspace = '\x7f'
Esc = '\x1b'
2021-04-01 20:26:40 -04:00
Enter = '\r'
2021-04-01 16:17:13 -04:00
)
2021-04-13 14:54:44 -04:00
// IsAscii Is this an ASCII character?
2021-04-02 19:24:20 -04:00
func IsAscii(char rune) bool {
return char <= unicode.MaxASCII
2021-04-01 16:17:13 -04:00
}
2021-04-13 14:54:44 -04:00
// IsCtrl Is this an ASCII ctrl character?
2021-04-01 16:17:13 -04:00
func IsCtrl(char rune) bool {
2021-04-02 19:24:20 -04:00
if !IsAscii(char) {
2021-04-01 16:17:13 -04:00
return false
}
return char == 0x7f || char < 0x20
}
2021-04-13 14:54:44 -04:00
// Ctrl Return the input code of a Ctrl-key chord.
2021-04-01 16:17:13 -04:00
func Ctrl(char rune) rune {
2021-04-02 19:24:20 -04:00
if !IsAscii(char) {
2021-04-01 16:17:13 -04:00
return 0
}
ch := char & 0x1f
return ch
}
2021-04-13 14:54:44 -04:00
// IsSeparator Is the character a general token separator type?
func IsSeparator(char rune) bool {
2023-10-04 12:50:21 -04:00
return unicode.IsSpace(char) || strings.ContainsRune(",.()+-/*=~%<>[];", char)
2021-04-16 11:46:16 -04:00
}