-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
context: use the line object instead of rendered strings (makes elide…
…, subseconds-only mode, etc. work correctly)
- Loading branch information
Showing
5 changed files
with
81 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,54 @@ | ||
package parse | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
type context struct { | ||
Before, After int | ||
|
||
lines []string | ||
lines []line | ||
printAfter int | ||
line int | ||
lastPrint int | ||
} | ||
|
||
func (c *context) Print(buf *bytes.Buffer, msg string, selected bool) { | ||
// Print returns the lines that should be displayed right now, based on the line that is being | ||
// added, its filtering status, and the context configuration. | ||
func (c *context) Print(msg *line, selected bool) []*line { | ||
c.line++ | ||
if selected { | ||
var result []*line | ||
c.printAfter = c.After | ||
if c.lastPrint != 0 && (c.After != 0 || c.Before != 0) && c.line-len(c.lines)-c.lastPrint > 1 { | ||
buf.WriteString("---\n") | ||
|
||
if true && | ||
// suppress separator if it's the first line of output | ||
c.lastPrint != 0 && | ||
// suppress separator if we are no-op context | ||
(c.After != 0 || c.Before != 0) && | ||
// suppress separator if end of after is contigious with the start of before | ||
c.line-len(c.lines)-c.lastPrint > 1 { | ||
|
||
result = append(result, &line{isSeparator: true}) | ||
} | ||
for _, l := range c.lines { | ||
buf.WriteString(l) | ||
line := l | ||
result = append(result, &line) | ||
} | ||
buf.WriteString(msg) | ||
result = append(result, msg) | ||
c.lastPrint = c.line | ||
c.lines = nil | ||
return | ||
c.lines = nil // TODO: allocate full capacity here | ||
return result | ||
} | ||
|
||
if c.printAfter > 0 { | ||
buf.WriteString(msg) | ||
c.lastPrint = c.line | ||
c.printAfter-- | ||
return | ||
return []*line{msg} | ||
} | ||
|
||
if c.Before > 0 { | ||
c.lines = append(c.lines, msg) | ||
// TODO: allocate full capacity here | ||
c.lines = append(c.lines, *msg) // shallow copy | ||
if len(c.lines) > c.Before { | ||
c.lines = c.lines[1:] | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters