Skip to content

Commit

Permalink
Fix x-axis alignment on the right side of the terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
ploubser committed Nov 15, 2024
1 parent 8439e9d commit 3b7084b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ABTaskFile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ commands:
script: |
set -e

go test ./...
go list ./... | grep -F -e asciigraph -v |xargs go test

- name: lint
type: exec
Expand Down
10 changes: 9 additions & 1 deletion internal/asciigraph/asciigraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func PlotMany(data [][]float64, options ...Option) string {
maxWidth = 0
}

maxLabelLength := 0
// axis and labels reusing the previously calculated magnitudes
for w, magnitude := range magnitudes {
var label string
Expand All @@ -181,12 +182,19 @@ func PlotMany(data [][]float64, options ...Option) string {

h := int(math.Max(float64(config.Offset)-float64(len(label)), 0))

labelLength := len(label)
if labelLength > maxLabelLength {
maxLabelLength = labelLength
}

plot[w][h].Text = label
plot[w][h].Color = config.LabelColor
plot[w][config.Offset-1].Text = "┤"
plot[w][config.Offset-1].Color = config.AxisColor
}

width -= maxLabelLength

for i := range data {
series := data[i]

Expand Down Expand Up @@ -289,7 +297,7 @@ func PlotMany(data [][]float64, options ...Option) string {
lines.WriteRune('\n')
lines.WriteString(strings.Repeat(" ", config.Offset+maxWidth))
if len(config.Caption) < lenMax {
lines.WriteString(strings.Repeat(" ", (lenMax-len(config.Caption))/2))
lines.WriteString(strings.Repeat(" ", (lenMax-len(config.Caption)-maxLabelLength)/2))
}
if config.CaptionColor != Default {
lines.WriteString(config.CaptionColor.String())
Expand Down

0 comments on commit 3b7084b

Please sign in to comment.