Skip to content

Commit 93d6978

Browse files
committed
wip: customizable table cells/rows
1 parent 637715b commit 93d6978

File tree

4 files changed

+93
-19
lines changed

4 files changed

+93
-19
lines changed

cmd/scom/scom.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"github.com/charmbracelet/bubbles/help"
99
"github.com/charmbracelet/bubbles/textinput"
1010
"github.com/charmbracelet/bubbles/viewport"
11-
"github.com/charmbracelet/lipgloss"
1211
tea "github.com/charmbracelet/bubbletea"
12+
"github.com/charmbracelet/lipgloss"
1313

1414
"github.com/CLIP-HPC/SlurmCommander/internal/cmdline"
1515
"github.com/CLIP-HPC/SlurmCommander/internal/command"
@@ -30,7 +30,7 @@ func main() {
3030

3131
var (
3232
debugSet bool = false
33-
args *cmdline.CmdArgs
33+
args *cmdline.CmdArgs
3434
)
3535

3636
fmt.Printf("Welcome to Slurm Commander!\n\n")
@@ -103,11 +103,11 @@ func main() {
103103
},
104104
JobTab: jobtab.JobTab{
105105
SqueueTable: table.New(table.WithColumns(jobtab.SqueueTabCols), table.WithRows(jobtab.TableRows{}), table.WithStyles(s)),
106-
Filter: ti,
106+
Filter: ti,
107107
},
108108
JobHistTab: jobhisttab.JobHistTab{
109109
SacctTable: table.New(table.WithColumns(jobhisttab.SacctTabCols), table.WithRows(jobtab.TableRows{}), table.WithStyles(s)),
110-
Filter: ti,
110+
Filter: ti,
111111
UserInputs: jobhisttab.NewUserInputs(cc.JobHist.Timeout, cc.JobHist.Starttime, cc.JobHist.Endtime),
112112
HistFetched: false,
113113
HistFetchFail: false,
@@ -130,10 +130,14 @@ func main() {
130130
},
131131
ClusterTab: clustertab.ClusterTab{
132132
SinfoTable: table.New(table.WithColumns(clustertab.SinfoTabCols), table.WithRows(jobtab.TableRows{}), table.WithStyles(s)),
133-
Filter: ti,
133+
Filter: ti,
134134
},
135135
}
136136

137+
m.ClusterTab.SinfoTable.CustomCellStyle = new(clustertab.CellStyle)
138+
m.ClusterTab.SinfoTable.CustomRowStyle = new(clustertab.RowStyle)
139+
m.ClusterTab.SinfoTable.CustomSelectedStyle = new(clustertab.SelectedStyle)
140+
137141
138142
//p := tea.NewProgram(tea.Model(m), tea.WithAltScreen())
139143
//if err := p.Start(); err != nil {

internal/model/tabs/clustertab/clustertabtable.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88

99
"github.com/CLIP-HPC/SlurmCommander/internal/command"
1010
"github.com/CLIP-HPC/SlurmCommander/internal/slurm"
11+
"github.com/CLIP-HPC/SlurmCommander/internal/styles"
1112
"github.com/CLIP-HPC/SlurmCommander/internal/table"
13+
"github.com/charmbracelet/lipgloss"
1214
)
1315

1416
const (
@@ -64,6 +66,29 @@ var SinfoTabCols = []table.Column{
6466
type SinfoJSON slurm.SinfoJSON
6567
type TableRows []table.Row
6668

69+
type CellStyle struct{}
70+
type RowStyle struct{}
71+
type SelectedStyle struct{}
72+
73+
func (h CellStyle) Style(i int, r table.Row) lipgloss.Style {
74+
switch r[i] {
75+
case "down":
76+
return styles.TextRed
77+
case "mixed":
78+
return styles.TextOrange
79+
default:
80+
return lipgloss.NewStyle()
81+
}
82+
}
83+
84+
func (h RowStyle) Style(i int, r table.Row) lipgloss.Style {
85+
return lipgloss.NewStyle()
86+
}
87+
88+
func (h SelectedStyle) Style(i int, r table.Row) lipgloss.Style {
89+
return styles.SelectedRow
90+
}
91+
6792
func (siJson *SinfoJSON) FilterSinfoTable(f string, l *log.Logger) (*TableRows, *SinfoJSON, *command.ErrorMsg) {
6893
var (
6994
siTabRows = TableRows{}

internal/styles/styles.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@ import (
55
)
66

77
var (
8-
// Blue
9-
Blue = lipgloss.Color("#0057b7")
10-
// Yellow
8+
Blue = lipgloss.Color("#0057b7")
119
Yellow = lipgloss.Color("#ffd700")
12-
// Red
13-
Red = lipgloss.Color("#cc0000")
14-
//red = lipgloss.Color("#b30000")
15-
//Green = lipgloss.Color("#009900")
16-
Green = lipgloss.Color("#00b300")
10+
Red = lipgloss.Color("#cc0000")
11+
Green = lipgloss.Color("#00b300")
12+
Orange = lipgloss.Color("#FFA500")
1713

1814
Bluegrey = lipgloss.Color("#c2d1f0")
1915

@@ -22,9 +18,14 @@ var (
2218
TextYellow = lipgloss.NewStyle().Foreground(Yellow)
2319
TextGreen = lipgloss.NewStyle().Foreground(Green)
2420
TextBlue = lipgloss.NewStyle().Foreground(Blue)
21+
TextOrange = lipgloss.NewStyle().Foreground(Orange)
2522
TextBlueGrey = lipgloss.NewStyle().Foreground(Bluegrey)
2623
TextYellowOnBlue = lipgloss.NewStyle().Foreground(Yellow).Background(Blue).Underline(true)
2724

25+
// Table styles
26+
//SelectedRow = lipgloss.NewStyle().Background(Blue).Foreground(Yellow).Bold(false)
27+
SelectedRow = lipgloss.NewStyle().Background(Blue).Foreground(Yellow)
28+
2829
// ErrorHelp Box
2930
//ErrorHelp = lipgloss.NewStyle().Foreground(red).Border(lipgloss.RoundedBorder()).BorderForeground(red)
3031
ErrorHelp = lipgloss.NewStyle().Foreground(Red)

internal/table/table.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,22 @@ type Model struct {
1919
cursor int
2020
focus bool
2121
styles Styles
22+
CustomStyles
2223

2324
viewport viewport.Model
2425
renderedLines
2526
}
2627

28+
type CustomStyles struct {
29+
CustomCellStyle CustomStyle
30+
CustomRowStyle CustomStyle
31+
CustomSelectedStyle CustomStyle
32+
}
33+
34+
type CustomStyle interface {
35+
Style(int, Row) lipgloss.Style
36+
}
37+
2738
type renderedLines struct {
2839
start, end int
2940
}
@@ -393,19 +404,52 @@ func (m Model) headersView() string {
393404
}
394405

395406
func (m *Model) renderRow(rowID int) string {
396-
var s = make([]string, 0, len(m.cols))
407+
var (
408+
selStyle lipgloss.Style
409+
rowStyle lipgloss.Style
410+
cellStyles = make([]lipgloss.Style, len(m.cols))
411+
s = make([]string, 0, len(m.cols))
412+
)
413+
414+
// Prepare RowStyle
415+
if m.CustomRowStyle != nil {
416+
rowStyle = m.CustomRowStyle.Style(rowID, m.rows[rowID])
417+
} else {
418+
// We don't have rowstyle separately, what now?
419+
// do an empty one? or m.Styles.Cell ???
420+
rowStyle = lipgloss.NewStyle()
421+
}
422+
// If row is selected, prepare selStyle
423+
if rowID == m.cursor {
424+
if m.CustomSelectedStyle != nil {
425+
selStyle = m.CustomSelectedStyle.Style(rowID, m.rows[rowID])
426+
} else {
427+
selStyle = m.styles.Selected
428+
}
429+
} else {
430+
selStyle = lipgloss.NewStyle()
431+
}
432+
// For each cell in a row get its style and save it
433+
for i := range m.rows[rowID] {
434+
if m.CustomCellStyle != nil {
435+
cellStyles[i] = m.CustomCellStyle.Style(i, m.rows[rowID])
436+
} else {
437+
cellStyles[i] = m.styles.Cell
438+
}
439+
}
440+
// Loop through the cells again Render() with overlayed styles
397441
for i, value := range m.rows[rowID] {
398442
style := lipgloss.NewStyle().Width(m.cols[i].Width).MaxWidth(m.cols[i].Width).Inline(true)
399-
renderedCell := m.styles.Cell.Render(style.Render(runewidth.Truncate(value, m.cols[i].Width, "…")))
443+
overlayedStyle := cellStyles[i].Inherit(rowStyle)
444+
overlayedStyle = overlayedStyle.Copy().Inherit(selStyle)
445+
//renderedCell := cellStyles[i].Inherit(rowStyle).Render(style.Render(runewidth.Truncate(value, m.cols[i].Width, "…")))
446+
renderedCell := overlayedStyle.Render(style.Render(runewidth.Truncate(value, m.cols[i].Width, "…")))
447+
400448
s = append(s, renderedCell)
401449
}
402450

403451
row := lipgloss.JoinHorizontal(lipgloss.Left, s...)
404452

405-
if rowID == m.cursor {
406-
return m.styles.Selected.Render(row)
407-
}
408-
409453
return row
410454
}
411455

0 commit comments

Comments
 (0)