Refactored ui_single

This commit is contained in:
Ivan Daniluk 2015-05-03 18:03:49 +03:00
parent 6691de274e
commit 44d77e4df4
4 changed files with 51 additions and 24 deletions

View File

@ -136,6 +136,7 @@ func (s Service) Values(name VarName) []int {
return stack.IntValues()
}
// Max returns maximum recorded value for given service and var.
func (s Service) Max(name VarName) interface{} {
val, ok := s.stacks[name]
if !ok {

View File

@ -32,10 +32,10 @@ func TestStack(t *testing.T) {
ints1 := s1.IntValues()
if len(ints1) != 3 {
t.Fatal("expecting len of to be %d, but got %d", 3, len(ints1))
t.Fatalf("expecting len of to be %d, but got %d", 3, len(ints1))
}
if ints1[0] != 1 || ints1[1] != 0 || ints1[2] != 1 {
t.Fatal("bool values converted to int incorrectly: %v", ints1)
t.Fatalf("bool values converted to int incorrectly: %v", ints1)
}
s2 := NewStackWithSize(3)
@ -45,9 +45,9 @@ func TestStack(t *testing.T) {
ints2 := s2.IntValues()
if len(ints2) != 3 {
t.Fatal("expecting len to be %d, but got %d", 3, len(ints2))
t.Fatalf("expecting len to be %d, but got %d", 3, len(ints2))
}
if ints2[0] != 10 || ints2[1] != 50 || ints2[2] != 3 {
t.Fatal("float values converted to int incorrectly: %v", ints2)
t.Fatalf("float values converted to int incorrectly: %v", ints2)
}
}

View File

@ -125,8 +125,19 @@ func (t *TermUI) Update(data UIData) {
}
t.Relayout()
var widgets []termui.Bufferer
widgets = append(widgets, t.Title, t.Status, t.Services, t.Sparkline1)
for _, list := range t.Lists {
widgets = append(widgets, list)
}
if t.Sparkline2 != nil {
widgets = append(widgets, t.Sparkline2)
}
termui.Render(widgets...)
}
// Relayout recalculates widgets sizes and coords.
func (t *TermUI) Relayout() {
tw, th := termui.TermWidth(), termui.TermHeight()
h := th
@ -135,6 +146,9 @@ func (t *TermUI) Relayout() {
firstRowH := 3
t.Title.Height = firstRowH
t.Title.Width = tw / 2
if tw%2 == 1 {
t.Title.Width += 1
}
t.Status.Height = firstRowH
t.Status.Width = tw / 2
t.Status.X = t.Title.X + t.Title.Width
@ -177,20 +191,14 @@ func (t *TermUI) Relayout() {
t.Sparkline1.Width = tw / 2
t.Sparkline2.Width = tw / 2
if tw%2 == 1 {
t.Sparkline2.Width += 1
}
t.Sparkline2.X = t.Sparkline1.X + t.Sparkline1.Width
t.Sparkline2.Height = h
t.Sparkline2.Y = th - h
}
var widgets []termui.Bufferer
widgets = append(widgets, t.Title, t.Status, t.Services, t.Sparkline1)
for _, list := range t.Lists {
widgets = append(widgets, list)
}
if t.Sparkline2 != nil {
widgets = append(widgets, t.Sparkline2)
}
termui.Render(widgets...)
}
// Close shuts down UI module.

View File

@ -61,14 +61,7 @@ func (t *TermUISingle) Init(data UIData) error {
return s
}()
termui.Body.AddRows(
termui.NewRow(
termui.NewCol(6, 0, t.Title),
termui.NewCol(6, 0, t.Status)),
termui.NewRow(termui.NewCol(12, 0, t.Sparkline)),
)
termui.Body.Align()
t.Relayout()
return nil
}
@ -96,9 +89,11 @@ func (t *TermUISingle) Update(data UIData) {
spl.Data = service.Values(name)
}
termui.Body.Width = termui.TermWidth()
termui.Body.Align()
termui.Render(termui.Body)
t.Relayout()
var widgets []termui.Bufferer
widgets = append(widgets, t.Title, t.Status, t.Sparkline)
termui.Render(widgets...)
}
// Close shuts down UI module.
@ -106,6 +101,29 @@ func (t *TermUISingle) Close() {
termui.Close()
}
// Relayout recalculates widgets sizes and coords.
func (t *TermUISingle) Relayout() {
tw, th := termui.TermWidth(), termui.TermHeight()
h := th
// First row: Title and Status pars
firstRowH := 3
t.Title.Height = firstRowH
t.Title.Width = tw / 2
if tw%2 == 1 {
t.Title.Width += 1
}
t.Status.Height = firstRowH
t.Status.Width = tw / 2
t.Status.X = t.Title.X + t.Title.Width
h -= firstRowH
// Second row: Sparklines
t.Sparkline.Width = tw
t.Sparkline.Height = h
t.Sparkline.Y = th - h
}
func formatMax(max interface{}) string {
var str string
if max != nil {