-
Notifications
You must be signed in to change notification settings - Fork 31
/
utils.go
142 lines (113 loc) · 3.25 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2019-2022 Graham Clark. All rights reserved. Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package gowid
import (
"fmt"
"strings"
tcell "github.com/gdamore/tcell/v2"
)
//======================================================================
type Direction int
const (
Forwards = Direction(1)
Backwards = Direction(-1)
)
//======================================================================
// Unit is a one-valued type used to send a message over a channel.
type Unit struct{}
//======================================================================
type InvalidTypeToCompare struct {
LHS interface{}
RHS interface{}
}
var _ error = InvalidTypeToCompare{}
func (e InvalidTypeToCompare) Error() string {
return fmt.Sprintf("Cannot compare RHS %v of type %T with LHS %v of type %T", e.RHS, e.RHS, e.LHS, e.LHS)
}
//======================================================================
type KeyValueError struct {
Base error
KeyVals map[string]interface{}
}
var _ error = KeyValueError{}
var _ error = (*KeyValueError)(nil)
func (e KeyValueError) Error() string {
kvs := make([]string, 0, len(e.KeyVals))
for k, v := range e.KeyVals {
kvs = append(kvs, fmt.Sprintf("%v: %v", k, v))
}
return fmt.Sprintf("%s [%s]", e.Cause().Error(), strings.Join(kvs, ", "))
}
func (e KeyValueError) Cause() error {
return e.Base
}
func (e KeyValueError) Unwrap() error {
return e.Base
}
func WithKVs(err error, kvs map[string]interface{}) KeyValueError {
return KeyValueError{
Base: err,
KeyVals: kvs,
}
}
//======================================================================
// TranslatedMouseEvent is supplied with a tcell event and an x and y
// offset - it returns a tcell mouse event that represents a horizontal and
// vertical translation.
func TranslatedMouseEvent(ev interface{}, x, y int) interface{} {
if ev3, ok := ev.(*tcell.EventMouse); ok {
x2, y2 := ev3.Position()
evTr := tcell.NewEventMouse(x2+x, y2+y, ev3.Buttons(), ev3.Modifiers())
return evTr
} else {
return ev
}
}
//======================================================================
func posInMap(value string, m map[string]int) int {
i, ok := m[value]
if ok {
return i
} else {
return -1
}
}
//======================================================================
type PrettyModMask tcell.ModMask
func (p PrettyModMask) String() string {
mods := make([]string, 0)
m := int(p)
if m == int(tcell.ModNone) {
mods = append(mods, "None")
} else {
if m&int(tcell.ModShift) != 0 {
mods = append(mods, "Shift")
}
if m&int(tcell.ModCtrl) != 0 {
mods = append(mods, "Ctrl")
}
if m&int(tcell.ModAlt) != 0 {
mods = append(mods, "Alt")
}
if m&int(tcell.ModMeta) != 0 {
mods = append(mods, "Meta")
}
}
return strings.Join(mods, "|")
}
type PrettyTcellKey tcell.EventKey
func (p *PrettyTcellKey) String() string {
k := (*tcell.EventKey)(p)
mod := PrettyModMask(k.Modifiers())
switch k.Key() {
case tcell.KeyRune:
return fmt.Sprintf("<Char:%c Mod:%v>", k.Rune(), mod)
default:
return fmt.Sprintf("<Key:%s Mod:%v>", tcell.KeyNames[k.Key()], mod)
}
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: