|
| 1 | +# This file is a part of Julia. License is MIT: https://julialang.org/license |
| 2 | + |
| 3 | +# Like MultiSelect but adds `n`/`p` to move to next/previous |
| 4 | +# unselected item and `N`/`P` to move to next/previous selected item. |
| 5 | +mutable struct MultiSelectWithSkipMenu <: TerminalMenus._ConfiguredMenu{TerminalMenus.Config} |
| 6 | + options::Array{String,1} |
| 7 | + pagesize::Int |
| 8 | + pageoffset::Int |
| 9 | + selected::Set{Int} |
| 10 | + cursor::Base.RefValue{Int} |
| 11 | + config::TerminalMenus.MultiSelectConfig |
| 12 | +end |
| 13 | + |
| 14 | +function MultiSelectWithSkipMenu(options::Array{String,1}; pagesize::Int=10, |
| 15 | + selected=Int[], kwargs...) |
| 16 | + length(options) < 1 && error("MultiSelectWithSkipMenu must have at least one option") |
| 17 | + |
| 18 | + pagesize = pagesize == -1 ? length(options) : pagesize |
| 19 | + pagesize = min(length(options), pagesize) |
| 20 | + pagesize < 1 && error("pagesize must be >= 1") |
| 21 | + |
| 22 | + pageoffset = 0 |
| 23 | + _selected = Set{Int}() |
| 24 | + for item in selected |
| 25 | + push!(_selected, item) |
| 26 | + end |
| 27 | + |
| 28 | + MultiSelectWithSkipMenu(options, pagesize, pageoffset, _selected, |
| 29 | + Ref{Int}(1), |
| 30 | + TerminalMenus.MultiSelectConfig(; kwargs...)) |
| 31 | +end |
| 32 | + |
| 33 | +TerminalMenus.header(m::MultiSelectWithSkipMenu) = "[press: d=done, a=all, c=none, npNP=move with skip]" |
| 34 | + |
| 35 | +TerminalMenus.options(m::MultiSelectWithSkipMenu) = m.options |
| 36 | + |
| 37 | +TerminalMenus.cancel(m::MultiSelectWithSkipMenu) = m.selected = Set{Int}() |
| 38 | + |
| 39 | +# Do not exit menu when a user selects one of the options |
| 40 | +function TerminalMenus.pick(menu::MultiSelectWithSkipMenu, cursor::Int) |
| 41 | + if cursor in menu.selected |
| 42 | + delete!(menu.selected, cursor) |
| 43 | + else |
| 44 | + push!(menu.selected, cursor) |
| 45 | + end |
| 46 | + |
| 47 | + return false |
| 48 | +end |
| 49 | + |
| 50 | +function TerminalMenus.writeline(buf::IOBuffer, |
| 51 | + menu::MultiSelectWithSkipMenu, |
| 52 | + idx::Int, iscursor::Bool) |
| 53 | + if idx in menu.selected |
| 54 | + print(buf, menu.config.checked, " ") |
| 55 | + else |
| 56 | + print(buf, menu.config.unchecked, " ") |
| 57 | + end |
| 58 | + |
| 59 | + print(buf, replace(menu.options[idx], "\n" => "\\n")) |
| 60 | +end |
| 61 | + |
| 62 | +# d: Done, return from request |
| 63 | +# a: Select all |
| 64 | +# c: Deselect all |
| 65 | +# n: Move to next unselected |
| 66 | +# p: Move to previous unselected |
| 67 | +# N: Move to next selected |
| 68 | +# P: Move to previous selected |
| 69 | +function TerminalMenus.keypress(menu::MultiSelectWithSkipMenu, key::UInt32) |
| 70 | + if key == UInt32('d') || key == UInt32('D') |
| 71 | + return true # break |
| 72 | + elseif key == UInt32('a') || key == UInt32('A') |
| 73 | + menu.selected = Set(1:length(menu.options)) |
| 74 | + elseif key == UInt32('c') || key == UInt32('C') |
| 75 | + menu.selected = Set{Int}() |
| 76 | + elseif key == UInt32('n') |
| 77 | + move_cursor!(menu, 1, false) |
| 78 | + elseif key == UInt32('p') |
| 79 | + move_cursor!(menu, -1, false) |
| 80 | + elseif key == UInt32('N') |
| 81 | + move_cursor!(menu, 1, true) |
| 82 | + elseif key == UInt32('P') |
| 83 | + move_cursor!(menu, -1, true) |
| 84 | + end |
| 85 | + false # don't break |
| 86 | +end |
| 87 | + |
| 88 | +function move_cursor!(menu, direction, selected) |
| 89 | + c = menu.cursor[] |
| 90 | + while true |
| 91 | + c += direction |
| 92 | + if !(1 <= c <= length(menu.options)) |
| 93 | + return |
| 94 | + end |
| 95 | + if (c in menu.selected) == selected |
| 96 | + break |
| 97 | + end |
| 98 | + end |
| 99 | + menu.cursor[] = c |
| 100 | + if menu.pageoffset >= c - 1 |
| 101 | + menu.pageoffset = max(c - 2, 0) |
| 102 | + end |
| 103 | + if menu.pageoffset + menu.pagesize <= c |
| 104 | + menu.pageoffset = min(c + 1, length(menu.options)) - menu.pagesize |
| 105 | + end |
| 106 | +end |
| 107 | + |
| 108 | +# Intercept the `request` call to insert the cursor field. |
| 109 | +function TerminalMenus.request(term::REPL.Terminals.TTYTerminal, |
| 110 | + m::MultiSelectWithSkipMenu; |
| 111 | + cursor::Int=1, kwargs...) |
| 112 | + m.cursor[] = cursor |
| 113 | + invoke(TerminalMenus.request, Tuple{REPL.Terminals.TTYTerminal, |
| 114 | + TerminalMenus.AbstractMenu}, |
| 115 | + term, m; cursor=m.cursor, kwargs...) |
| 116 | +end |
| 117 | + |
| 118 | +# These tests are specifically designed to verify that a `RefValue` |
| 119 | +# input to the AbstractMenu `request` function works as intended. |
| 120 | +menu = MultiSelectWithSkipMenu(string.(1:5), selected=[2, 3]) |
| 121 | +@test simulate_input(Set([2, 3, 4]), menu, 'n', :enter, 'd') |
| 122 | + |
| 123 | +menu = MultiSelectWithSkipMenu(string.(1:5), selected=[2, 3]) |
| 124 | +@test simulate_input(Set([2]), menu, 'P', :enter, 'd', cursor=5) |
0 commit comments