Skip to content

Instantly share code, notes, and snippets.

@Ankur008
Created October 13, 2017 12:07
Show Gist options
  • Save Ankur008/e5a740ac33948744877b1b6594714670 to your computer and use it in GitHub Desktop.
Save Ankur008/e5a740ac33948744877b1b6594714670 to your computer and use it in GitHub Desktop.
Func to print the line no, code and current local variable with value
func printCode(t *Term, filename string, line int, showArrow bool,th *api.Thread,ctx callContext,args string) error {
file, err := os.Open(t.substitutePath(filename))
if err != nil {
return err
}
defer file.Close()
fi, _ := file.Stat()
lastModExe := t.client.LastModified()
if fi.ModTime().After(lastModExe) {
fmt.Println("Warning: listing may not match stale executable")
}
buf := bufio.NewScanner(file)
l := line
for i := 1; i < l-5; i++ {
if !buf.Scan() {
return nil
}
}
s := l - 5
if s < 1 {
s = 1
}
for i := s; i <= l+5; i++ {
if !buf.Scan() {
return nil
}
var prefix string
if showArrow {
prefix = " "
if i == l {
prefix = "=>"
}
}
prefix = fmt.Sprintf("%s%4d:\t", prefix, i)
//t.Println(prefix, buf.Text())
//print the code
fmt.Printf("> (%s) %s:%d\n",
buf.Text(),
th.File,
th.Line)
locals(t,ctx,args)
fmt.Println()
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment