-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathutils.go
98 lines (75 loc) · 2.26 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
// Copyright 2019 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"
"github.com/gdamore/tcell"
)
//======================================================================
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{}
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 WithKVs(err error, kvs map[string]interface{}) error {
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
}
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: