Skip to content

Commit

Permalink
Add PygmentizeConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Jul 22, 2018
1 parent 581b2a9 commit 681ee22
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/ColorfulCodeGen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,28 @@ else
end
end

const _PYGMENTIZE = `pygmentize -f terminal256`
const HIGHLIGHTERS = let
pygmentize = _PYGMENTIZE
mutable struct PygmentizeConfig
command::Cmd
options::Dict{Any, Cmd}
end

const PYGMENTIZE = PygmentizeConfig(
`pygmentize -f terminal256`,
Dict(
:code_lowered => `$pygmentize -l julia`,
:code_typed => `$pygmentize -l julia`,
:code_warntype => `$pygmentize -l julia`,
:code_llvm => `$pygmentize -l llvm`,
:code_native => `$pygmentize -l asm`,
code_lowered => `-l julia`,
code_typed => `-l julia`,
code_warntype => `-l julia`,
code_llvm => `-l llvm`,
code_native => `-l asm`,
# TODO: "-l nasm" when syntax=:intel
)
Expr => `-l julia`,
MD => `-l md`,
),
)

function get_command(key, config::PygmentizeConfig = PYGMENTIZE)
option = config.options[key]
return `$(config.command) $option`
end

function with_highlighter(f, proc_out, highlighter)
Expand All @@ -40,8 +51,8 @@ function with_highlighter(f, proc_out, highlighter)
end

highlight(x) = highlight(stdout, x)
highlight(io::IO, x::Expr) = showpiped(io, x, `$_PYGMENTIZE -l julia`)
highlight(io::IO, x::MD) = showpiped(io, x, `$_PYGMENTIZE -l md`)
highlight(io::IO, x::Expr) = showpiped(io, x, get_command(Expr))
highlight(io::IO, x::MD) = showpiped(io, x, get_command(MD))

showpiped(thing, cmd::Cmd) = showpiped(stdout, thing, cmd)

Expand All @@ -64,7 +75,7 @@ for fname in _with_io
Colored version of `$(fname)([io,] args...; kwargs...)`.
""")
function $(colored_name)(io::IO, args...;
cmd = HIGHLIGHTERS[$(QuoteNode(fname))],
cmd = get_command($fname),
kwargs...)
with_highlighter(io, cmd) do proc_in
$fname(proc_in, args...; kwargs...)
Expand All @@ -84,7 +95,7 @@ for fname in _no_io
Colored version of `$(fname)(args...; kwargs...)`.
""")
function $(colored_name)(io::IO, args...;
cmd = HIGHLIGHTERS[$(QuoteNode(fname))],
cmd = get_command($fname),
kwargs...)
results = $fname(args...; kwargs...)
result = length(results) == 1 ? results[1] : results
Expand Down

0 comments on commit 681ee22

Please sign in to comment.