-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
103 lines (91 loc) · 1.88 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
//
package main
import (
"bytes"
"os/exec"
"strings"
)
func indent(strs []string) []string {
indented := make([]string, len(strs))
for i, line := range strs {
indented[i] = "\t" + line
}
return indented
}
// Render html with w3m; note that w3m appends a newline
func renderHTML(s string) []string {
cmd := exec.Command("w3m", "-dump", "-T", "text/html")
// https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html#behind-the-scenes-of-combinedoutput
var b bytes.Buffer
cmd.Stdout = &b
in, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
if _, err := in.Write([]byte(s)); err != nil {
panic(err)
}
if err := in.Close(); err != nil {
panic(err)
}
if err := cmd.Run(); err != nil {
panic(err)
}
// return b.String()
return strings.Split(b.String(), "\n")
}
func stripHtmlTags(s string) string {
var count int
var inner []rune
var chars []rune
for _, c := range s {
// fmt.Println(c, string(inner))
switch {
case c == '<':
count++
continue
case c == '>' && count > 0:
count--
if count == 0 && string(inner) == "br" {
chars = append(chars, ' ')
}
inner = []rune{}
continue
case count > 0:
inner = append(inner, c)
continue
default:
chars = append(chars, c)
}
}
return string(chars)
}
// Returns [start,end) range
func getScrollWindow[T any](
current int,
slice *[]T,
// limit int, // very prone to off-by-1 footguns
scrolloff int,
) (start int, end int) {
limit := len(*slice) - 1
switch {
case current <= scrolloff:
start, end = 0, min(limit, scrolloff*2)
case limit-current <= scrolloff:
start, end = max(0, limit-2*scrolloff), limit
default:
start, end = current-scrolloff, current+scrolloff
}
return start, end + 1
}
func filter[T comparable](arr []T, remove T) []T {
i := 0
for _, x := range arr {
if x == remove {
continue
}
arr[i] = x
i++
}
return arr[:i]
}