Skip to content

Commit 302726e

Browse files
committed
feat: Update table example ✨
1 parent 8b57417 commit 302726e

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

examples/table.jl

+14-16
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,25 @@ using Random
88
end
99

1010
function TUI.view(m::Model)
11-
cells = [
12-
TUI.Cell(; content = "Hello", style = TUI.Crayon(; foreground = :black, background = :white)),
13-
TUI.Cell(; content = "World", style = TUI.Crayon(; foreground = :black, background = :white)),
14-
]
11+
data = [TUI.Datum(; content = "Hello"), TUI.Datum(; content = "World"), TUI.Datum(; content = "FSFDS")]
1512
header = TUI.Row(;
16-
cells = [
17-
TUI.Cell(; content = "Column1", style = TUI.Crayon(; foreground = :black, background = :white)),
18-
TUI.Cell(; content = "Column2", style = TUI.Crayon(; foreground = :black, background = :white)),
13+
data = [
14+
TUI.Datum(; content = "Column1", style = TUI.Crayon(; foreground = :black, background = :white)),
15+
TUI.Datum(; content = "Column2", style = TUI.Crayon(; foreground = :black, background = :white)),
16+
TUI.Datum(; content = "Column3", style = TUI.Crayon(; foreground = :black, background = :white)),
1917
],
2018
)
2119
rows = [
22-
TUI.Row(; cells),
23-
TUI.Row(; cells),
24-
TUI.Row(; cells),
25-
TUI.Row(; cells),
26-
TUI.Row(; cells),
27-
TUI.Row(; cells),
28-
TUI.Row(; cells),
29-
TUI.Row(; cells),
20+
TUI.Row(; data),
21+
TUI.Row(; data),
22+
TUI.Row(; data),
23+
TUI.Row(; data),
24+
TUI.Row(; data),
25+
TUI.Row(; data),
26+
TUI.Row(; data),
27+
TUI.Row(; data),
3028
]
31-
widths = [TUI.Auto(50), TUI.Auto(50)]
29+
widths = [TUI.Percent(33), TUI.Percent(33), TUI.Percent(33)]
3230
TUI.Table(; rows, widths, header)
3331
end
3432

src/buffer.jl

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
@kwdef struct Cell{T}
3-
content::T
2+
@kwdef struct Cell
3+
content::Char
44
style::Crayons.Crayon = Crayon()
55
end
66

@@ -39,17 +39,12 @@ function set(buffer::Buffer, area::Rect, style::Crayons.Crayon)
3939
end
4040
end
4141

42-
function set(buffer::Buffer, area::Rect, cell::Cell{Char})
42+
function set(buffer::Buffer, area::Rect, cell::Cell)
4343
for j in area.y:area.height, i in area.x:area.width
4444
set(buffer, i, j, cell)
4545
end
4646
end
4747

48-
function set(buf::Buffer, area::Rect, cell::Cell)
49-
set(buf, area, cell.style)
50-
set(buf, left(area), top(area), Base.split(cell.content, '\n'))
51-
end
52-
5348
function set(buffer::Buffer, col::Integer, row::Integer, style::Crayons.Crayon)
5449
row = min(row, Base.size(buffer.content, 1))
5550
col = min(col, Base.size(buffer.content, 2))
@@ -91,7 +86,7 @@ function set(buffer::Buffer, col::Integer, row::Integer, c::Char, style::Crayons
9186
set(buffer, col, row, Cell(c, style))
9287
end
9388

94-
function set(buffer::Buffer, col::Integer, row::Integer, c::Cell{Char})
89+
function set(buffer::Buffer, col::Integer, row::Integer, c::Cell)
9590
row = min(row, Base.size(buffer.content, 1))
9691
col = min(col, Base.size(buffer.content, 2))
9792
buffer.content[row, col] = c

src/widgets/table.jl

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
@kwdef struct Datum
2+
content::String = ""
3+
style::Crayon = Crayon()
4+
end
5+
16
@kwdef struct Row
2-
cells::Vector{Cell}
7+
data::Vector{Datum}
38
height::Int = 3
49
bottom_margin::Int = 0
510
end
@@ -117,13 +122,13 @@ function render_row(
117122
highlight_symbol::Union{Nothing,String} = nothing,
118123
)
119124
x_offset = x
120-
for (i, cell) in enumerate(row.cells)
125+
for (i, datum) in enumerate(row.data)
121126
# Highlight if row is selected and highlight symbol is provided
122127
if is_selected && !isnothing(highlight_symbol)
123-
set(buf, x_offset, y, highlight_symbol, cell.style)
128+
set(buf, x_offset, y, highlight_symbol)
124129
x_offset += width(highlight_symbol)
125130
end
126-
set(buf, Rect(; x = x_offset, y = y, width = column_widths[i], height = row.height), cell)
131+
set(buf, x_offset, y, datum.content, datum.style)
127132
x_offset += column_widths[i]
128133
end
129134
end

0 commit comments

Comments
 (0)