forked from tigrawap/slit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slit.go
314 lines (279 loc) · 5.79 KB
/
slit.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
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
package slit
import (
"bufio"
"context"
"fmt"
"io"
"io/ioutil"
"time"
//"log"
//"net/http"
//_ "net/http/pprof"
"os"
"path/filepath"
"sync"
"syscall"
"github.com/nsf/termbox-go"
"github.com/tigrawap/slit/filters"
"github.com/tigrawap/slit/logging"
"github.com/tigrawap/slit/utils"
)
func init() {
logging.Config.LogPath = filepath.Join(os.TempDir(), "slit.log")
slitdir := os.Getenv("SLIT_DIR")
if slitdir == "" {
slitdir = filepath.Join(utils.GetHomeDir(), ".slit")
}
config.historyPath = filepath.Join(slitdir, "history")
config.stdinFinished = make(chan struct{})
config.filterOutput = os.Getenv("SLIT_FILTER_OUTPUT_DIR")
//go func() {
// log.Println(http.ListenAndServe("localhost:6060", nil))
//}()
}
type Config struct {
outPath string
historyPath string
stdin bool
stdinFinished chan struct{}
follow bool
keepChars int
filterOutput string
initFilters []*filters.Filter
}
var config Config
func (c *Config) isStdinRead() bool {
select {
case <-c.stdinFinished:
return true
default:
return false
}
}
// Slit is a configured instance of the pager, ready to be displayed
type Slit struct {
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
file *os.File
isCacheFile bool // if true, file will be removed on shutdown
fetcher *Fetcher
initialised bool
}
// Returns input file, original or cache file when reading from stdin
func (s *Slit) GetFile() *os.File { return s.file }
// Set explicit stdin cache location
func (s *Slit) SetOutPath(path string) { config.outPath = path }
// Set whether to follow file/stdin
func (s *Slit) SetFollow(b bool) { config.follow = b }
// Set initial num of chars kept during horizontal scrolling
func (s *Slit) SetKeepChars(i int) { config.keepChars = i }
// Set initial filters
func (s *Slit) SetFilters(f []*filters.Filter) { config.initFilters = f }
// Invoke the Slit UI
func (s *Slit) Init() {
s.fetcher = newFetcher(s.file, s.ctx)
s.fetcher.filters = config.initFilters
s.initialised = true
}
// Invoke the Slit UI
func (s *Slit) Display() {
if !s.initialised {
s.Init()
}
s.file.Seek(0, io.SeekStart)
//s.fetcher = newFetcher(s.file, s.ctx)
s.fetcher.lock.Lock()
s.fetcher.filters = config.initFilters
s.fetcher.lock.Unlock()
s.fetcher.seek(0)
s.initialised = true
v := &viewer{
fetcher: s.fetcher,
ctx: s.ctx,
keepChars: config.keepChars,
}
v.termGui()
}
// Shutdown and cleanup this pager instance. After instance shutdown,
// it cannot be displayed again
func (s *Slit) Shutdown() {
s.cancel()
s.wg.Wait()
s.file.Close()
if s.isCacheFile {
os.Remove(s.file.Name())
}
}
func New(f *os.File) *Slit {
ctx, cancel := context.WithCancel(context.Background())
s := &Slit{
wg: sync.WaitGroup{},
ctx: ctx,
cancel: cancel,
file: f,
}
return s
}
func NewFromStream(ch chan string) (*Slit, error) {
cacheFile, err := mkCacheFile()
if err != nil {
return nil, err
}
f, err := os.Open(cacheFile.Name())
if err != nil {
return nil, err
}
s := New(f)
s.isCacheFile = true
w := bufio.NewWriter(cacheFile)
lock := sync.Mutex{}
done := false
s.wg.Add(1)
go func() {
for _ = range time.Tick(100 * time.Millisecond) {
lock.Lock()
w.Flush()
lock.Unlock()
if done {
break
}
}
s.wg.Done()
}()
s.wg.Add(1)
go func() {
defer func() {
done = true
s.wg.Done()
}()
for {
select {
case line, ok := <-ch:
if !ok { // channel closed
return
}
lock.Lock()
_, err := w.WriteString(line + "\n")
if err != nil {
panic(err)
//return
}
lock.Unlock()
case <-s.ctx.Done():
return
}
}
}()
return s, nil
}
func NewFromStdin() (*Slit, error) {
config.stdin = true
cacheFile, err := mkCacheFile()
if err != nil {
return nil, err
}
f, err := os.Open(cacheFile.Name())
if err != nil {
return nil, err
}
s := New(f)
if err != nil {
return nil, err
}
s.isCacheFile = true
s.wg.Add(1)
go func() {
var err error
copyLoop:
for {
select {
case <-s.ctx.Done():
break copyLoop
default:
_, err = io.CopyN(cacheFile, os.Stdin, 64*1024)
if err != nil {
break copyLoop
}
}
}
close(config.stdinFinished)
s.wg.Done()
}()
return s, nil
}
func NewFromFilepath(path string) (*Slit, error) {
if err := utils.ValidateRegularFile(path); err != nil {
return nil, err
}
f, err := os.Open(path)
if e, ok := err.(*os.PathError); ok && e.Err == syscall.EACCES {
return nil, fmt.Errorf("%s: Permission denied\n", path)
}
if err != nil {
return nil, err
}
return New(f), nil
}
func mkCacheFile() (f *os.File, err error) {
if config.outPath == "" {
f, err = ioutil.TempFile(os.TempDir(), "slit_")
} else {
f = utils.OpenRewrite(config.outPath)
}
return f, err
}
func (s *Slit) CanFitDisplay(ctx context.Context) bool {
s.Init()
termbox.Init()
w, h := termbox.Size()
termbox.Close()
if w == 0 || h == 0 {
return false
}
localCtx, cancel := context.WithCancel(ctx)
parsedLineCount := 0
lines := s.fetcher.Get(localCtx, Pos{})
defer func() {
for range lines {
} // Draining channel to avoid races on fetcher
}()
defer cancel()
FORLOOP:
for {
select {
case <-ctx.Done():
return false
case line, isOpen := <-lines:
if !isOpen {
if config.stdin && !config.isStdinRead() {
select {
case <-ctx.Done():
return false
case <-time.After(10 * time.Millisecond):
lines = s.fetcher.Get(localCtx, line.Pos)
continue FORLOOP
}
}
break FORLOOP
}
lineLen := len(line.Str.Runes)
if lineLen > 0 {
parsedLineCount += lineLen / w
} else {
parsedLineCount++
}
mod := lineLen % w
if mod != 0 {
parsedLineCount += 1
}
if parsedLineCount > h {
return false
}
}
}
if parsedLineCount < h {
return true
}
return false
}