Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use StyledStrings for REPL prompt styling #51887

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
prompt and prompt prefix stuff
  • Loading branch information
caleb-allen committed Nov 2, 2023
commit 045d5e871e6762d6b6705622b6cd52b476e77af6
20 changes: 15 additions & 5 deletions stdlib/REPL/src/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1532,13 +1532,22 @@ default_enter_cb(_) = true

write_prompt(terminal::AbstractTerminal, s::PromptState, color::Bool) = write_prompt(terminal, s.p, color)
function write_prompt(terminal::AbstractTerminal, p::Prompt, color::Bool)
width = write_prompt(terminal, p.prompt, color)
return width
@static Sys.iswindows() && _reset_console_mode()
# width = write_prompt(terminal, p.prompt, color)

promptstr = prompt_string(p)::AnnotatedString
# TODO this write is not dispatching to `StyledStrings`
write(terminal, promptstr)
return textwidth(promptstr)
# return width
end
# TODO remove
function write_output_prefix(io::IO, p::Prompt, color::Bool)
width = write_prompt(io, p.output_prefix, color)
return width
@static Sys.iswindows() && _reset_console_mode()
promptstr = prompt_string(p.output_prefix)::String
# TODO this write is not dispatching to `StyledStrings`
write(io, promptstr)
return textwidth(promptstr)
end
# On Windows, when launching external processes, we cannot control what assumption they make on the
# console mode. We thus forcibly reset the console mode at the start of the prompt to ensure they do
Expand Down Expand Up @@ -1573,7 +1582,8 @@ end
# returns the width of the written prompt
function write_prompt(terminal::Union{IO, AbstractTerminal}, s::Union{AbstractString,Function}, color::Bool)
@static Sys.iswindows() && _reset_console_mode()
promptstr = prompt_string(s)::Union{String,AnnotatedString}
promptstr = prompt_string(s)::AbstractString
# TODO this write is not dispatching to `StyledStrings`
write(terminal, promptstr)
return textwidth(promptstr)
end
Expand Down
29 changes: 14 additions & 15 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,13 @@ mutable struct LineEditREPL <: AbstractREPL
interface::ModalInterface
backendref::REPLBackendRef
frontend_task::Task
function LineEditREPL(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,in_help,envcolors)
function LineEditREPL(t,hascolor,history_file,in_shell,in_help,envcolors)
opts = Options()
opts.hascolor = hascolor
if !hascolor
opts.beep_colors = [""]
end
new(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,
in_help,envcolors,false,nothing, opts, nothing, Tuple{String,Int}[])
new(t,hascolor,history_file,in_shell,in_help,envcolors,false,nothing, opts, nothing, Tuple{String,Int}[])
end
end
outstream(r::LineEditREPL) = (t = r.t; t isa TTYTerminal ? t.out_stream : t)
Expand Down Expand Up @@ -961,11 +960,11 @@ repl_filename(repl, hp) = "REPL"
const JL_PROMPT_PASTE = Ref(true)
enable_promptpaste(v::Bool) = JL_PROMPT_PASTE[] = v

function contextual_prompt(repl::LineEditREPL, prompt::Union{AnnotatedString,String,Function})
function contextual_prompt(repl::LineEditREPL, prompt::Union{AnnotatedString,Function})
function ()
mod = active_module(repl)
prefix = mod == Main ? "" : string('(', mod, ") ")
pr = prompt isa AbstractString ? prompt : prompt()
pr = prompt isa AnnotatedString ? prompt : prompt()
prefix * pr
end
end
Expand Down Expand Up @@ -1482,19 +1481,19 @@ function run_frontend(repl::StreamREPL, backend::REPLBackendRef)
dopushdisplay = !in(d,Base.Multimedia.displays)
dopushdisplay && pushdisplay(d)
while !eof(repl.stream)::Bool
if have_color
print(repl.stream,repl.prompt_color)
end
print(repl.stream, "julia> ")
if have_color
print(repl.stream, input_color(repl))
end
# if have_color
# print(repl.stream,repl.prompt_color)
# end
print(repl.stream, JULIA_PROMPT)
# if have_color
# print(repl.stream, input_color(repl))
# end
line = readline(repl.stream, keep=true)
if !isempty(line)
ast = Base.parse_input_line(line)
if have_color
print(repl.stream, Base.color_normal)
end
# if have_color
# print(repl.stream, Base.color_normal)
# end
response = eval_with_backend(ast, backend)
print_response(repl, response, !ends_with_semicolon(line), have_color)
end
Expand Down
6 changes: 5 additions & 1 deletion stdlib/REPL/src/notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ When running with incremental precompilation, the above code causes issues with

I'm sure that the `_PROMPT` consts could be declared in a precompile-friendly way, but I wonder if this could be fixed within the `styled` macro. Maybe there are macros beyond base/stdlib which solve the same problem.

- [ ] report precompilation issue to `StyledStrings.jl`
- [ ] report precompilation issue to `StyledStrings.jl`

Relevant PRs
https://github.com/JuliaLang/julia/pull/36689
https://github.com/JuliaLang/julia/pull/46474/
4 changes: 4 additions & 0 deletions stdlib/REPL/src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const JULIA_PROMPT = "julia> "
const PKG_PROMPT = "pkg> "
const SHELL_PROMPT = "shell> "
const HELP_PROMPT = "help?> "
# const JULIA_PROMPT = styled"{repl_prompt_julia:julia> }"
# const PKG_PROMPT = styled"{repl_prompt_pkg:pkg> }"
# const SHELL_PROMPT = styled"{repl_prompt_shell:shell> }"
# const HELP_PROMPT = styled"{repl_prompt_help:help?> }"

blackhole = Sys.isunix() ? "/dev/null" : "nul"
procenv = Dict{String,Any}(
Expand Down