Skip to content

Commit 6f80a22

Browse files
committed
Implement resizing banner, with StyledStrings
This supplants the --banner=short option in favour of a set of banner sizes, chosen such that it kinda-fits within the terminal frame.
1 parent adedd1d commit 6f80a22

File tree

9 files changed

+89
-71
lines changed

9 files changed

+89
-71
lines changed

base/client.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ end
411411
global active_repl
412412

413413
# run the requested sort of evaluation loop on stdio
414-
function run_main_repl(interactive::Bool, quiet::Bool, banner::Symbol, history_file::Bool, color_set::Bool)
414+
function run_main_repl(interactive::Bool, quiet::Bool, banner::Int, history_file::Bool, color_set::Bool)
415415
fallback_repl = parse(Bool, get(ENV, "JULIA_FALLBACK_REPL", "false"))
416416
if !fallback_repl && interactive
417417
load_InteractiveUtils()
@@ -424,7 +424,7 @@ function run_main_repl(interactive::Bool, quiet::Bool, banner::Symbol, history_f
424424
invokelatest(REPL_MODULE_REF[]) do REPL
425425
term_env = get(ENV, "TERM", @static Sys.iswindows() ? "" : "dumb")
426426
term = REPL.Terminals.TTYTerminal(term_env, stdin, stdout, stderr)
427-
banner == :no || REPL.banner(term, short=banner==:short)
427+
banner > 0 && REPL.banner(term, banner)
428428
if term.term_type == "dumb"
429429
repl = REPL.BasicREPL(term)
430430
quiet || @warn "Terminal not fully functional"
@@ -587,12 +587,11 @@ end
587587
function repl_main(_)
588588
opts = Base.JLOptions()
589589
interactiveinput = isa(stdin, Base.TTY)
590-
b = opts.banner
591-
auto = b == -1
592-
banner = b == 0 || (auto && !interactiveinput) ? :no :
593-
b == 1 || (auto && interactiveinput) ? :yes :
594-
:short # b == 2
595-
590+
banner = if opts.banner == -1
591+
10 * Int(interactiveinput)
592+
else
593+
Int(opts.banner)
594+
end
596595
quiet = (opts.quiet != 0)
597596
history_file = (opts.historyfile != 0)
598597
color_set = (opts.color != 0) # --color!=auto

doc/Manifest.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ deps = ["Unicode"]
9494
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
9595

9696
[[deps.REPL]]
97-
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
97+
deps = ["InteractiveUtils", "Markdown", "Sockets", "StyledStrings", "Unicode"]
9898
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
9999

100100
[[deps.Random]]
@@ -111,6 +111,9 @@ uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
111111
[[deps.Sockets]]
112112
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
113113

114+
[[deps.StyledStrings]]
115+
uuid = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
116+
114117
[[deps.Test]]
115118
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
116119
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

pkgimage.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ $(eval $(call stdlib_builder,InteractiveUtils,Markdown))
107107
# 3-depth packages
108108
$(eval $(call stdlib_builder,LibGit2_jll,MbedTLS_jll LibSSH2_jll Artifacts Libdl))
109109
$(eval $(call stdlib_builder,LibCURL_jll,LibSSH2_jll nghttp2_jll MbedTLS_jll Zlib_jll Artifacts Libdl))
110-
$(eval $(call stdlib_builder,REPL,InteractiveUtils Markdown Sockets Unicode))
110+
$(eval $(call stdlib_builder,REPL,InteractiveUtils Markdown Sockets StyledStrings Unicode))
111111
$(eval $(call stdlib_builder,SharedArrays,Distributed Mmap Random Serialization))
112112
$(eval $(call stdlib_builder,TOML,Dates))
113113
$(eval $(call stdlib_builder,Test,Logging Random Serialization InteractiveUtils))

src/jloptions.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,15 +440,20 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
440440
break;
441441
case opt_banner: // banner
442442
if (!strcmp(optarg, "yes"))
443-
jl_options.banner = 1;
443+
jl_options.banner = 10;
444444
else if (!strcmp(optarg, "no"))
445445
jl_options.banner = 0;
446446
else if (!strcmp(optarg, "auto"))
447447
jl_options.banner = -1;
448-
else if (!strcmp(optarg, "short"))
449-
jl_options.banner = 2;
450-
else
451-
jl_errorf("julia: invalid argument to --banner={yes|no|auto|short} (%s)", optarg);
448+
else {
449+
errno = 0;
450+
int bval = strtol(optarg, NULL, 10);
451+
if (errno == ERANGE)
452+
jl_errorf("julia: invalid argument to --banner={yes|no|0-100|auto} (%s)", optarg);
453+
else
454+
jl_options.banner = bval;
455+
break;
456+
}
452457
break;
453458
case opt_sysimage_native_code:
454459
if (!strcmp(optarg,"yes"))

stdlib/REPL/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "1.11.0"
66
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
77
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
88
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
9+
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
910
Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
1011

1112
[extras]

stdlib/REPL/src/REPL.jl

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ end
3434
Base.Experimental.@optlevel 1
3535
Base.Experimental.@max_methods 1
3636

37-
using Base.Meta, Sockets
37+
using Base.Meta, Sockets, StyledStrings
3838
import InteractiveUtils
3939

4040
export
@@ -1422,11 +1422,17 @@ ends_with_semicolon(code::AbstractString) = ends_with_semicolon(String(code))
14221422
ends_with_semicolon(code::Union{String,SubString{String}}) =
14231423
contains(_rm_strings_and_comments(code), r";\s*$")
14241424

1425-
function banner(io::IO = stdout; short = false)
1426-
if Base.GIT_VERSION_INFO.tagged_commit
1427-
commit_string = Base.TAGGED_RELEASE_BANNER
1425+
"""
1426+
banner(io::IO = stdout, maxsize:Int = 10)
1427+
1428+
Print the "Julia" informative banner to `io`, using a size variant no larger
1429+
than `maxsize`.
1430+
"""
1431+
function banner(io::IO = stdout, maxsize::Int = 10)
1432+
commit_string = if Base.GIT_VERSION_INFO.tagged_commit
1433+
Base.AnnotatedString(TAGGED_RELEASE_BANNER, :face => :shadow)
14281434
elseif isempty(Base.GIT_VERSION_INFO.commit)
1429-
commit_string = ""
1435+
styled""
14301436
else
14311437
days = Int(floor((ccall(:jl_clock_now, Float64, ()) - Base.GIT_VERSION_INFO.fork_master_timestamp) / (60 * 60 * 24)))
14321438
days = max(0, days)
@@ -1435,60 +1441,61 @@ function banner(io::IO = stdout; short = false)
14351441
commit = Base.GIT_VERSION_INFO.commit_short
14361442

14371443
if distance == 0
1438-
commit_string = "Commit $(commit) ($(days) $(unit) old master)"
1444+
styled"""Commit {grey:$commit} \
1445+
({warning:⌛ {italic:$days $unit}} old master)"""
14391446
else
14401447
branch = Base.GIT_VERSION_INFO.branch
1441-
commit_string = "$(branch)/$(commit) (fork: $(distance) commits, $(days) $(unit))"
1448+
styled"""{emphasis:$branch}/{grey:$commit} \
1449+
({italic:{success:{bold,(slant=normal):↑} $distance commits}, \
1450+
{warning:{(slant=normal):⌛} $days $unit}})"""
14421451
end
14431452
end
14441453

1445-
commit_date = isempty(Base.GIT_VERSION_INFO.date_string) ? "" : " ($(split(Base.GIT_VERSION_INFO.date_string)[1]))"
1446-
1447-
if get(io, :color, false)::Bool
1448-
c = Base.text_colors
1449-
tx = c[:normal] # text
1450-
jl = c[:normal] # julia
1451-
d1 = c[:bold] * c[:blue] # first dot
1452-
d2 = c[:bold] * c[:red] # second dot
1453-
d3 = c[:bold] * c[:green] # third dot
1454-
d4 = c[:bold] * c[:magenta] # fourth dot
1455-
1456-
if short
1457-
print(io,"""
1458-
$(d3)o$(tx) | Version $(VERSION)$(commit_date)
1459-
$(d2)o$(tx) $(d4)o$(tx) | $(commit_string)
1460-
""")
1461-
else
1462-
print(io,""" $(d3)_$(tx)
1463-
$(d1)_$(tx) $(jl)_$(tx) $(d2)_$(d3)(_)$(d4)_$(tx) | Documentation: https://docs.julialang.org
1464-
$(d1)(_)$(jl) | $(d2)(_)$(tx) $(d4)(_)$(tx) |
1465-
$(jl)_ _ _| |_ __ _$(tx) | Type \"?\" for help, \"]?\" for Pkg help.
1466-
$(jl)| | | | | | |/ _` |$(tx) |
1467-
$(jl)| | |_| | | | (_| |$(tx) | Version $(VERSION)$(commit_date)
1468-
$(jl)_/ |\\__'_|_|_|\\__'_|$(tx) | $(commit_string)
1469-
$(jl)|__/$(tx) |
1470-
1471-
""")
1472-
end
1473-
else
1474-
if short
1475-
print(io,"""
1476-
o | Version $(VERSION)$(commit_date)
1477-
o o | $(commit_string)
1478-
""")
1479-
else
1480-
print(io,"""
1481-
_
1482-
_ _ _(_)_ | Documentation: https://docs.julialang.org
1483-
(_) | (_) (_) |
1484-
_ _ _| |_ __ _ | Type \"?\" for help, \"]?\" for Pkg help.
1485-
| | | | | | |/ _` | |
1486-
| | |_| | | | (_| | | Version $(VERSION)$(commit_date)
1487-
_/ |\\__'_|_|_|\\__'_| | $(commit_string)
1488-
|__/ |
1489-
1490-
""")
1491-
end
1454+
commit_date = isempty(Base.GIT_VERSION_INFO.date_string) ? "" : styled" {light:($(split(Base.GIT_VERSION_INFO.date_string)[1]))}"
1455+
doclink = styled"{bold:Documentation\:} {(underline=grey),link={https://docs.julialang.org}:https\://docs.julialang.org}"
1456+
help = styled"Type {region,repl_prompt_help:?} for help, {region,repl_prompt_pkg:]?} for {(underline=grey),link={https://pkgdocs.julialang.org/}:Pkg} help."
1457+
1458+
size = min(if all(displaysize(io) .>= (5, 70)); 4 # Full size
1459+
elseif all(displaysize(io) .>= (5, 45)); 3 # Narrower
1460+
elseif all(displaysize(io) .>= (3, 50)); 2 # Tiny
1461+
else 1 end,
1462+
max(0, maxsize))
1463+
1464+
if size == 4 # Full size
1465+
print(io, styled"""
1466+
{bold,green:_}
1467+
{bold,blue:_} _ {bold:{red:_}{green:(_)}{magenta:_}} {shadow:│} $doclink
1468+
{bold,blue:(_)} | {bold:{red:(_)} {magenta:(_)}} {shadow:│}
1469+
_ _ _| |_ __ _ {shadow:│} $help
1470+
| | | | | | |/ _` | {shadow:│}
1471+
| | |_| | | | (_| | {shadow:│} Version {bold:$VERSION}$commit_date
1472+
_/ |\\__'_|_|_|\\__'_| {shadow:│} $commit_string
1473+
|__/ {shadow:│}
1474+
\n""")
1475+
elseif size == 3 # Rotated
1476+
print(io, styled"""
1477+
{bold,green:_}
1478+
{bold,blue:_} _ {bold:{red:_}{green:(_)}{magenta:_}}
1479+
{bold,blue:(_)} | {bold:{red:(_)} {magenta:(_)}}
1480+
_ _ _| |_ __ _
1481+
| | | | | | |/ _` |
1482+
| | |_| | | | (_| |
1483+
_/ |\\__'_|_|_|\\__'_|
1484+
|__/
1485+
1486+
$doclink
1487+
$help
1488+
1489+
Version {bold:$VERSION}$commit_date
1490+
$commit_string
1491+
\n""")
1492+
elseif size == 2 # Tiny
1493+
print(io, styled"""
1494+
{bold,green:o} {shadow:│} Version {bold:$VERSION}$commit_date
1495+
{bold:{red:o} {magenta:o}} {shadow:│} $commit_string
1496+
""")
1497+
elseif size == 1 # Text only
1498+
print(io, styled"""{bold:{blue:∴} {magenta:Julia} $VERSION}$commit_date\n""")
14921499
end
14931500
end
14941501

stdlib/REPL/src/precompile.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ end
1616
using Base.Meta
1717

1818
import Markdown
19+
import StyledStrings
1920

2021
## Debugging options
2122
# Disable parallel precompiles generation by setting `false`

test/cmdlineargs.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,10 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
235235
@test read(`$exename -e $p`, String) == "(0, -1)"
236236
@test read(`$exename -q -e $p`, String) == "(1, 0)"
237237
@test read(`$exename --quiet -e $p`, String) == "(1, 0)"
238+
@test read(`$exename --banner=auto -e $p`, String) == "(0, -1)"
238239
@test read(`$exename --banner=no -e $p`, String) == "(0, 0)"
239-
@test read(`$exename --banner=yes -e $p`, String) == "(0, 1)"
240-
@test read(`$exename --banner=short -e $p`, String) == "(0, 2)"
240+
@test read(`$exename --banner=yes -e $p`, String) == "(0, 10)"
241+
@test read(`$exename --banner=4 -e $p`, String) == "(0, 4)"
241242
@test read(`$exename -q --banner=no -e $p`, String) == "(1, 0)"
242243
@test read(`$exename -q --banner=yes -e $p`, String) == "(1, 1)"
243244
@test read(`$exename -q --banner=short -e $p`, String) == "(1, 2)"

test/precompile.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ precompile_test_harness(false) do dir
418418
Dict(Base.PkgId(Base.root_module(Base, :Markdown)) => Base.module_build_id(Base.root_module(Base, :Markdown))),
419419
# and their dependencies
420420
Dict(Base.PkgId(Base.root_module(Base, :Base64)) => Base.module_build_id(Base.root_module(Base, :Base64))),
421+
Dict(Base.PkgId(Base.root_module(Base, :StyledStrings)) => Base.module_build_id(Base.root_module(Base, :StyledStrings))),
421422
)
422423
@test Dict(modules) == modules_ok
423424

0 commit comments

Comments
 (0)