Skip to content

Commit

Permalink
feat: added total cost to summary line
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Mar 4, 2021
1 parent a64b17c commit eb45192
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
53 changes: 43 additions & 10 deletions internal/ui/component/summary/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package summary
import (
"strings"

grid "github.com/achannarasappa/term-grid"
"github.com/achannarasappa/ticker/internal/position"
. "github.com/achannarasappa/ticker/internal/ui/util"
"github.com/muesli/reflow/ansi"
)

type Model struct {
Expand All @@ -25,16 +27,47 @@ func (m Model) View() string {
return ""
}

return strings.Join([]string{
StyleNeutralFaded("Day:"),
quoteChangeText(m.Summary.DayChange, m.Summary.DayChangePercent),
StyleNeutralFaded("•"),
StyleNeutralFaded("Change:"),
quoteChangeText(m.Summary.Change, m.Summary.ChangePercent),
StyleNeutralFaded("•"),
StyleNeutralFaded("Value:"),
ValueText(m.Summary.Value),
}, " ") + "\n" + StyleLine(strings.Repeat("━", m.Width))
textChange := StyleNeutralFaded("Day Change: ") + quoteChangeText(m.Summary.DayChange, m.Summary.DayChangePercent) +
StyleNeutralFaded(" • ") +
StyleNeutralFaded("Change: ") + quoteChangeText(m.Summary.Change, m.Summary.ChangePercent)
widthChange := ansi.PrintableRuneWidth(textChange)
textValue := StyleNeutralFaded(" • ") +
StyleNeutralFaded("Value: ") + ValueText(m.Summary.Value)
widthValue := ansi.PrintableRuneWidth(textValue)
textCost := StyleNeutralFaded(" • ") +
StyleNeutralFaded("Cost: ") + ValueText(m.Summary.Cost)
widthCost := ansi.PrintableRuneWidth(textValue)

return grid.Render(grid.Grid{
Rows: []grid.Row{
{
Width: m.Width,
Cells: []grid.Cell{
{
Text: textChange,
Width: widthChange,
},
{
Text: textValue,
Width: widthValue,
VisibleMinWidth: widthChange + widthValue,
},
{
Text: textCost,
Width: widthCost,
VisibleMinWidth: widthChange + widthValue + widthCost,
},
},
},
{
Width: m.Width,
Cells: []grid.Cell{
{Text: StyleLine(strings.Repeat("━", m.Width))},
},
},
},
GutterHorizontal: 1,
})

}

Expand Down
12 changes: 7 additions & 5 deletions internal/ui/component/summary/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var _ = Describe("Summary", func() {
When("the change is positive", func() {
It("should render a summary with up arrow", func() {
m := NewModel()
m.Width = 120
m.Summary = position.PositionSummary{
Value: 10000,
Cost: 1000,
Expand All @@ -29,15 +30,16 @@ var _ = Describe("Summary", func() {
DayChangePercent: 10.0,
}
Expect(removeFormatting(m.View())).To(Equal(strings.Join([]string{
"Day: ↑ 100.00 (10.00%) • Change: ↑ 9000.00 (1000.00%) • Value: 10000.00",
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
"Day Change: ↑ 100.00 (10.00%) • Change: ↑ 9000.00 (1000.00%) • Value: 10000.00 • Cost: 1000.00 ",
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
}, "\n")))
})
})

When("the change is negative", func() {
It("should render a summary with down arrow", func() {
m := NewModel()
m.Width = 120
m.Summary = position.PositionSummary{
Value: 1000,
Cost: 10000,
Expand All @@ -47,8 +49,8 @@ var _ = Describe("Summary", func() {
DayChangePercent: -10.0,
}
Expect(removeFormatting(m.View())).To(Equal(strings.Join([]string{
"Day: ↓ -100.00 (-10.00%) • Change: ↓ -9000.00 (-1000.00%) • Value: 1000.00",
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
"Day Change: ↓ -100.00 (-10.00%) • Change: ↓ -9000.00 (-1000.00%) • Value: 1000.00 • Cost: 10000.00",
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
}, "\n")))
})
})
Expand All @@ -57,7 +59,7 @@ var _ = Describe("Summary", func() {
It("should render an empty summary", func() {
m := NewModel()
Expect(removeFormatting(m.View())).To(Equal(strings.Join([]string{
"Day: 0.00 (0.00%) • Change: 0.00 (0.00%) • Value: ",
"Day Change: 0.00 (0.00%) • Change: 0.00 (0.00%) • Value: • Cost: ",
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
}, "\n")))
})
Expand Down

0 comments on commit eb45192

Please sign in to comment.