Skip to content

Commit

Permalink
🐛 Bypass pager calls in pager> REPL mode
Browse files Browse the repository at this point in the history
If we are in pager> REPL mode, we should bypass all other pager calls.
Otherwise, we freeze the REPL until the user types CTRL-D.

Closes #40
  • Loading branch information
ronisbr committed Feb 13, 2025
1 parent 5f754da commit 1edfd0c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
15 changes: 14 additions & 1 deletion src/TerminalPager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,20 @@ function pager(obj::Any; kwargs...)
return pager(str; kwargs...)
end

pager(obj::AbstractString; kwargs...) = return _pager(obj; kwargs...)
function pager(obj::AbstractString; kwargs...)
# If we have a context key called `bypass_pager` with the value `true`, we must not call
# the pager because we are in the pager> REPL mode. Hence, we we call the pager, it
# locks the screen until the user types CTRL-D. For more information, see:
#
# https://github.com/ronisbr/TerminalPager.jl/issues/40
#
if get(stdout, :bypass_pager, false)
print(obj)
return nothing
end

return _pager(obj; kwargs...)
end

const less = pager

Expand Down
15 changes: 11 additions & 4 deletions src/helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,17 @@ Capture the `stdout` generated by `ex_in` and show inside a pager.
"""
macro stdout_to_pager(ex_in)
ex_out = quote
hascolor = get(stdout, :color, true)
old_stdout = stdout
buf = IOBuffer()
io = IOContext(buf, :color => hascolor, :limit => false)
hascolor = get(stdout, :color, true)
bypass_pager = get(stdout, :bypass_pager, false)
old_stdout = stdout
buf = IOBuffer()

io = IOContext(
buf,
:bypass_pager => bypass_pager,
:color => hascolor,
:limit => false,
)

try
Base.eval(:(stdout = $io))
Expand Down
15 changes: 11 additions & 4 deletions src/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,19 @@ function _tp_mode_do_cmd(repl::REPL.AbstractREPL, input::String)
old_stdout = stdout

try
# Create a buffer that will replace `stdout`.
# Create a buffer that will replace `stdout`. Notice that we add a context key
# called `bypass_pager` with value `true`. All the commands we call in this mode
# will have its output handled to the pager. Hence, if a command also calls a pager,
# we must only return the object. Otherwise, the section freezes until the user
# press CTRL-D. For more information, see:
#
# https://github.com/ronisbr/TerminalPager.jl/issues/40
buf = IOBuffer()
io = IOContext(
io = IOContext(
IOContext(buf, stdout),
:displaysize => displaysize(stdout),
:limit => false,
:bypass_pager => true,
:displaysize => displaysize(stdout),
:limit => false,
)

# Redirect `stdout` to the new buffer.
Expand Down

0 comments on commit 1edfd0c

Please sign in to comment.