• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37527

pending completion
#37527

push

local

web-flow
make `IRShow.method_name` inferrable (#49607)

18 of 18 new or added lines in 3 files covered. (100.0%)

68710 of 81829 relevant lines covered (83.97%)

33068903.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/stdlib/REPL/src/TerminalMenus/RadioMenu.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
"""
4

5
    RadioMenu
6

7
A menu that allows a user to select a single option from a list.
8

9
# Sample Output
10

11
```julia-repl
12
julia> request(RadioMenu(options, pagesize=4))
13
Choose your favorite fruit:
14
^  grape
15
   strawberry
16
 > blueberry
17
v  peach
18
Your favorite fruit is blueberry!
19
```
20

21
"""
22
mutable struct RadioMenu{C} <: _ConfiguredMenu{C}
23
    options::Array{String,1}
×
24
    keybindings::Vector{Char}
25
    pagesize::Int
26
    pageoffset::Int
27
    selected::Int
28
    config::C
29
end
30

31

32
"""
33

34
    RadioMenu(options::Array{String,1}; pagesize::Int=10,
35
                                        keybindings::Vector{Char}=Char[],
36
                                        kwargs...)
37

38
Create a RadioMenu object. Use `request(menu::RadioMenu)` to get user input.
39
`request()` returns an `Int` which is the index of the option selected by the
40
user.
41

42
# Arguments
43

44
  - `options::Array{String, 1}`: Options to be displayed
45
  - `pagesize::Int=10`: The number of options to be displayed at one time, the menu will scroll if length(options) > pagesize
46
  - `keybindings::Vector{Char}=Char[]`: Shortcuts to pick corresponding entry from `options`
47

48
Any additional keyword arguments will be passed to [`TerminalMenus.Config`](@ref).
49

50
!!! compat "Julia 1.8"
51
    The `keybindings` argument requires Julia 1.8 or later.
52
"""
53
function RadioMenu(options::Array{String,1}; pagesize::Int=10, warn::Bool=true, keybindings::Vector{Char}=Char[], kwargs...)
×
54
    length(options) < 1 && error("RadioMenu must have at least one option")
×
55
    length(keybindings) in [0, length(options)] || error("RadioMenu must have either no keybindings, or one per option")
×
56

57
    # if pagesize is -1, use automatic paging
58
    pagesize = pagesize == -1 ? length(options) : pagesize
×
59
    # pagesize shouldn't be bigger than options
60
    pagesize = min(length(options), pagesize)
×
61
    # after other checks, pagesize must be greater than 1
62
    pagesize < 1 && error("pagesize must be >= 1")
×
63

64
    pageoffset = 0
×
65
    selected = -1 # none
×
66

67
    if !isempty(kwargs)
×
68
        RadioMenu(options, keybindings, pagesize, pageoffset, selected, Config(; kwargs...))
×
69
    else
70
        warn && Base.depwarn("Legacy `RadioMenu` interface is deprecated, set a configuration option such as `RadioMenu(options; charset=:ascii)` to trigger the new interface.", :RadioMenu)
×
71
        RadioMenu(options, keybindings, pagesize, pageoffset, selected, CONFIG)
×
72
    end
73
end
74

75

76

77
# AbstractMenu implementation functions
78
# See AbstractMenu.jl
79
#######################################
80

81
options(m::RadioMenu) = m.options
×
82

83
cancel(m::RadioMenu) = m.selected = -1
×
84

85
function pick(menu::RadioMenu, cursor::Int)
×
86
    menu.selected = cursor
×
87
    return true #break out of the menu
×
88
end
89

90
function writeline(buf::IOBuffer, menu::RadioMenu{Config}, idx::Int, iscursor::Bool)
×
91
    print(buf, replace(menu.options[idx], "\n" => "\\n"))
×
92
end
93

94
function keypress(m::RadioMenu, i::UInt32)
×
95
    isempty(m.keybindings) && return false
×
96
    i = findfirst(isequal(i), Int.(m.keybindings))
×
97
    isnothing(i) && return false
×
98
    m.selected = i
×
99
    return true
×
100
end
101

102
# Legacy interface
103
function writeLine(buf::IOBuffer, menu::RadioMenu{<:Dict}, idx::Int, cursor::Bool)
×
104
    # print a ">" on the selected entry
105
    cursor ? print(buf, menu.config[:cursor] ," ") : print(buf, "  ")
×
106

107
    print(buf, replace(menu.options[idx], "\n" => "\\n"))
×
108
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc