Skip to content

Commit c701c32

Browse files
Make TerminalMenus header dynamic. (#38489)
1 parent bb6a48e commit c701c32

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

stdlib/REPL/src/TerminalMenus/AbstractMenu.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ function request(term::REPL.Terminals.TTYTerminal, m::AbstractMenu; cursor::Unio
176176
if cursor isa Int
177177
cursor = Ref(cursor)
178178
end
179-
menu_header = header(m)
180-
!suppress_output && !isempty(menu_header) && println(term.out_stream, menu_header)
181179

182180
state = nothing
183181
if !suppress_output
@@ -327,6 +325,12 @@ function printmenu(out::IO, m::AbstractMenu, cursoridx::Int; oldstate=nothing, i
327325
print(buf, "\x1b[999D\x1b[$(ncleared)A") # move left 999 spaces and up `ncleared` lines
328326
end
329327

328+
nheaderlines = 0
329+
for headerline in split(header(m), "\n", keepempty=false)
330+
print(buf, "\x1b[2K", headerline, "\r\n")
331+
nheaderlines += 1
332+
end
333+
330334
firstline = m.pageoffset+1
331335
lastline = min(m.pagesize+m.pageoffset, lastoption)
332336

@@ -353,7 +357,8 @@ function printmenu(out::IO, m::AbstractMenu, cursoridx::Int; oldstate=nothing, i
353357
(firstline == lastline || i != lastline) && print(buf, "\r\n")
354358
end
355359

356-
newstate = lastline-firstline # final line doesn't have `\n`
360+
newstate = nheaderlines + lastline - firstline # final line doesn't have `\n`
361+
357362
if newstate < ncleared && oldstate !== nothing
358363
# we printed fewer lines than last time. Erase the leftovers.
359364
for i = newstate+1:ncleared

stdlib/REPL/test/TerminalMenus/multiselect_menu.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ for kws in ((charset=:ascii,),
3030
TerminalMenus.writeline(buf, multi_menu, 1, true)
3131
@test String(take!(buf)) == "$uck 1"
3232
TerminalMenus.printmenu(buf, multi_menu, 1; init=true)
33-
@test startswith(String(take!(buf)), string("\e[2K $cur $uck 1"))
33+
@test startswith(String(take!(buf)), string("\e[2K[press: d=done, a=all, n=none]\r\n\e[2K $cur $uck 1"))
3434
push!(multi_menu.selected, 1)
3535
TerminalMenus.printmenu(buf, multi_menu, 2; init=true)
36-
@test startswith(String(take!(buf)), string("\e[2K $chk 1\r\n\e[2K $cur $uck 2"))
36+
@test startswith(String(take!(buf)), string("\e[2K[press: d=done, a=all, n=none]\r\n\e[2K $chk 1\r\n\e[2K $cur $uck 2"))
3737
end
3838

3939
# Preselection

stdlib/REPL/test/TerminalMenus/multiselect_with_skip_menu.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function MultiSelectWithSkipMenu(options::Array{String,1}; pagesize::Int=10,
3030
TerminalMenus.MultiSelectConfig(; kwargs...))
3131
end
3232

33-
TerminalMenus.header(m::MultiSelectWithSkipMenu) = "[press: d=done, a=all, c=none, npNP=move with skip]"
33+
TerminalMenus.header(m::MultiSelectWithSkipMenu) = "[press: d=done, a=all, c=none, npNP=move with skip, $(length(m.selected)) items selected]"
3434

3535
TerminalMenus.options(m::MultiSelectWithSkipMenu) = m.options
3636

@@ -118,7 +118,13 @@ end
118118
# These tests are specifically designed to verify that a `RefValue`
119119
# input to the AbstractMenu `request` function works as intended.
120120
menu = MultiSelectWithSkipMenu(string.(1:5), selected=[2, 3])
121+
buf = IOBuffer()
122+
TerminalMenus.printmenu(buf, menu, 1; init=true)
123+
@test occursin("2 items selected", String(take!(buf)))
121124
@test simulate_input(Set([2, 3, 4]), menu, 'n', :enter, 'd')
125+
buf = IOBuffer()
126+
TerminalMenus.printmenu(buf, menu, 1; init=true)
127+
@test occursin("3 items selected", String(take!(buf)))
122128

123129
menu = MultiSelectWithSkipMenu(string.(1:5), selected=[2, 3])
124130
@test simulate_input(Set([2]), menu, 'P', :enter, 'd', cursor=5)

0 commit comments

Comments
 (0)