Skip to content

Commit

Permalink
config: add option for printfile() line count (#2043)
Browse files Browse the repository at this point in the history
The option is "source-list-line-count". It defaults to 5, which was previously
hardcoded in printfile(), but now you can change it dynamically, for instance:

 $ config source-list-line-count 20
 $ list
  • Loading branch information
sqaxomonophonen authored May 11, 2020
1 parent f559c3c commit 71a460f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
17 changes: 17 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,24 @@ type Config struct {
// here: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors)
SourceListLineColor int `yaml:"source-list-line-color"`

// number of lines to list above and below cursor when printfile() is
// called (i.e. when execution stops, listCommand is used, etc)
SourceListLineCount *int `yaml:"source-list-line-count,omitempty"`

// DebugFileDirectories is the list of directories Delve will use
// in order to resolve external debug info files.
DebugInfoDirectories []string `yaml:"debug-info-directories"`
}

func (c *Config) GetSourceListLineCount() int {
n := 5 // default value
lcp := c.SourceListLineCount
if lcp != nil && *lcp >= 0 {
n = *lcp
}
return n
}

// LoadConfig attempts to populate a Config object from the config.yml file.
func LoadConfig() *Config {
err := createConfigPath()
Expand Down Expand Up @@ -204,6 +217,10 @@ func writeDefaultConfig(f *os.File) error {
# dark blue) See https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit
# source-list-line-color: 34
# Uncomment to change the number of lines printed above and below cursor when
# listing source code.
# source-list-line-count: 5
# Provided aliases will be added to the default aliases for a given command.
aliases:
# command: ["alias1", "alias2"]
Expand Down
8 changes: 5 additions & 3 deletions pkg/terminal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2227,20 +2227,22 @@ func printfile(t *Term, filename string, line int, showArrow bool) error {
fmt.Println("Warning: listing may not match stale executable")
}

lineCount := t.conf.GetSourceListLineCount()

buf := bufio.NewScanner(file)
l := line
for i := 1; i < l-5; i++ {
for i := 1; i < l-lineCount; i++ {
if !buf.Scan() {
return nil
}
}

s := l - 5
s := l - lineCount
if s < 1 {
s = 1
}

for i := s; i <= l+5; i++ {
for i := s; i <= l+lineCount; i++ {
if !buf.Scan() {
return nil
}
Expand Down

0 comments on commit 71a460f

Please sign in to comment.