Skip to content

Commit

Permalink
Merge pull request #5 from muffix/fix-separators
Browse files Browse the repository at this point in the history
Fix separators
  • Loading branch information
muffix committed Jun 24, 2020
2 parents 85937f4 + 0d4e38d commit be4ebf0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
38 changes: 23 additions & 15 deletions internal/bitbar/bitbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (p *Printer) Interruptions(interruptions []interruption.Interruption, err e
func (p *Printer) addInterruption(i interruption.Interruption) {
p.count++

p.writeln("", "---")
p.writeSeparator()
p.writeln("Updated", i.ModificationDate.String())
if len(i.Lines) > 0 {
p.writeln("Affected lines", i.Lines.String())
Expand All @@ -49,6 +49,10 @@ func (p *Printer) addInterruption(i interruption.Interruption) {
}
}

func (p *Printer) writeSeparator() {
p.builder.WriteString("---\n")
}

func (p *Printer) writeln(label, text string) {
if !strings.HasSuffix(text, "\n") {
text += "\n"
Expand All @@ -66,7 +70,7 @@ func (p *Printer) writeln(label, text string) {
// Print writes the previously added Interruptions out to a writer
func (p *Printer) Print(w io.Writer) {
if p.err != nil {
_, _ = fmt.Fprintf(w, "🚇⚠️\n%s", p.err)
_, _ = fmt.Fprintf(w, "🚇⚠️\n---\n%s", p.err)
return
}

Expand All @@ -82,20 +86,24 @@ func (p *Printer) trimLineLength(text string) string {
linesIn := strings.Split(text, "\n")

for _, lineIn := range linesIn {
var shortLines []string
words := strings.Fields(lineIn)

var currentLine string
for _, word := range words {
if len(word)+len(currentLine) > p.MaxLineLength {
shortLines = append(shortLines, strings.TrimSpace(currentLine))
currentLine = ""
}
currentLine = fmt.Sprintf("%s %s", currentLine, word)
}
shortLines = append(shortLines, strings.TrimSpace(currentLine))
shortLinesOut = append(shortLinesOut, strings.Join(shortLines, "\n"))
shortLinesOut = append(shortLinesOut, strings.Join(p.reshapeLineIntoMaxLength(lineIn), "\n"))
}

return strings.Join(shortLinesOut, "\n")
}

func (p *Printer) reshapeLineIntoMaxLength(line string) []string {
var shortLines []string
words := strings.Fields(line)

var currentLine string
for _, word := range words {
if len(word)+len(currentLine) > p.MaxLineLength {
shortLines = append(shortLines, strings.TrimSpace(currentLine))
currentLine = ""
}
currentLine = fmt.Sprintf("%s %s", currentLine, word)
}
shortLines = append(shortLines, strings.TrimSpace(currentLine))
return shortLines
}
24 changes: 21 additions & 3 deletions internal/bitbar/bitbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bitbar

import (
"bytes"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -72,17 +73,17 @@ func TestPrinter_Print(t *testing.T) {
},
},
`🚇3️
--- | trim=false
---
Updated: Wed Jan 1 10:00:00 UTC | trim=false
Affected lines: U1, 42, X999 | trim=false
Important message | trim=false
Simple text | trim=false
Duration: Some time | trim=false
--- | trim=false
---
Updated: Wed Jan 1 10:00:00 UTC | trim=false
Short message | trim=false
Simple text | trim=false
--- | trim=false
---
Updated: Wed Jan 1 10:00:00 UTC | trim=false
Affected lines: U1, 42, X999 | trim=false
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna | trim=false
Expand Down Expand Up @@ -113,3 +114,20 @@ Duration: Some time | trim=false
t.Fatalf("Wrong format printed. Got: \n%s\nWant: \n%s", got, testCase.want)
}
}

func TestPrinter_PrintError(t *testing.T) {
w := &mockWriter{}

underTest := &Printer{MaxLineLength: 120}
underTest.Interruptions(nil, fmt.Errorf("borked"))
underTest.Print(w)

want := `🚇⚠️
---
borked`
got := w.String()

if got != want {
t.Fatalf("Wrong format printed. Got: \n%s\nWant: \n%s", got, want)
}
}

0 comments on commit be4ebf0

Please sign in to comment.