- Notifications
You must be signed in to change notification settings - Fork0
/
Copy pathpreview.go
191 lines (168 loc) · 4.22 KB
/
preview.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
package main
import (
"bytes"
"math"
"os"
"path/filepath"
"strings"
"unicode/utf8"
"github.com/alecthomas/chroma/v2/formatters"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
)
// TODO: Add more preview types
// Csv?
// Pdf?
// Images?
// Videos?
func (ts*terminalSession)queuePreview() {
// The width of the preview pane is defined
width:=int(math.Ceil(float64(ts.width)/2.0)-1)
iflen(ts.cwdFiles)<1 {
// If the selection pos is out of range
ts.previewHatch(width)
return
}
file,err:=ts.cwdFiles[ts.selectionPos].Info()
iferr!=nil {
return
}
iffile.Mode()&os.ModeSymlink!=0 {
link,err:=filepath.EvalSymlinks(filepath.Join(ts.cwd,file.Name()))
// Only change the file if the link is found
iferr==nil {
linkInfo,err:=os.Stat(link)
iferr==nil {
file=linkInfo
}
}
}
iffile.IsDir() {
// Get the files under the currently selected dir
fileName:=file.Name()
previewDir:=filepath.Join(ts.cwd,fileName)
previewFiles,err:=os.ReadDir(previewDir)
previewFiles=ts.sortFunc(previewFiles)
ts.previewLen=len(previewFiles)
iferr!=nil {
return
}
ts.queueFiles(
previewFiles,
previewDir,
ts.previewOffsetV,
ts.width/2,
width,
false)// We do not want to get a selection in the preview panel
return
}
// Check if file is valid utf8 and can be displayed as text
// TODO: maybe change this to only read the first line of the file to check UTF8
filePath:=filepath.Join(ts.cwd,file.Name())
b,_:=os.ReadFile(filePath)
fileContent:=string(b)
ifutf8.ValidString(fileContent) {
ts.queueFileContents(
file.Name(),
fileContent,
ts.previewOffsetV,
ts.previewOffsetH,
ts.width/2,
width)
return
}
ts.previewHatch(width)
}
func (ts*terminalSession)previewHatch(widthint) {
ts.previewLen=ts.height-BottomRows
fori:=rangets.previewLen {
line:=StyleFgBlackBright+addPadding("","╱",width)+StyleReset
drawInstr:=drawInstruction{
x:ts.width/2,
y:i,
line:line,
}
ts.drawQueue=append(ts.drawQueue,drawInstr)
}
}
func (ts*terminalSession)queueFileContents(
namestring,
contentsstring,
offsetVint,
offsetHint,
colint,
widthint,
) {
// First we apply the syntax highlighting
lexer:=lexers.Match(name)
iflexer==nil {
lexer=lexers.Fallback
}
// TODO: Make the syntax highlighting match the terminal colors
style:=styles.Get("tokyonight-night")
formatter:=formatters.Get("terminal256")
iterator,err:=lexer.Tokenise(nil,contents)
iferr!=nil {
// TODO:
}
// Write the text with syntax highlighting to a buffer
buffer:=new(bytes.Buffer)
err=formatter.Format(buffer,style,iterator)
iferr!=nil {
// TODO:
}
lines:=strings.Split(buffer.String(),"\n")
ts.previewLen=len(lines)
// Split up the buffer text per newline and add to the drawQueue
fori,line:=rangelines {
ifi<offsetV||i>ts.height+offsetV-1-BottomRows {
continue
}
line=strings.ReplaceAll(line,"\t"," ")
// Trim off the first "offsetH" characters
line=removeFirstChars(line,offsetH)
// Make every line the correct width
line=addPadding(line," ",width)
drawInstr:=drawInstruction{
x:col,
y:i-offsetV,
line:line,
}
ts.drawQueue=append(ts.drawQueue,drawInstr)
}
// We will write blank lines under the files to clear the files pane
blanklines:=ts.height-BottomRows-len(lines)
fori:=rangeblanklines {
line:=addPadding(""," ",width)
drawInstr:=drawInstruction{
x:col,
y:len(lines)+i,
line:line,
}
ts.drawQueue=append(ts.drawQueue,drawInstr)
}
}
funcremoveFirstChars(linestring,nint)string {
runeLine:= []rune(line)
escapeCode:=false
returnLine:=""
for_,rune:=rangeruneLine {
// Escape codes get started with escape and stop at m for the color codes
// Skip adding n non escape characters to the return string
ifescapeCode {
ifrune=='m' {
escapeCode=false
}
returnLine+=string(rune)
continue
}elseifrune==inputMap["escape"] {
escapeCode=true
returnLine+=string(rune)
}elseifn>0 {
n-=1
}else {
returnLine+=string(rune)
}
}
returnreturnLine
}