Skip to content
Draft
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
12 changes: 8 additions & 4 deletions src/Sixel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ include("frontend/fileio.jl")

# Ref: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
"""
is_sixel_supported(tty=stdout)::Bool
is_sixel_supported()::Bool

Check if given terminal `tty` supports sixel format.
Check if the current terminal supports sixel format.

!!! warning
(Experiment) The return value is not fully tested on all terminals and all platforms.
"""
function is_sixel_supported(tty::Base.TTY=stdout)
'4' in TerminalTools.query_terminal("\033[0c", tty)
function is_sixel_supported()
return '4' in TerminalTools.query_terminal("\033[>c")
end
function is_sixel_supported(tty::TTY)
return '4' in TerminalTools.query_terminal("\033[>c", tty)
end
is_sixel_supported(ioc::IOContext) = is_sixel_supported(ioc.io)
is_sixel_supported(io::IO) = false

end # module
25 changes: 17 additions & 8 deletions src/terminaltools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,30 @@ function with_raw(f, tty::Terminals.TTYTerminal)
end

query_terminal(msg, io::IO; kwargs...) = ""
query_terminal(msg, regex, io::IO; kwargs...) = ("", )
query_terminal(msg, regex::Regex, io::IO; kwargs...) = ("", )
query_terminal(msg; kwargs...) = query_terminal(msg, Terminals.TTYTerminal("", stdin, stdout, stderr); kwargs...)
function query_terminal(msg, tty::TTY; timeout=1)
term = Terminals.TTYTerminal("", stdin, tty, stderr)
return query_terminal(msg, term; timeout=timeout)
end
function query_terminal(msg, term::Terminals.TTYTerminal; timeout=1)
@show term
try
timeout_call(timeout; pollint=timeout/100) do
return timeout_call(()->
with_raw(term) do
write(tty, msg)
return transcode(String, readavailable(tty))
write(term.out, msg)
flush(term.out)
sleep(0.5)
@info "here"
return String(read(term.out))
end
end
, timeout; pollint=timeout/10)
catch e
e isa TimeoutException && return ""
@debug "Error: $e" ex=(e, catch_backtrace())
return "" # on timeout or error
end
end
function query_terminal(msg, regex, tty::TTY; kwargs...)
function query_terminal(msg, regex::Regex, tty::TTY; kwargs...)
response = query_terminal(msg, tty; kwargs...)
m = match(regex, response)
isnothing(m) ? ("", ) : Tuple(m.captures)
Expand All @@ -76,4 +85,4 @@ end
# get_text_area(tty=stdout) = displaysize(tty)
# get_cursor_position(tty=stdout) = parse.(Int, query_terminal("\033[6n", r"\033\[(?<row>[0-9]*);(?<col>[0-9]*)R"))

end # moudle
end # module
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ using ImageQualityIndexes
using LinearAlgebra
using FileIO, TestImages

@show stdin stdout stderr
@show Sixel.TerminalTools.query_terminal("\033[>c")
sixel_output = Sixel.is_sixel_supported()
sixel_output || @info "Current terminal does not support sixel format sequence. Display tests to stdout will be marked as broken."
function test_sixel_display(f)
Expand Down
Loading