From 046de78b72041eacf32c5f1a2c5b4489c5e59d09 Mon Sep 17 00:00:00 2001 From: Berger Eugene Date: Sat, 20 Jan 2024 19:06:01 +0200 Subject: [PATCH] Issue #128: Use - for empty values to improve TUI columns clarity --- src/app/process.go | 9 ++++- src/tui/proc-table.go | 74 ++++++++++++++++++++++++++++++++----- src/tui/procstate_sorter.go | 6 ++- src/types/process.go | 5 ++- 4 files changed, 78 insertions(+), 16 deletions(-) diff --git a/src/app/process.go b/src/app/process.go index c24eb05..8249903 100644 --- a/src/app/process.go +++ b/src/app/process.go @@ -352,6 +352,7 @@ func (p *Process) onProcessEnd(state string) { } p.stopProbes() p.setState(state) + p.updateProcState() p.Lock() p.done = true @@ -395,13 +396,17 @@ func (p *Process) getCommand() []string { } func (p *Process) updateProcState() { - if p.isRunning() { + isRunning := p.isRunning() + p.stateMtx.Lock() + defer p.stateMtx.Unlock() + if isRunning { dur := time.Since(p.getStartTime()) p.procState.SystemTime = durationToString(dur) p.procState.Age = dur - p.procState.IsRunning = true p.procState.Name = p.getName() } + p.procState.IsRunning = isRunning + } func (p *Process) setStartTime(startTime time.Time) { p.timeMutex.Lock() diff --git a/src/tui/proc-table.go b/src/tui/proc-table.go index f32d501..48970b0 100644 --- a/src/tui/proc-table.go +++ b/src/tui/proc-table.go @@ -11,6 +11,19 @@ import ( "time" ) +type tableRowValues struct { + icon string + iconColor tcell.Color + pid string + name string + ns string + status string + age string + health string + restarts string + exitCode string +} + func (pv *pcView) fillTableData() { if pv.project == nil { return @@ -33,16 +46,8 @@ func (pv *pcView) fillTableData() { pv.procTable.RemoveRow(row) continue } - icon, color := getIconForState(state) - pv.procTable.SetCell(row, int(ProcessStateIcon), tview.NewTableCell(icon).SetAlign(tview.AlignCenter).SetExpansion(0).SetTextColor(color)) - pv.procTable.SetCell(row, int(ProcessStatePid), tview.NewTableCell(strconv.Itoa(state.Pid)).SetAlign(tview.AlignRight).SetExpansion(0).SetTextColor(tcell.ColorLightSkyBlue)) - pv.procTable.SetCell(row, int(ProcessStateName), tview.NewTableCell(state.Name).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(tcell.ColorLightSkyBlue)) - pv.procTable.SetCell(row, int(ProcessStateNamespace), tview.NewTableCell(state.Namespace).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(tcell.ColorLightSkyBlue)) - pv.procTable.SetCell(row, int(ProcessStateStatus), tview.NewTableCell(state.Status).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(tcell.ColorLightSkyBlue)) - pv.procTable.SetCell(row, int(ProcessStateAge), tview.NewTableCell(state.SystemTime).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(tcell.ColorLightSkyBlue)) - pv.procTable.SetCell(row, int(ProcessStateHealth), tview.NewTableCell(state.Health).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(tcell.ColorLightSkyBlue)) - pv.procTable.SetCell(row, int(ProcessStateRestarts), tview.NewTableCell(strconv.Itoa(state.Restarts)).SetAlign(tview.AlignRight).SetExpansion(0).SetTextColor(tcell.ColorLightSkyBlue)) - pv.procTable.SetCell(row, int(ProcessStateExit), tview.NewTableCell(strconv.Itoa(state.ExitCode)).SetAlign(tview.AlignRight).SetExpansion(0).SetTextColor(tcell.ColorLightSkyBlue)) + rowVals := getTableRowValues(state) + setRowValues(pv.procTable, row, rowVals) if state.IsRunning { runningProcCount += 1 } @@ -68,6 +73,18 @@ func (pv *pcView) fillTableData() { } } +func setRowValues(procTable *tview.Table, row int, rowVals tableRowValues) { + procTable.SetCell(row, int(ProcessStateIcon), tview.NewTableCell(rowVals.icon).SetAlign(tview.AlignCenter).SetExpansion(0).SetTextColor(rowVals.iconColor)) + procTable.SetCell(row, int(ProcessStatePid), tview.NewTableCell(rowVals.pid).SetAlign(tview.AlignRight).SetExpansion(0).SetTextColor(tcell.ColorLightSkyBlue)) + procTable.SetCell(row, int(ProcessStateName), tview.NewTableCell(rowVals.name).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(tcell.ColorLightSkyBlue)) + procTable.SetCell(row, int(ProcessStateNamespace), tview.NewTableCell(rowVals.ns).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(tcell.ColorLightSkyBlue)) + procTable.SetCell(row, int(ProcessStateStatus), tview.NewTableCell(rowVals.status).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(tcell.ColorLightSkyBlue)) + procTable.SetCell(row, int(ProcessStateAge), tview.NewTableCell(rowVals.age).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(tcell.ColorLightSkyBlue)) + procTable.SetCell(row, int(ProcessStateHealth), tview.NewTableCell(rowVals.health).SetAlign(tview.AlignLeft).SetExpansion(1).SetTextColor(tcell.ColorLightSkyBlue)) + procTable.SetCell(row, int(ProcessStateRestarts), tview.NewTableCell(rowVals.restarts).SetAlign(tview.AlignRight).SetExpansion(0).SetTextColor(tcell.ColorLightSkyBlue)) + procTable.SetCell(row, int(ProcessStateExit), tview.NewTableCell(rowVals.exitCode).SetAlign(tview.AlignRight).SetExpansion(0).SetTextColor(tcell.ColorLightSkyBlue)) +} + func (pv *pcView) onTableSelectionChange(_, _ int) { name := pv.getSelectedProcName() if len(name) == 0 { @@ -226,3 +243,40 @@ func getIconForState(state types.ProcessState) (string, tcell.Color) { return "●", tcell.ColorLightSkyBlue } } + +func getStrForRestarts(restarts int) string { + if restarts == 0 { + return types.PlaceHolderValue + } + return strconv.Itoa(restarts) +} + +func getStrForExitCode(state types.ProcessState) string { + // running no exit info yet + if state.IsRunning && state.ExitCode == 0 { + return types.PlaceHolderValue + } + // disabled foreground or pending state + if state.Status == types.ProcessStateDisabled || + state.Status == types.ProcessStatePending || + state.Status == types.ProcessStateForeground { + return types.PlaceHolderValue + } + return strconv.Itoa(state.ExitCode) +} + +func getTableRowValues(state types.ProcessState) tableRowValues { + icon, color := getIconForState(state) + return tableRowValues{ + icon: icon, + iconColor: color, + pid: strconv.Itoa(state.Pid), + name: state.Name, + ns: state.Namespace, + status: state.Status, + age: state.SystemTime, + health: state.Health, + restarts: getStrForRestarts(state.Restarts), + exitCode: getStrForExitCode(state), + } +} diff --git a/src/tui/procstate_sorter.go b/src/tui/procstate_sorter.go index 02a220c..e104104 100644 --- a/src/tui/procstate_sorter.go +++ b/src/tui/procstate_sorter.go @@ -140,10 +140,12 @@ func getSorter(sortBy ColumnID, states *types.ProcessesState) sortFn { } case ProcessStateExit: return func(i, j int) bool { - if states.States[i].ExitCode == states.States[j].ExitCode { + eci := getStrForExitCode(states.States[i]) + ecj := getStrForExitCode(states.States[j]) + if eci == ecj { return states.States[i].Name < states.States[j].Name } else { - return states.States[i].ExitCode < states.States[j].ExitCode + return eci < ecj } } case ProcessStateName: diff --git a/src/types/process.go b/src/types/process.go index bad3797..b881aa1 100644 --- a/src/types/process.go +++ b/src/types/process.go @@ -8,6 +8,7 @@ import ( ) const DefaultNamespace = "default" +const PlaceHolderValue = "-" type Processes map[string]ProcessConfig type Environment []string @@ -67,7 +68,7 @@ func NewProcessState(proc *ProcessConfig) *ProcessState { Name: proc.ReplicaName, Namespace: proc.Namespace, Status: ProcessStatePending, - SystemTime: "", + SystemTime: PlaceHolderValue, Age: time.Duration(0), IsRunning: false, Health: ProcessHealthUnknown, @@ -130,7 +131,7 @@ const ( const ( ProcessHealthReady = "Ready" ProcessHealthNotReady = "Not Ready" - ProcessHealthUnknown = "N/A" + ProcessHealthUnknown = PlaceHolderValue ) type RestartPolicyConfig struct {