Skip to content

Commit

Permalink
feat: allow disabling emojis (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Feb 12, 2024
1 parent 5bf436e commit f3465cd
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 36 deletions.
1 change: 1 addition & 0 deletions cmd/tmtop.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func main() {
rootCmd.PersistentFlags().StringVar(&config.ConsumerChainID, "consumer-chain-id", "", "Consumer chain ID")
rootCmd.PersistentFlags().DurationVar(&config.RefreshRate, "refresh-rate", time.Second, "Refresh rate")
rootCmd.PersistentFlags().BoolVar(&config.Verbose, "verbose", false, "Display more debug logs")
rootCmd.PersistentFlags().BoolVar(&config.DisableEmojis, "disable-emojis", false, "Disable emojis in output")
rootCmd.PersistentFlags().StringVar(&config.ChainType, "chain-type", "cosmos", "Chain type. Allowed values are: 'cosmos', 'tendermint'")
rootCmd.PersistentFlags().DurationVar(&config.ValidatorsRefreshRate, "validators-refresh-rate", time.Minute, "Validators refresh rate")
rootCmd.PersistentFlags().DurationVar(&config.ChainInfoRefreshRate, "chain-info-refresh-rate", 5*time.Minute, "Chain info refresh rate")
Expand Down
2 changes: 1 addition & 1 deletion pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewApp(config configPkg.Config, version string) *App {
Version: version,
Config: config,
Aggregator: aggregator.NewAggregator(config, logger),
DisplayWrapper: display.NewWrapper(logger, pauseChannel, version),
DisplayWrapper: display.NewWrapper(config, logger, pauseChannel, version),
State: types.NewState(),
LogChannel: logChannel,
PauseChannel: pauseChannel,
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {
BlockTimeRefreshRate time.Duration
ChainType string
Verbose bool
DisableEmojis bool
}

func (c Config) GetProviderOrConsumerHost() string {
Expand Down
10 changes: 6 additions & 4 deletions pkg/display/all_rounds_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
type AllRoundsTableData struct {
tview.TableContentReadOnly

Validators types.ValidatorsWithInfoAndAllRoundVotes
Validators types.ValidatorsWithInfoAndAllRoundVotes
DisableEmojis bool
}

func NewAllRoundsTableData() *AllRoundsTableData {
func NewAllRoundsTableData(disableEmojis bool) *AllRoundsTableData {
return &AllRoundsTableData{
Validators: types.ValidatorsWithInfoAndAllRoundVotes{},
Validators: types.ValidatorsWithInfoAndAllRoundVotes{},
DisableEmojis: disableEmojis,
}
}

Expand All @@ -44,7 +46,7 @@ func (d *AllRoundsTableData) GetCell(row, column int) *tview.TableCell {

roundVotes := d.Validators.RoundsVotes[column-1]
roundVote := roundVotes[row-1]
text := roundVote.Serialize()
text := roundVote.Serialize(d.DisableEmojis)

cell := tview.NewTableCell(text)

Expand Down
14 changes: 8 additions & 6 deletions pkg/display/last_round_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
type LastRoundTableData struct {
tview.TableContentReadOnly

Validators types.ValidatorsWithInfo
ColumnsCount int
Validators types.ValidatorsWithInfo
ColumnsCount int
DisableEmojis bool
}

func NewLastRoundTableData(columnsCount int) *LastRoundTableData {
func NewLastRoundTableData(columnsCount int, disableEmojis bool) *LastRoundTableData {
return &LastRoundTableData{
ColumnsCount: columnsCount,
Validators: make(types.ValidatorsWithInfo, 0),
ColumnsCount: columnsCount,
Validators: make(types.ValidatorsWithInfo, 0),
DisableEmojis: disableEmojis,
}
}

Expand All @@ -30,7 +32,7 @@ func (d *LastRoundTableData) GetCell(row, column int) *tview.TableCell {
text := ""

if index < len(d.Validators) {
text = d.Validators[index].Serialize()
text = d.Validators[index].Serialize(d.DisableEmojis)
}

cell := tview.NewTableCell(text)
Expand Down
33 changes: 21 additions & 12 deletions pkg/display/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package display

import (
"fmt"
configPkg "main/pkg/config"
"main/pkg/types"
"main/static"
"strings"
Expand Down Expand Up @@ -49,11 +50,18 @@ type Wrapper struct {
IsPaused bool

IsHelpDisplayed bool

DisableEmojis bool
}

func NewWrapper(logger zerolog.Logger, pauseChannel chan bool, appVersion string) *Wrapper {
lastRoundTableData := NewLastRoundTableData(DefaultColumnsCount)
allRoundsTableData := NewAllRoundsTableData()
func NewWrapper(
config configPkg.Config,
logger zerolog.Logger,
pauseChannel chan bool,
appVersion string,
) *Wrapper {
lastRoundTableData := NewLastRoundTableData(DefaultColumnsCount, config.DisableEmojis)
allRoundsTableData := NewAllRoundsTableData(config.DisableEmojis)

helpTextBytes, _ := static.TemplatesFs.ReadFile("help.txt")
helpText := strings.ReplaceAll(string(helpTextBytes), "{{ Version }}", appVersion)
Expand Down Expand Up @@ -116,6 +124,7 @@ func NewWrapper(logger zerolog.Logger, pauseChannel chan bool, appVersion string
PauseChannel: pauseChannel,
IsPaused: false,
IsHelpDisplayed: false,
DisableEmojis: config.DisableEmojis,
}
}

Expand Down Expand Up @@ -171,9 +180,9 @@ func (w *Wrapper) Start() {

w.Redraw()

fmt.Fprint(w.ChainInfoTextView, "Loading...")
fmt.Fprint(w.ConsensusInfoTextView, "Loading...")
fmt.Fprint(w.ProgressTextView, "Loading...")
_, _ = fmt.Fprint(w.ChainInfoTextView, "Loading...")
_, _ = fmt.Fprint(w.ConsensusInfoTextView, "Loading...")
_, _ = fmt.Fprint(w.ProgressTextView, "Loading...")

w.App.SetBeforeDrawFunc(func(screen tcell.Screen) bool {
screen.Clear()
Expand Down Expand Up @@ -204,19 +213,19 @@ func (w *Wrapper) SetState(state *types.State) {
w.ConsensusInfoTextView.Clear()
w.ChainInfoTextView.Clear()
w.ProgressTextView.Clear()
fmt.Fprint(w.ConsensusInfoTextView, state.SerializeConsensus())
fmt.Fprint(w.ChainInfoTextView, state.SerializeChainInfo())
_, _ = fmt.Fprint(w.ConsensusInfoTextView, state.SerializeConsensus())
_, _ = fmt.Fprint(w.ChainInfoTextView, state.SerializeChainInfo())

_, _, width, height := w.ConsensusInfoTextView.GetInnerRect()
fmt.Fprint(w.ProgressTextView, state.SerializePrevotesProgressbar(width, height/2))
fmt.Fprint(w.ProgressTextView, "\n")
fmt.Fprint(w.ProgressTextView, state.SerializePrecommitsProgressbar(width, height/2))
_, _ = fmt.Fprint(w.ProgressTextView, state.SerializePrevotesProgressbar(width, height/2))
_, _ = fmt.Fprint(w.ProgressTextView, "\n")
_, _ = fmt.Fprint(w.ProgressTextView, state.SerializePrecommitsProgressbar(width, height/2))

w.App.Draw()
}

func (w *Wrapper) DebugText(text string) {
fmt.Fprint(w.DebugTextView, text)
_, _ = fmt.Fprint(w.DebugTextView, text)
w.DebugTextView.ScrollToEnd()
}

Expand Down
18 changes: 11 additions & 7 deletions pkg/types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ type RoundVote struct {
IsProposer bool
}

func (v RoundVote) Serialize() string {
func (v RoundVote) Serialize(disableEmojis bool) string {
return fmt.Sprintf(
" %s %s",
v.Prevote.Serialize(),
v.Precommit.Serialize(),
v.Prevote.Serialize(disableEmojis),
v.Precommit.Serialize(disableEmojis),
)
}

Expand Down Expand Up @@ -106,19 +106,23 @@ type ValidatorWithInfo struct {
ChainValidator *ChainValidator
}

func (v ValidatorWithInfo) Serialize() string {
func (v ValidatorWithInfo) Serialize(disableEmojis bool) string {
name := v.Validator.Address
if v.ChainValidator != nil {
name = v.ChainValidator.Moniker
if v.ChainValidator.AssignedAddress != "" {
name = "🔑 " + name
emoji := "🔑"
if disableEmojis {
emoji = "[k[]"
}
name = emoji + " " + name
}
}

return fmt.Sprintf(
" %s %s %s %s%% %s ",
v.RoundVote.Prevote.Serialize(),
v.RoundVote.Precommit.Serialize(),
v.RoundVote.Prevote.Serialize(disableEmojis),
v.RoundVote.Precommit.Serialize(disableEmojis),
utils.RightPadAndTrim(strconv.Itoa(v.Validator.Index+1), 3),
utils.RightPadAndTrim(fmt.Sprintf("%.2f", v.Validator.VotingPowerPercent), 6),
utils.LeftPadAndTrim(name, 25),
Expand Down
22 changes: 16 additions & 6 deletions pkg/types/vote.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package types

import loggerPkg "main/pkg/logger"

type Vote int

const (
Expand All @@ -10,16 +8,28 @@ const (
VotedZero
)

func (v Vote) Serialize() string {
func (v Vote) Serialize(disableEmojis bool) string {
if disableEmojis {
switch v {
case Voted:
return "[X[]"
case VotedZero:
return "[0[]"
case VotedNil:
return "[ []"
default:
return ""
}
}

switch v {
case Voted:
return "✅"
case VotedZero:
return "🤷"
case VotedNil:
return "❌"
default:
return ""
}

loggerPkg.GetDefaultLogger().Fatal().Str("value", string(rune(v))).Msg("Error parsing vote")
return ""
}

0 comments on commit f3465cd

Please sign in to comment.