-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
174 lines (152 loc) · 3.37 KB
/
util.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package util
import (
"fmt"
"path"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
"unsafe"
"golang.org/x/exp/constraints"
"golang.org/x/exp/maps"
"github.com/srlehn/termimg/internal/errors"
)
func MapsKeysSorted[M ~map[K]V, K constraints.Ordered, V any](m M) []K {
if m == nil {
return nil
}
keys := maps.Keys(m)
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
return keys
}
func MaybeCast[T any](obj any) T {
var ret T
reflect.ValueOf(ret).Kind()
objT, okT := any(obj).(T)
if okT {
ret = objT
}
return ret
}
func MaybeCastDefault[T any](obj any, fallback T) T {
var ret T
reflect.ValueOf(ret).Kind()
objT, okT := any(obj).(T)
if okT {
ret = objT
} else {
ret = fallback
}
return ret
}
// AnyOf returns first non-nil object
func AnyOf[T any](objs ...T) (_ T, found bool) {
var ret T
for _, obj := range objs {
if any(obj) != nil {
return obj, true
}
}
return ret, false
}
func TryClose(obj any) error {
if obj == nil {
return nil
}
closer, ok := any(obj).(interface{ Close() error })
if ok {
return closer.Close()
}
closerIrregular, okIrregular := any(obj).(interface{ Close() })
if okIrregular {
closerIrregular.Close()
}
return nil
}
func Must(err error) {
if err != nil {
err = errors.Wrap(err, 1)
panic(err)
}
}
func Must2[T any](obj T, err error) T {
if err != nil {
err = errors.Wrap(err, 1)
panic(err)
}
return obj
}
func Must3[T, U any](obj1 T, obj2 U, err error) (T, U) {
if err != nil {
err = errors.Wrap(err, 1)
panic(err)
}
return obj1, obj2
}
func Must4[T, U, V any](o1 T, o2 U, o3 V, err error) (T, U, V) {
if err != nil {
err = errors.Wrap(err, 1)
panic(err)
}
return o1, o2, o3
}
func Must5[T, U, V, W any](o1 T, o2 U, o3 V, o4 W, err error) (T, U, V, W) {
if err != nil {
err = errors.Wrap(err, 1)
panic(err)
}
return o1, o2, o3, o4
}
func IgnoreError[T any](obj T, err error) T {
return obj
}
var modulePath string
func fileLinePrefix(skip int) string {
if len(modulePath) == 0 {
_, mp, _, okModule := runtime.Caller(0)
if !okModule {
return ``
}
mp = path.Dir(path.Dir(path.Dir(mp))) // depends on depth of this source file within the module!
modulePath = mp
}
_, filename, line, okCaller := runtime.Caller(skip)
if !okCaller {
return ``
}
return strings.TrimPrefix(filename, modulePath+`/`) + `:` + strconv.Itoa(line) + `: `
}
func StringToBytes(s string) []byte { return unsafe.Slice(unsafe.StringData(s), len(s)) }
func BytesToString(b []byte) string { return unsafe.String(unsafe.SliceData(b), len(b)) }
func storePosAndJumpToPosStr(x, y uint) string { return fmt.Sprintf("\033[s\033[%d;%dH", y, x) }
const restorePosStr = "\033[u"
/*
func RestoreTTY(ptyName string) error {
var errs []error
// if unix
err := restoreTTYUnix(ptyName)
if err == nil {
return nil
}
errs = append(errs, err)
// try posix: stty echo
errRet := errors.New(errors.Join(errs...))
return errRet
}
const (
// TODO build tags
ioctlWriteTermios = unix.TCSETS // unix.TIOCSETA on BSD
ioctlReadTermios = unix.TCGETS // unix.TIOCGETA on BSD
)
func restoreTTYUnix(ptyName string) error {
// TODO: tag unix && !windows && !plan9
in, err := os.Open(ptyName)
if err != nil {
return err
}
termios, err := unix.IoctlGetTermios(int(in.Fd()), uint(ioctlReadTermios)) // get termios
unix.IoctlSetTermios(int(in.Fd()), uint(ioctlWriteTermios), termios) // set termios
return nil
}
*/