-
Notifications
You must be signed in to change notification settings - Fork 0
/
preview.go
40 lines (33 loc) · 936 Bytes
/
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
package ilse
import (
"fmt"
"os/exec"
"strconv"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/tjmtmmnk/ilse/filter"
)
var (
preview *tview.TextView
)
func initPreview() {
preview = tview.NewTextView().
SetDynamicColors(true).
SetScrollable(true)
preview.SetBackgroundColor(tcell.ColorBlack)
}
func getPreviewContent(item filter.SearchResult) (string, error) {
_, h := app.screen.Size()
from := item.LineNum - (h/2 - 1)
if from < 0 {
from = 0
}
to := item.LineNum + (h/2 - 1)
lineRange := fmt.Sprintf("%d:%d", from, to)
cmd := []string{"bat", "--line-range", lineRange, "--highlight-line", strconv.Itoa(item.LineNum), "--color=always", "--theme", conf.Theme, "--style=numbers,changes", item.FileName}
out, err := exec.Command(cmd[0], cmd[1:]...).Output()
text := string(out)
text = strings.ReplaceAll(text, "\x1b[0m", "\x1b[39;40m")
return tview.TranslateANSI(text), err
}