Skip to content

Commit 94e32ab

Browse files
authored
Add an option for user-defined key presses (#10)
1 parent 1c41f86 commit 94e32ab

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ One can create very large menus with thousands of options, in which case the men
114114
and `PgUp`/`PgDn`.
115115
As described in the `TerminalMenus` documentation, you can customize aspects of the menu's appearance,
116116
such as the number of items visible simultaneously and the characters used to indicate scrolling and the cursor position.
117+
`TreeMenu` includes optional keyword arguments `pagesize`, `dynamic`, `maxsize`, and `keypress` to control
118+
various aspects of the interactive menu. See the docstring for `TreeMenu` for more information.
117119

118120
For `Node` objects where `data` is not an `AbstractString`, you will most likely want to specialize `FoldingTrees.writeoption` for your type.
119121
See `?FoldingTrees.writeoption` for details.
122+

src/treemenu.jl

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
export TreeMenu
44

5+
"""
6+
TreeMenu(root; pagesize::Int=10, dynamic = false, maxsize = pagesize, keypress = (m,i) -> false, kwargs...)
7+
8+
Use `root` to create an interactive menu using TerminalMenus. `pagesize` is the number of lines to use for a page.
9+
If `dynamic`, adjust the page size based on the expansion of the content. `maxsize` is the maximum size of the page.
10+
11+
Provide a function `keypress` to respond to keys while the menu is shown. The function has two arguments, `m::TreeMenu`
12+
and `i::UInt32`. The integer `i` is the key pressed. This function should return `true` if the menu should exit and
13+
`false` otherwise.
14+
15+
`kwargs` are passed to `TerminalMenus.Config`.
16+
"""
517
mutable struct TreeMenu{N<:Node} <: TerminalMenus._ConfiguredMenu{TerminalMenus.Config}
618
root::N
719
current::N
@@ -14,11 +26,12 @@ mutable struct TreeMenu{N<:Node} <: TerminalMenus._ConfiguredMenu{TerminalMenus.
1426
dynamic::Bool
1527
maxsize::Int
1628
pageoffset::Int
29+
keypress::Any
1730
config::TerminalMenus.Config
1831
end
19-
function TreeMenu(root; pagesize::Int=10, dynamic = false, maxsize = pagesize, kwargs...)
32+
function TreeMenu(root; pagesize::Int=10, dynamic = false, maxsize = pagesize, keypress = (m,i) -> false, kwargs...)
2033
pagesize = min(pagesize, count_open_leaves(root))
21-
return TreeMenu(root, root, 1, 1, 1, false, pagesize, dynamic, maxsize, 0, TerminalMenus.Config(kwargs...))
34+
return TreeMenu(root, root, 1, 1, 1, false, pagesize, dynamic, maxsize, 0, keypress, TerminalMenus.Config(kwargs...))
2235
end
2336

2437
"""
@@ -122,7 +135,7 @@ function TerminalMenus.keypress(menu::TreeMenu, i::UInt32)
122135
menu.pagesize = min(menu.maxsize, count_open_leaves(menu.root))
123136
end
124137
end
125-
return false
138+
return menu.keypress(menu, i)
126139
end
127140

128141
function TerminalMenus.selected(menu::TreeMenu)

test/runtests.jl

+17
Original file line numberDiff line numberDiff line change
@@ -266,5 +266,22 @@ if isdefined(FoldingTrees, :TreeMenu)
266266
@test length(lines) == 5
267267
@test lines[5] == "v 1b"
268268
@test menu.pagesize == 5
269+
270+
# Test keypress
271+
testint = 1
272+
function keypress(menu::TreeMenu, i::UInt32)
273+
if i == Int('+')
274+
testint += 1
275+
elseif i == Int('-')
276+
testint -= 1
277+
end
278+
return false
279+
end
280+
menu2 = TreeMenu(root, keypress = keypress)
281+
TerminalMenus.keypress(menu2, UInt32('+'))
282+
@test testint == 2
283+
TerminalMenus.keypress(menu2, UInt32('-'))
284+
@test testint == 1
285+
269286
end
270287
end

0 commit comments

Comments
 (0)