Skip to content

Commit

Permalink
feat: show nodekit version within the TUI
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Feher <[email protected]>
  • Loading branch information
Tasos Bitsios and PhearZero committed Feb 12, 2025
1 parent 1e82c0a commit 9183a54
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var bootstrapCmd = &cobra.Command{
// Execute the TUI if we are caught up.
// TODO: check the delta to see if it is necessary,
if resp.JSON200.CatchupTime == 0 {
err = runTUI(RootCmd, dir, false)
err = runTUI(RootCmd, dir, false, RootCmd.Version)

Check warning on line 92 in cmd/bootstrap.go

View check run for this annotation

Codecov / codecov/patch

cmd/bootstrap.go#L92

Added line #L92 was not covered by tests
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -230,6 +230,6 @@ var bootstrapCmd = &cobra.Command{

}

return runTUI(RootCmd, dataDir, false)
return runTUI(RootCmd, dataDir, false, RootCmd.Version)

Check warning on line 233 in cmd/bootstrap.go

View check run for this annotation

Codecov / codecov/patch

cmd/bootstrap.go#L233

Added line #L233 was not covered by tests
},
}
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var (
},
Run: func(cmd *cobra.Command, args []string) {
log.SetOutput(cmd.OutOrStdout())
err := runTUI(cmd, algodData, IncentivesDisabled)
err := runTUI(cmd, algodData, IncentivesDisabled, cmd.Version)

Check warning on line 59 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L59

Added line #L59 was not covered by tests
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func Execute(version string, needsUpgrade bool) error {
return RootCmd.Execute()
}

func runTUI(cmd *cobra.Command, dataDir string, incentivesFlag bool) error {
func runTUI(cmd *cobra.Command, dataDir string, incentivesFlag bool, version string) error {

Check warning on line 119 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L119

Added line #L119 was not covered by tests
if cmd == nil {
return fmt.Errorf("cmd is nil")
}
Expand All @@ -128,7 +128,7 @@ func runTUI(cmd *cobra.Command, dataDir string, incentivesFlag bool) error {
cobra.CheckErr(err)

// Fetch the state and handle any creation errors
state, stateResponse, err := algod.NewStateModel(ctx, client, httpPkg, incentivesFlag)
state, stateResponse, err := algod.NewStateModel(ctx, client, httpPkg, incentivesFlag, version)

Check warning on line 131 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L131

Added line #L131 was not covered by tests
utils.WithInvalidResponsesExplanations(err, stateResponse, cmd.UsageString())
cobra.CheckErr(err)

Expand Down
6 changes: 5 additions & 1 deletion internal/algod/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
// including status, metrics, accounts, keys, and other configurations.
type StateModel struct {

// Version indicates the version of the application.
Version string

// Status represents the current status of the algod node,
// including network state and round information.
Status Status
Expand Down Expand Up @@ -56,7 +59,7 @@ type StateModel struct {

// NewStateModel initializes and returns a new StateModel instance
// along with an API response and potential error.
func NewStateModel(ctx context.Context, client api.ClientWithResponsesInterface, httpPkg api.HttpPkgInterface, incentivesDisabled bool) (*StateModel, api.ResponseInterface, error) {
func NewStateModel(ctx context.Context, client api.ClientWithResponsesInterface, httpPkg api.HttpPkgInterface, incentivesDisabled bool, version string) (*StateModel, api.ResponseInterface, error) {

Check warning on line 62 in internal/algod/state.go

View check run for this annotation

Codecov / codecov/patch

internal/algod/state.go#L62

Added line #L62 was not covered by tests
// Preload the node status
status, response, err := NewStatus(ctx, client, httpPkg)
if err != nil {
Expand All @@ -79,6 +82,7 @@ func NewStateModel(ctx context.Context, client api.ClientWithResponsesInterface,
Admin: true,
Watching: true,

Version: version,

Check warning on line 85 in internal/algod/state.go

View check run for this annotation

Codecov / codecov/patch

internal/algod/state.go#L85

Added line #L85 was not covered by tests
Client: client,
HttpPkg: httpPkg,
Context: ctx,
Expand Down
1 change: 1 addition & 0 deletions ui/internal/test/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

func GetState(client api.ClientWithResponsesInterface) *algod.StateModel {
sm := &algod.StateModel{
Version: "vTest",
Status: algod.Status{
State: algod.StableState,
Version: "v-test",
Expand Down
21 changes: 13 additions & 8 deletions ui/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,19 @@ func (m StatusViewModel) View() string {

row3 := lipgloss.JoinHorizontal(lipgloss.Left, beginning, middle, end)

return style.WithTitle("Status", style.ApplyBorder(max(0, size-2), 5, "5").Render(
lipgloss.JoinVertical(lipgloss.Left,
row1,
"",
style.Cyan.Render(" -- "+strconv.Itoa(m.Data.Metrics.Window)+" round average --"),
row2,
row3,
)))
return style.WithTitles(
"( "+style.Red.Render(fmt.Sprintf("Nodekit-%s", m.Data.Version))+" )",
lipgloss.NewStyle().Foreground(lipgloss.Color("5")).Render("Status"),
style.ApplyBorder(max(0, size-2), 5, "5").Render(
lipgloss.JoinVertical(lipgloss.Left,
row1,
"",
style.Cyan.Render(" -- "+strconv.Itoa(m.Data.Metrics.Window)+" round average --"),
row2,
row3,
),
),
)
}

// MakeStatusViewModel constructs the model to be used in a tea.Program
Expand Down
3 changes: 3 additions & 0 deletions ui/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var statusViewSnapshots = map[string]StatusViewModel{
"Syncing": {
Data: &algod.StateModel{
Version: "v0.0.0-test",
Status: algod.Status{
LastRound: 1337,
NeedsUpdate: true,
Expand All @@ -31,6 +32,7 @@ var statusViewSnapshots = map[string]StatusViewModel{
},
"Hidden": {
Data: &algod.StateModel{
Version: "v0.0.0-test",
Status: algod.Status{
LastRound: 1337,
NeedsUpdate: true,
Expand All @@ -47,6 +49,7 @@ var statusViewSnapshots = map[string]StatusViewModel{
},
"Loading": {
Data: &algod.StateModel{
Version: "v0.0.0-test",
Status: algod.Status{
LastRound: 1337,
NeedsUpdate: true,
Expand Down
24 changes: 24 additions & 0 deletions ui/style/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ func WithTitle(title string, view string) string {
}
return view
}
func WithTitles(leftText string, rightText string, view string) string {
if leftText == "" || rightText == "" {
return view
}

Check warning on line 65 in ui/style/style.go

View check run for this annotation

Codecov / codecov/patch

ui/style/style.go#L64-L65

Added lines #L64 - L65 were not covered by tests

pad := 4
controlWidth := lipgloss.Width(leftText) + lipgloss.Width(rightText)

lines := strings.Split(view, "\n")

if lipgloss.Width(view) >= controlWidth+(2*pad) {
line := lines[0]
lineWidth := lipgloss.Width(line)
lineLeft := ansi.Truncate(line, pad, "") + leftText
lineRight := rightText + TruncateLeft(line, lineWidth-pad)

midTemplate := TruncateLeft(line, pad)
midLen := lineWidth - lipgloss.Width(lineLeft) - lipgloss.Width(lineRight)
midLine := ansi.Truncate(midTemplate, midLen, "")
lines[0] = lineLeft + midLine + lineRight
}
return strings.Join(lines, "\n")
}

func WithControls(nav string, view string) string {
if nav == "" {
return view
Expand Down
2 changes: 1 addition & 1 deletion ui/testdata/Test_StatusSnapshot/Syncing.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
╭──Status────────────────────────────────────────────────────────────────────────────────╮
╭───( Nodekit-v0.0.0-test )─────────────────────────────────────────────────────Status───╮
│ Latest Round: 1337 SYNCING │
│ │
│ -- 0 round average -- │
Expand Down

0 comments on commit 9183a54

Please sign in to comment.