nick-paul / terminalmenus.jl Goto Github PK
View Code? Open in Web Editor NEWSimple interactive menus for the terminal (Now ships with Julia!)
License: Other
Simple interactive menus for the terminal (Now ships with Julia!)
License: Other
Hi,
I have two questions about the usage of terminal menus:
TerminalMenus.config()
, I see the option of supress_output
. Is there a typo? suppress_output
?using REPL.TerminalMenus
using Test
function simulateInput(keys...)
keydict = Dict(:up => "\e[A",
:down => "\e[B",
:enter => "\r")
for key in keys
if isa(key, Symbol)
write(Base.stdin.buffer, keydict[key])
else
write(Base.stdin.buffer, "$key")
end
end
end
TerminalMenus.config(supress_output=true)
options = ["a", "b", "c"]
menu = RadioMenu(options)
simulateInput(:enter)
choice = request("Choose variable:", menu)
@test choice == 1
julia> include("test_menu.jl")
Choose variable:
> a
b
c
Test Passed
Expression: choice == 1
Evaluated: 1 == 1
The writing to stdin works fine, but how should I turn off the menu display?
my use case is that i programmatically calculate the choices from a search in a database, and then want to give the user the opportunity to manually prune a few bad results. multi-select is perfect for this.
the problem is that on occasion there is just one entry in the database, and i'd still like to give the user the opportunity to discard it. as terminalmenus.jl stands now, i have to add logic to handle this case. would be better to remove the restriction that there must be at least two choices. if you agree, i'm happy to work up a PR. let me know. thanks.
Hi, I realize this library has been merged, and this repo is inactive, but the solution to my problem would probably end up with me forking this repo anyway, so I'd like to ask here. Maybe someone is still listening all these years later.
When overwriting the writeline
method to use printstyled
instead of print
, TerminalMenus.request
ignores the colored output:
import REPL
using REPL.TerminalMenus
# create menu
options = ["answer_01", "answer_02", "answer_03"]
menu = RadioMenu(options)
# print what options should look like
for e in menu.options
printstyled(stdout, e * "\n", reverse=true)
end
# overwrite writeline to print styled
function TerminalMenus.writeline(io::IO, menu::RadioMenu, cursor::Int, iscursor::Bool)
printstyled(io, (iscursor ? "> " : " ") * menu.options[cursor], reverse=true)
end
# request menu
request(menu)
This will first print the three answers with reverse
properly displayed, then, when the answers are printed through the TerminalMenus
routine, the style is ignored, despite printsytled
writing to the menu-supplied buffer io
.
Any tips what I function I would have to modify to make this work? I think it would be super nice to for example make the selected answer bold and yellow while making the other ones a darker gray, but this is currently not possible, which is a shame, considering how customizable TerminalMenus
already is
I assume it may be related to https://github.com/nick-paul/TerminalMenus.jl/blob/master/src/AbstractMenu.jl#L129 but I don't fully understand this method
https://travis-ci.org/invenia/PkgTemplates.jl/jobs/267682403#L115
It appears that in environments where a TTY is not available, terminal.in_stream
is an IOStream
which has no check_open
method defined for it.
Some tests for request
would catch this, you can write some by manually writing to STDIN
. For example, this will select the third entry from the top in a MultiSelectMenu
:
write(STDIN.buffer, "$('\x1b')[B\r$('\x1b')[B\rd")
As for a fix, the simplest option is just for {enable,disable}RawMode
to no-op if terminal.in_stream
is an IOStream
, since I'd imagine it's an exceedingly rare case in the "real world".
currently the prompt for a MultiSelectMenu is [press: d=done, a=all, n=none]
. took me awhile to figure out that one should press enter
to select. how about changing the prompt to [press: d=done, a=all, n=none, <enter>=select]
?
Currently, I is not clear if this README is the best and updated source for documentation for TerminalMenus. To me, it would feel more appropriate to replace the readme with a short description of the history and scope of the package, and then just link to the official docs.
Would you agree? Because the current situation feels like it is split between this package and the official docs, and the maintenance burden is doubled (right)?
Issue originally submitted here by @vdayanand
If the text is longer than the width of the terminal, wrapping will occur and cause the text to be rendered improperly:
julia> A = [string(repeat("ABC-", x*2), "ABC") for x in 7:10]
4-element Array{String,1}:
"ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC"
"ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC"
"ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC"
"ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC"
julia> m = RadioMenu(A)
TerminalMenus.RadioMenu(String["ABC-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ABC-ABC-ABC
Possible solution: trim lines before printing. Show the end of the selection if it is highlighted by the cursor.
julia> request(m)
ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC
ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-A...
> ...C-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC
ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-ABC-A...
First of all thanks for such a nice package! This is not really an issue but I wanted to suggest to allow J
and K
keys for navigating between options (meaning down and up respectively). I've found myself instinctively trying to use them when selecting options and it'd be nice if that worked out of the box.
@nick-paul, you may have noticed that @KristofferC and I have been using this lovely package for interactive prompting in the new Pkg3 package manager, which we're currently in the process of moving into Julia's standard library. Since dependencies of stdlib packages need to also be in the stdlib somewhere and we are planning on continuing to use and extend the interactive REPL menu functionality, at this point it seems to make sense to include the functionality of TerminalMenus as a standard part of Julia's built-in REPL library. However, I wanted to run the idea past you first since it's your code. There are few different approaches we could take to this:
We could use git filter-branch to preserve the commit history from this repo, so you would retain full credit for the code you've written (thank you!). However, since the stdlib is (for the time being) part of the main Julia repo, you'd have to make PR's to make changes to your own code, which you might not be happy about.
We could instead use git subtree to make the stdlib repo essentially a mirror of this repo instead. However, I've never used git subtree
and I don't know how much this will complicate things, so I'm a bit reluctant. Even if it works well technically, it may become confusing where to open issues and make PRs to the TerminalMenus code.
Anyway, that's the situation and I wanted to run it by you to see what you think.
I was directed here from JuliaLang/Pkg.jl#61. I have looked at the sources and it seems that the problem is that ansi-term
in Emacs sends \eOA
etc as the arrow keys, TerminalMenus.jl
expects \e[A
etc.
While ansi-term
s codes can be changed, the change is global, not buffer-specific. So if possible, it would be great if TerminalMenus.jl
could also accept \eOA
(up), \eOB
(down), \eOC
(right), \eOD
(left).
It would be cool to allow for some default or pre-selected item to be specified.
To me it seems most logical to add an optional argument, say default = 1
, to the request
function. In the case of a RadioMenu
, the default option will already have the cursor on it. In the case of the MultiSelectMenu
, each of the indices in default
will be prechecked when displayed.
Additionally, it could be cool to have the menu object remember the last choice and use that as the default. So request
will update its menu object with the last choice and this will be used as the default value next time there is a request.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.