Skip to content

Commit 8b57417

Browse files
committed
feat: Better defaults for tui ✨
1 parent cc302e6 commit 8b57417

File tree

3 files changed

+77
-39
lines changed

3 files changed

+77
-39
lines changed

src/TerminalUserInterfaces.jl

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ using KiwiConstraintSolver
1010

1111
export Terminal
1212

13+
const TUI = TerminalUserInterfaces
14+
1315
include("logger.jl")
1416
include("symbols.jl")
1517
include("layout.jl")

src/layout.jl

+62-34
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,25 @@ function split(layout::Union{Horizontal,Vertical}, area::Rect)
118118
]
119119
s = Solver()
120120
for (i, c) in enumerate(layout.constraints)
121-
add_constraint(s, variables[i].x >= area.x)
122-
add_constraint(s, variables[i].y >= area.y)
123-
add_constraint(s, variables[i].w <= area.width)
124-
add_constraint(s, variables[i].h <= area.height)
125-
if c isa Auto
121+
if c isa Fixed
122+
if orientation == :horizontal
123+
add_constraint(s, variables[i].w == c.value)
124+
else
125+
add_constraint(s, variables[i].h == c.value)
126+
end
127+
elseif c isa Min
128+
if orientation == :horizontal
129+
add_constraint(s, variables[i].w >= c.value)
130+
else
131+
add_constraint(s, variables[i].h >= c.value)
132+
end
133+
elseif c isa Max
134+
if orientation == :horizontal
135+
add_constraint(s, variables[i].w <= c.value)
136+
else
137+
add_constraint(s, variables[i].h <= c.value)
138+
end
139+
elseif c isa Auto
126140
if orientation == :horizontal
127141
add_constraint(s, @constraint variables[i].w == c.value strength = KiwiConstraintSolver.STRONG)
128142
else
@@ -140,42 +154,33 @@ function split(layout::Union{Horizontal,Vertical}, area::Rect)
140154
@constraint variables[i].h == (c.value * height(area) ÷ 100) strength = KiwiConstraintSolver.MEDIUM
141155
)
142156
end
143-
elseif c isa Fixed
144-
if orientation == :horizontal
145-
add_constraint(s, variables[i].w == c.value)
146-
else
147-
add_constraint(s, variables[i].h == c.value)
148-
end
149-
elseif c isa Min
157+
end
158+
end
159+
for (i, c) in enumerate(layout.constraints)
160+
if i == 1
161+
add_constraint(s, variables[i].x >= area.x)
162+
add_constraint(s, variables[i].y >= area.y)
163+
else
150164
if orientation == :horizontal
151-
add_constraint(s, variables[i].w >= c.value)
165+
add_constraint(s, variables[i].x == variables[i-1].x + variables[i-1].w + 1)
152166
else
153-
add_constraint(s, variables[i].h >= c.value)
167+
add_constraint(s, variables[i].y == variables[i-1].y + variables[i-1].h + 1)
154168
end
155-
elseif c isa Max
169+
end
170+
if i == length(layout.constraints)
156171
if orientation == :horizontal
157-
add_constraint(s, variables[i].w <= c.value)
172+
add_constraint(
173+
s,
174+
@constraint variables[i].x + variables[i].w == width(area) strength = KiwiConstraintSolver.STRONG
175+
)
158176
else
159-
add_constraint(s, variables[i].h <= c.value)
177+
add_constraint(
178+
s,
179+
@constraint variables[i].y + variables[i].h == height(area) strength = KiwiConstraintSolver.STRONG
180+
)
160181
end
161182
end
162183
end
163-
for (i, c) in enumerate(layout.constraints)
164-
if i == 1
165-
continue
166-
end
167-
if orientation == :horizontal
168-
add_constraint(
169-
s,
170-
@constraint variables[i].x == variables[i-1].x + variables[i-1].w + 1 strength = KiwiConstraintSolver.MEDIUM
171-
)
172-
else
173-
add_constraint(
174-
s,
175-
@constraint variables[i].y == variables[i-1].y + variables[i-1].h + 1 strength = KiwiConstraintSolver.MEDIUM
176-
)
177-
end
178-
end
179184
if orientation == :horizontal
180185
add_constraint(s, sum(variables[i].w for (i, c) in enumerate(layout.constraints)) == width(area))
181186
else
@@ -192,10 +197,33 @@ function split(layout::Union{Horizontal,Vertical}, area::Rect)
192197
end
193198
end
194199
update_variables(s)
195-
rects = [Rect(round(v.x.value), round(v.y.value), round(v.w.value), round(v.h.value)) for v in variables]
200+
rects = if orientation == :horizontal
201+
[Rect(round(v.x.value), top(area), round(v.w.value), height(area)) for v in variables]
202+
else
203+
[Rect(left(area), round(v.y.value), width(area), round(v.h.value)) for v in variables]
204+
end
196205
rects
197206
end
198207

208+
function testing()
209+
# TUI.tui(; alternate_screen = false) do
210+
# t = TUI.Terminal()
211+
# TUI.render(t, TUI.Block(), r1)
212+
# TUI.render(t, TUI.Block(), r2)
213+
# TUI.render(t, TUI.Block(), r3)
214+
# TUI.draw(t)
215+
# end
216+
end
217+
218+
@testset "layout-rects-gaps" begin
219+
target = Rect(; x = 2, y = 2, width = 10, height = 10)
220+
constraints = [Percent(10), Max(5), Min(1)]
221+
r1, r2, r3 = split(Horizontal(constraints), target)
222+
@test r1 == Rect(; x = 2, y = 2, width = 1, height = 10)
223+
@test r2 == Rect(; x = 4, y = 2, width = 4, height = 10)
224+
@test r3 == Rect(; x = 10, y = 2, width = 4, height = 10)
225+
end
226+
199227
@testset "layout-rects-fixed" begin
200228
constraints = [Fixed(50), Fixed(50)]
201229
r1, r2 = split(Horizontal(constraints), Rect(0, 0, 100, 1))

src/terminal.jl

+13-5
Original file line numberDiff line numberDiff line change
@@ -263,21 +263,29 @@ function tui(f::Function; flags...)
263263
return r
264264
end
265265

266-
function tui(switch = true; log = true, enhance_keyboard = Sys.iswindows() ? false : true, mouse = false, flush = true)
266+
function tui(
267+
switch = true;
268+
log = true,
269+
enhance_keyboard = Sys.iswindows() ? false : true,
270+
mouse = false,
271+
flush = true,
272+
alternate_screen = true,
273+
raw_mode = true,
274+
)
267275
flush && Crossterm.flush()
268276
if switch
269277
log && Logger.initialize()
270-
Crossterm.raw_mode(true)
271-
Crossterm.alternate_screen(true)
278+
raw_mode && Crossterm.raw_mode(true)
279+
alternate_screen && Crossterm.alternate_screen(true)
272280
mouse && Crossterm.mouse_capture(true)
273281
enhance_keyboard && Crossterm.enhance_keyboard(true)
274282
Crossterm.hide()
275283
else
276284
Crossterm.show()
277285
enhance_keyboard && Crossterm.enhance_keyboard(false)
278286
mouse && Crossterm.mouse_capture(false)
279-
Crossterm.alternate_screen(false)
280-
Crossterm.raw_mode(false)
287+
alternate_screen && Crossterm.alternate_screen(false)
288+
raw_mode && Crossterm.raw_mode(false)
281289
log && Logger.reset()
282290
end
283291
flush && Crossterm.flush()

0 commit comments

Comments
 (0)