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
exploring :color for TerminalBuffer
  • Loading branch information
caleb-allen committed Nov 2, 2023
commit 1039f60ea32858fbbb6930f625f04e714de820f6
17 changes: 9 additions & 8 deletions stdlib/REPL/src/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1541,11 +1541,20 @@ function write_prompt(terminal::AbstractTerminal, p::Prompt, color::Bool)
return textwidth(promptstr)
# return width
end
# returns the width of the written prompt
function write_prompt(io::IO, s::Union{AbstractString,Function}, color::Bool)
@static Sys.iswindows() && _reset_console_mode()
promptstr = prompt_string(s)::AbstractString
# TODO this write is not dispatching to `StyledStrings`
write(io, promptstr)
return textwidth(promptstr)
end
# TODO remove
function write_output_prefix(io::IO, p::Prompt, color::Bool)
@static Sys.iswindows() && _reset_console_mode()
promptstr = prompt_string(p.output_prefix)::String
# TODO this write is not dispatching to `StyledStrings`
# write(io, which(write, (REPL.Terminals.TTYTerminal, AnnotatedString)))
write(io, promptstr)
return textwidth(promptstr)
end
Expand Down Expand Up @@ -1579,14 +1588,6 @@ function _reset_console_mode()
end
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)::AbstractString
# TODO this write is not dispatching to `StyledStrings`
write(terminal, promptstr)
return textwidth(promptstr)
end

### Keymap Support

Expand Down
9 changes: 8 additions & 1 deletion stdlib/REPL/src/Terminals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ abstract type UnixTerminal <: TextTerminal end
pipe_reader(t::UnixTerminal) = t.in_stream::IO
pipe_writer(t::UnixTerminal) = t.out_stream::IO

# TODO somehow pass color context to TerminalBuffer.
# The issue is that :color => false when creating
# a TerminalBuffer like TerminalBuffer(IOBuffer()).
# Also, we're double buffering if you include TerminalBuffer
# and the IOBuffer() in StyledStrings/io.jl/_ansi_writer()
# Another possibility is to replace IOBuffer with a
# Array{AnnotatedChar}
mutable struct TerminalBuffer <: UnixTerminal
out_stream::IO
end
Expand Down Expand Up @@ -152,7 +159,7 @@ beep(t::UnixTerminal) = write(t.err_stream,"\x7")

Base.displaysize(t::UnixTerminal) = displaysize(t.out_stream)

hascolor(t::TTYTerminal) = get(t.out_stream, :color, false)::Bool
hascolor(t::Union{TTYTerminal,TerminalBuffer}) = get(t.out_stream, :color, false)::Bool

# use cached value of have_color
Base.in(key_value::Pair, t::TTYTerminal) = in(key_value, pipe_writer(t))
Expand Down
17 changes: 9 additions & 8 deletions stdlib/REPL/src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ finally
Base._track_dependencies[] = true
end
using Base.Meta
using StyledStrings

import Markdown
import StyledStrings
Expand Down Expand Up @@ -57,14 +58,14 @@ cd("complete_path\t\t$CTRL_C

julia_exepath() = joinpath(Sys.BINDIR, Base.julia_exename())

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?> }"
# 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