Skip to content

Commit

Permalink
feat: count disagreeing on progressbar + add colors (#1)
Browse files Browse the repository at this point in the history
* chore: refactor progress bar serialisation

* feat: allow increasing/decreasing columns count

* fix: count disagreeing on progressbar

* feat: change colors
  • Loading branch information
freak12techno authored Nov 20, 2023
1 parent 62d6788 commit 7e9be92
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 17 deletions.
6 changes: 5 additions & 1 deletion pkg/display/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func NewTableData(columnsCount int) *TableData {
}
}

func (d *TableData) SetColumnsCount(count int) {
d.ColumnsCount = count
}

func (d *TableData) GetCell(row, column int) *tview.TableCell {
index := row*d.ColumnsCount + column
text := ""
Expand All @@ -32,7 +36,7 @@ func (d *TableData) GetCell(row, column int) *tview.TableCell {
cell := tview.NewTableCell(text)

if index < len(d.Validators) && d.Validators[index].Validator.IsProposer {
cell.SetBackgroundColor(tcell.ColorPeachPuff)
cell.SetBackgroundColor(tcell.ColorForestGreen)
}

return cell
Expand Down
31 changes: 26 additions & 5 deletions pkg/display/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

const (
ColumnsAmount = 3
RowsAmount = 10
DebugBlockHeight = 2
DefaultColumnsCount = 3
RowsAmount = 10
DebugBlockHeight = 2
)

type Wrapper struct {
Expand All @@ -30,6 +30,7 @@ type Wrapper struct {
HelpModal *tview.Modal

InfoBlockWidth int
ColumnsCount int

DebugEnabled bool

Expand All @@ -42,7 +43,7 @@ type Wrapper struct {
}

func NewWrapper(logger zerolog.Logger, pauseChannel chan bool, appVersion string) *Wrapper {
tableData := NewTableData(ColumnsAmount)
tableData := NewTableData(DefaultColumnsCount)

helpTextBytes, _ := static.TemplatesFs.ReadFile("help.txt")
helpText := strings.ReplaceAll(string(helpTextBytes), "{{ Version }}", appVersion)
Expand Down Expand Up @@ -93,6 +94,7 @@ func NewWrapper(logger zerolog.Logger, pauseChannel chan bool, appVersion string
Logger: logger.With().Str("component", "display_wrapper").Logger(),
DebugEnabled: false,
InfoBlockWidth: 2,
ColumnsCount: DefaultColumnsCount,
PauseChannel: pauseChannel,
IsPaused: false,
IsHelpDisplayed: false,
Expand Down Expand Up @@ -121,6 +123,14 @@ func (w *Wrapper) Start() {
w.ToggleHelp()
}

if event.Rune() == 'm' {
w.ChangeColumnsCount(true)
}

if event.Rune() == 'l' {
w.ChangeColumnsCount(false)
}

if event.Rune() == 'p' {
w.IsPaused = !w.IsPaused
w.PauseChannel <- w.IsPaused
Expand Down Expand Up @@ -177,7 +187,6 @@ func (w *Wrapper) SetState(state *types.State) {
fmt.Fprint(w.ProgressTextView, state.SerializePrevotesProgressbar(width, height/2))
fmt.Fprint(w.ProgressTextView, "\n")
fmt.Fprint(w.ProgressTextView, state.SerializePrecommitsProgressbar(width, height/2))
w.ProgressTextView.Highlight("progress")

w.App.Draw()
}
Expand All @@ -197,6 +206,18 @@ func (w *Wrapper) ChangeInfoBlockHeight(increase bool) {
w.Redraw()
}

func (w *Wrapper) ChangeColumnsCount(increase bool) {
if increase {
w.ColumnsCount++
} else if !increase && w.ColumnsCount-1 >= 1 {
w.ColumnsCount--
}

w.TableData.SetColumnsCount(w.ColumnsCount)

w.Redraw()
}

func (w *Wrapper) Redraw() {
w.Grid.RemoveItem(w.ConsensusInfoTextView)
w.Grid.RemoveItem(w.ChainInfoTextView)
Expand Down
25 changes: 16 additions & 9 deletions pkg/types/progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,27 @@ func (p ProgressBar) Serialize() string {
percentTextStart := (p.Width - len(percentText)) / 2
percentTextLine := (p.Height+1)/2 - 1

isColored := func(value bool) string {
if value {
return "green"
}

return "-"
}

for lineIndex := 0; lineIndex < p.Height; lineIndex++ {
line := "[\"progress\"]"
terminated := false
line := ""
format := ""

for index := 0; index < p.Width; index++ {
percent := int(float64(index) / float64(p.Width) * 100)

if percent > p.Progress && !terminated {
terminated = true
line += "[\"\"]"
isBackgroundColored := percent <= p.Progress

newFormat := fmt.Sprintf("[white:%s]", isColored(isBackgroundColored))
if format != newFormat {
format = newFormat
line += format
}

if lineIndex == percentTextLine && (index >= percentTextStart && index < percentTextStart+len(percentText)) {
Expand All @@ -38,10 +49,6 @@ func (p ProgressBar) Serialize() string {
}
}

if !terminated {
line += "[\"\"]"
}

sb.WriteString(line + "\n")
}

Expand Down
24 changes: 22 additions & 2 deletions pkg/types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ func (s *State) SerializeConsensus() string {
s.Validators.GetTotalVotingPowerPrecommittedPercent(true),
s.Validators.GetTotalVotingPowerPrecommittedPercent(false),
))

prevoted := 0
precommitted := 0

for _, validator := range *s.Validators {
if validator.Prevote != VotedNil {
prevoted += 1
}
if validator.Precommit != VotedNil {
precommitted += 1
}
}

sb.WriteString(fmt.Sprintf(
" prevoted/precommitted: %d/%d (out of %d)\n",
prevoted,
precommitted,
len(*s.Validators),
))

sb.WriteString(fmt.Sprintf(" last updated at: %s\n", utils.SerializeTime(time.Now())))

return sb.String()
Expand Down Expand Up @@ -147,7 +167,7 @@ func (s *State) SerializePrevotesProgressbar(width int, height int) string {
return ""
}

prevotePercent := s.Validators.GetTotalVotingPowerPrevotedPercent(false)
prevotePercent := s.Validators.GetTotalVotingPowerPrevotedPercent(true)
prevotePercentFloat, _ := prevotePercent.Float64()
prevotePercentInt := int(prevotePercentFloat)

Expand All @@ -159,7 +179,7 @@ func (s *State) SerializePrecommitsProgressbar(width int, height int) string {
return ""
}

precommitPercent := s.Validators.GetTotalVotingPowerPrecommittedPercent(false)
precommitPercent := s.Validators.GetTotalVotingPowerPrecommittedPercent(true)
precommitPercentFloat, _ := precommitPercent.Float64()
precommitPercentInt := int(precommitPercentFloat)

Expand Down
1 change: 1 addition & 0 deletions static/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Created by 🐹 Quokka Stake with ❤️.
You can use the following shortcuts:
- enable [d[]ebug panel
- make the info blocks above [b[]igger or [s[]maller
- display [m[]more or [l[]ess columns in validators table
- display or hide this [h[]elp message
- [p[]ause new updates
- [q[]uit the app (or Ctrl+C)

0 comments on commit 7e9be92

Please sign in to comment.