forked from gcla/termshark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
338 lines (272 loc) · 8.07 KB
/
Copy pathutils.go
File metadata and controls
338 lines (272 loc) · 8.07 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// 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 termshark
import (
"bytes"
"context"
"encoding/xml"
"fmt"
"io"
"os"
"slices"
"regexp"
"runtime"
"strings"
"sync"
"text/template"
"time"
"unicode"
"github.com/gcla/gowid"
"github.com/gcla/termshark/v2/pkg/lifecycle"
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/terminfo"
"github.com/gdamore/tcell/v2/terminfo/dynamic"
"github.com/mattn/go-isatty"
log "github.com/sirupsen/logrus"
)
//======================================================================
type BadStateError struct{}
var _ error = BadStateError{}
func (e BadStateError) Error() string {
return "Bad state"
}
var BadState = BadStateError{}
//======================================================================
type BadCommandError struct{}
var _ error = BadCommandError{}
func (e BadCommandError) Error() string {
return "Error running command"
}
var BadCommand = BadCommandError{}
//======================================================================
type ConfigError struct{}
var _ error = ConfigError{}
func (e ConfigError) Error() string {
return "Configuration error"
}
var ConfigErr = ConfigError{}
//======================================================================
type InternalError struct{}
var _ error = InternalError{}
func (e InternalError) Error() string {
return "Internal error"
}
var InternalErr = InternalError{}
//======================================================================
const (
UserGuideURL = "https://github.com/georgeglarson/termshark/blob/master/docs/UserGuide.md"
FAQURL = "https://github.com/georgeglarson/termshark/blob/master/docs/FAQ.md"
BugURL = "https://github.com/georgeglarson/termshark/issues/new?assignees=&labels=&template=bug_report.md&title="
FeatureURL = "https://github.com/georgeglarson/termshark/issues/new?assignees=&labels=&template=feature_request.md&title="
)
var (
OriginalEnv []string
ShouldSwitchTerminal bool
ShouldSwitchBack bool
unitsRe *regexp.Regexp = regexp.MustCompile(`^([0-9,]+)\s*(bytes|kB|MB)?`)
tsharkVersionRe *regexp.Regexp = regexp.MustCompile(`^TShark .*?(\d+\.\d+\.\d+)`)
interfaceRe *regexp.Regexp = regexp.MustCompile(`^(?P<index>[0-9]+)\.\s+(?P<name1>[^\s]+)(\s*\((?P<name2>[^)]+)\))?`)
argRe *regexp.Regexp = regexp.MustCompile(`^\$([1-9][0-9]{0,4})$`)
)
//======================================================================
func ReverseStringSlice(s []string) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func KeyPressIsPrintable(key gowid.IKey) bool {
return unicode.IsPrint(key.Rune()) && key.Modifiers() & ^tcell.ModShift == 0
}
// Must succeed - use on internal templates
func TemplateToString(tmpl *template.Template, name string, data interface{}) string {
var res bytes.Buffer
if err := tmpl.ExecuteTemplate(&res, name, data); err != nil {
log.Fatal(err)
}
return res.String()
}
func StringIsArgPrefixOf(a string, list []string) bool {
for _, b := range list {
if strings.HasPrefix(a, fmt.Sprintf("%s=", b)) {
return true
}
}
return false
}
func RunOnDoubleTicker(ch <-chan struct{}, fn func(), dur1 time.Duration, dur2 time.Duration, loops int) {
ctx := Context()
ticker := time.NewTicker(dur1)
counter := 0
Loop:
for {
select {
case <-ticker.C:
fn()
counter++
if counter == loops {
ticker.Stop()
ticker = time.NewTicker(dur2)
}
case <-ch:
ticker.Stop()
break Loop
case <-ctx.Done():
ticker.Stop()
break Loop
}
}
}
// globalTracker is the centralized goroutine lifecycle tracker.
// Set via SetTracker() from main().
var globalTracker *lifecycle.Tracker
// SetTracker sets the global lifecycle tracker. Call this from main()
// before starting any goroutines.
func SetTracker(t *lifecycle.Tracker) {
globalTracker = t
}
// Tracker returns the global lifecycle tracker.
func Tracker() *lifecycle.Tracker {
return globalTracker
}
// TrackedGo starts a goroutine tracked by the provided WaitGroups.
// Deprecated: Use Tracker().Go() instead when globalTracker is set.
func TrackedGo(fn func(), wgs ...*sync.WaitGroup) {
for _, wg := range wgs {
wg.Add(1)
}
go func() {
for _, wg := range wgs {
defer wg.Done()
}
fn()
}()
}
// Go starts a tracked goroutine using the global tracker.
// This is the preferred way to start goroutines.
func Go(fn func()) {
if globalTracker != nil {
globalTracker.Go(fn)
} else {
// Fallback if tracker not set (shouldn't happen in normal operation)
go fn()
}
}
// GoWithContext starts a tracked goroutine and provides the tracker's context.
// The function should monitor ctx.Done() for shutdown signals.
func GoWithContext(fn func(ctx context.Context)) {
if globalTracker != nil {
globalTracker.GoWithContext(fn)
} else {
// Fallback if tracker not set
go fn(context.Background())
}
}
// Context returns the global tracker's context, or a background context if
// the tracker is not set. Useful for goroutines that need to detect shutdown.
func Context() context.Context {
if globalTracker != nil {
return globalTracker.Context()
}
return context.Background()
}
//======================================================================
func ErrLogger(key string, val string) *io.PipeWriter {
l := log.StandardLogger()
return log.NewEntry(l).WithField(key, val).WriterLevel(log.ErrorLevel)
}
// KeyValueErrorString returns a string representation of
// a gowid KeyValueError intended to be suitable for displaying in
// a termshark error dialog.
func KeyValueErrorString(err gowid.KeyValueError) string {
res := fmt.Sprintf("%v\n\n", err.Cause())
kvs := make([]string, 0, len(err.KeyVals))
ks := make([]string, 0, len(err.KeyVals))
for k := range err.KeyVals {
ks = append(ks, k)
}
slices.Sort(ks)
for _, k := range ks {
kvs = append(kvs, fmt.Sprintf("%v: %v", k, err.KeyVals[k]))
}
res = res + strings.Join(kvs, "\n\n")
return res
}
//======================================================================
func IsTerminal(fd uintptr) bool {
return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
}
//======================================================================
type pdmlany struct {
XMLName xml.Name
Attrs []xml.Attr `xml:",any,attr"`
Comment string `xml:",comment"`
Nested []*pdmlany `xml:",any"`
//Content string `xml:",chardata"`
}
// IndentPdml reindents XML, disregarding content between tags (because we knoe
// PDML doesn't use that capability of XML)
func IndentPdml(in io.Reader, out io.Writer) error {
decoder := xml.NewDecoder(in)
n := pdmlany{}
if err := decoder.Decode(&n); err != nil {
return err
}
b, err := xml.MarshalIndent(n, "", " ")
if err != nil {
return err
}
out.Write(fixNewlines(b))
return nil
}
func fixNewlines(unix []byte) []byte {
if runtime.GOOS != "windows" {
return unix
}
return bytes.Replace(unix, []byte{'\n'}, []byte{'\r', '\n'}, -1)
}
//======================================================================
type iWrappedError interface {
Cause() error
}
func RootCause(err error) error {
res := err
for {
if cerr, ok := res.(iWrappedError); ok {
res = cerr.Cause()
} else {
break
}
}
return res
}
//======================================================================
func RunningRemotely() bool {
return os.Getenv("SSH_TTY") != ""
}
//======================================================================
type KeyState struct {
NumberPrefix int
PartialgCmd bool
PartialZCmd bool
PartialCtrlWCmd bool
PartialmCmd bool
PartialQuoteCmd bool
}
//======================================================================
func Does256ColorTermExist() error {
return ValidateTerm(fmt.Sprintf("%s-256color", os.Getenv("TERM")))
}
func ValidateTerm(term string) error {
var err error
_, err = terminfo.LookupTerminfo(term)
if err != nil {
_, _, err = dynamic.LoadTerminfo(term)
}
return err
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 78
// End: