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

A more colourful menu #236

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 16 additions & 8 deletions src/ui.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ end

const debugcolors = (:nothing, :light_black, :yellow)
function usage(@nospecialize(view_cmd), optimize, iswarn, hide_type_stable, debuginfo, remarks, inline_cost, type_annotations, highlight)
colorize(iotmp, use_color::Bool, c::Char) = stringify(iotmp) do io
use_color ? printstyled(io, c; color=:cyan) : print(io, c)
colorize(iotmp, active_option::Bool, c::Char) = stringify(iotmp) do io
active_option ? printstyled(io, c; bold=true, color=:blue) : printstyled(io, c; color=:magenta)
end

colorize(iotmp, s::AbstractString; color::Symbol = :cyan) = stringify(iotmp) do io
printstyled(io, s; color)
end

io, iotmp = IOBuffer(), IOBuffer()
ioctx = IOContext(io, :color=>true)

println(ioctx, "Select a call to descend into or ↩ to ascend. [q]uit. [b]ookmark.")
println(ioctx, "Toggles: [",
println(ioctx,
colorize(iotmp, "Select a call to descend into or ↩ to ascend. [q]uit. [b]ookmark."; color=:green))
println(ioctx,
colorize(iotmp, "Toggles"), ": [",
colorize(iotmp, optimize, 'o'), "]ptimize, [",
colorize(iotmp, iswarn, 'w'), "]arn, [",
colorize(iotmp, hide_type_stable, 'h'), "]ide type-stable statements, [",
Expand All @@ -66,16 +72,18 @@ function usage(@nospecialize(view_cmd), optimize, iswarn, hide_type_stable, debu
colorize(iotmp, inline_cost, 'i'), "]nlining costs, [",
colorize(iotmp, type_annotations, 't'), "]ype annotations, [",
colorize(iotmp, highlight, 's'), "]yntax highlight for Source/LLVM/Native.")
println(ioctx, "Show: [",
println(ioctx,
colorize(iotmp, "Show"), ": [",
colorize(iotmp, view_cmd === cthulhu_source, 'S'), "]ource code, [",
colorize(iotmp, view_cmd === cthulhu_ast, 'A'), "]ST, [",
colorize(iotmp, view_cmd === cthulhu_typed, 'T'), "]yped code, [",
colorize(iotmp, view_cmd === cthulhu_llvm, 'L'), "]LVM IR, [",
colorize(iotmp, view_cmd === cthulhu_native, 'N'), "]ative code")
print(ioctx,
"""
Actions: [E]dit source code, [R]evise and redisplay
Advanced: dump [P]arams cache.""")
colorize(iotmp, "Actions"),
": [E]dit source code, [R]evise and redisplay\n",
colorize(iotmp, "Advanced"),
": dump [P]arams cache.")
return String(take!(io))
end

Expand Down
17 changes: 11 additions & 6 deletions test/terminal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ const keydict = Dict(:up => "\e[A",
if isdefined(Base, :active_repl)
@test Cthulhu.default_terminal() isa REPL.Terminals.TTYTerminal
end
colorize(use_color::Bool, c::Char) = Cthulhu.stringify() do io
use_color ? printstyled(io, c; color=:cyan) : print(io, c)
colorize(active_option::Bool, c::Char) = Cthulhu.stringify() do io
active_option ? printstyled(io, c; bold=true, color=:blue) : printstyled(io, c; color=:magenta)
end

colorize(s::AbstractString; color::Symbol = :cyan) = Cthulhu.stringify() do io
printstyled(io, s; color)
end

# Write a file that we track with Revise. Creating it programmatically allows us to rewrite it with
# different content
fn = tempname()
Expand Down Expand Up @@ -64,14 +69,14 @@ const keydict = Dict(:up => "\e[A",
@test occursin('[' * colorize(true, 'o') * "]ptimize", lines)
@test occursin('[' * colorize(true, 'T') * "]yped", lines)

@test occursin("\nSelect a call to descend into", lines) # beginning of the line
@test occursin('\n' * colorize("Select a call to descend into or ↩ to ascend. [q]uit. [b]ookmark."; color = :green), lines) # beginning of the line
@test occursin('•', lines)
write(in, 'o') # switch to unoptimized
lines = cread(out)
@test occursin("invoke simplef(::Float32,::Int32)::Float32", lines)
@test occursin(r"\(z = a \* a\)\u001B\[\d\dm::Float32\u001B\[39m", lines)
@test occursin('[' * colorize(false, 'o') * "]ptimize", lines)
@test occursin("\nSelect a call to descend into", lines) # beginning of the line
@test occursin('\n' * colorize("Select a call to descend into or ↩ to ascend. [q]uit. [b]ookmark."; color = :green), lines) # beginning of the line
@test occursin("• %1 = *(::Float32,::Float32)::Float32", lines)
# Call selection
write(in, keydict[:down])
Expand Down Expand Up @@ -103,7 +108,7 @@ const keydict = Dict(:up => "\e[A",
@test occursin(r"z.*::Float32", lines)
@test occursin(r"\nBody.*Float32", lines)
@test occursin('[' * colorize(true, 'w') * "]arn", lines)
@test occursin("\nSelect a call to descend into", lines) # beginning of the line
@test occursin('\n' * colorize("Select a call to descend into or ↩ to ascend. [q]uit. [b]ookmark."; color = :green), lines) # beginning of the line
@test occursin("• %1 = *(::Float32,::Float32)::Float32", lines)
# Source view
write(in, 'S')
Expand Down Expand Up @@ -146,7 +151,7 @@ const keydict = Dict(:up => "\e[A",
write(in, 'd'); cread(out)
write(in, 'L')
lines = cread(out)
@test occursin('[' * colorize(false, 'd') * "]ebuginfo", lines)
@test occursin("[d]ebuginfo", lines)
@test !occursin("┌ @ promotion.jl", lines)
# Native-code view
write(in, 'N')
Expand Down