/
cmd_list.go
65 lines (54 loc) · 1.15 KB
/
cmd_list.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
package debug
import (
"bufio"
"fmt"
"os"
"strconv"
)
var cmdList = Command{
Name: "list",
Aliases: []string{"ls"},
Summary: "Lists the lines of the source code",
Exec: listLinesExec,
}
func listLinesExec(args []string) {
file := getCurBTFFilename()
if file == "" {
fmt.Println(yellow("Program has no BTF, can't list lines, showing instruction instread"))
listInstructionExec(nil)
return
}
// TODO cache files
f, err := os.Open(file)
if err != nil {
// TODO fall back to just BTF in case we can't open the original source file
printRed("error open source: %s\n", err)
return
}
defer f.Close()
s := bufio.NewScanner(f)
const windowsize = 9
start := getCurBTFLineNumber() - windowsize
if start < 0 {
start = 0
}
end := getCurBTFLineNumber() + windowsize
// Scan up to the start of the line
i := 1
for s.Scan() && i < start {
i++
}
indexPadSize := len(strconv.Itoa(end))
for i = i + 1; i < end; i++ {
if !s.Scan() {
break
}
if i == getCurBTFLineNumber() {
fmt.Print(yellow(" => "))
} else {
fmt.Print(" ")
}
fmt.Print(blue(fmt.Sprintf("%*d ", indexPadSize, i)))
fmt.Println(s.Text())
}
}