From 377654ac7e3ce55a36122c83e800784815f1c045 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Sun, 20 Apr 2025 22:02:44 -0400 Subject: [PATCH] try to get the Pkg.test subprocess to detect sixel compat --- src/Sixel.jl | 12 ++++++++---- src/terminaltools.jl | 25 +++++++++++++++++-------- test/runtests.jl | 2 ++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/Sixel.jl b/src/Sixel.jl index f05a25a..b3c7a64 100644 --- a/src/Sixel.jl +++ b/src/Sixel.jl @@ -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 diff --git a/src/terminaltools.jl b/src/terminaltools.jl index e23b92b..e247533 100644 --- a/src/terminaltools.jl +++ b/src/terminaltools.jl @@ -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) @@ -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\[(?[0-9]*);(?[0-9]*)R")) -end # moudle +end # module diff --git a/test/runtests.jl b/test/runtests.jl index 71ba9ac..13df3a6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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)