mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-02 15:17:47 +00:00
chore: 🧹 update imgcat and add support for kitty (#1017)
This could be done better by exposing more options from the protocol, but it should be a good start!
This commit is contained in:
parent
14e3ecb139
commit
1f6ee6c86d
1 changed files with 122 additions and 65 deletions
|
@ -1,53 +1,101 @@
|
||||||
# https://iterm2.com/documentation-images.html
|
# https://iterm2.com/documentation-images.html
|
||||||
|
# https://sw.kovidgoyal.net/kitty/graphics-protocol/
|
||||||
|
|
||||||
|
def is-screen-term [] {
|
||||||
|
$env.TERM =~ ^screen
|
||||||
|
}
|
||||||
|
|
||||||
def print_osc [] {
|
def print_osc [] {
|
||||||
if $env.TERM == screen* {
|
if (is-screen-term) {
|
||||||
"\ePtmux;\e\e]"
|
return "\ePtmux;\e\e]"
|
||||||
} else {
|
|
||||||
"\e]"
|
|
||||||
}
|
}
|
||||||
|
ansi escape_right
|
||||||
}
|
}
|
||||||
|
|
||||||
def print_st [] {
|
def print_st [] {
|
||||||
if $env.TERM == screen* {
|
if (is-screen-term) {
|
||||||
"\a\e\\"
|
return "\a\e\\"
|
||||||
} else {
|
|
||||||
"\a"
|
|
||||||
}
|
}
|
||||||
|
"\a"
|
||||||
}
|
}
|
||||||
|
|
||||||
def --env b64_encode [fn] {
|
def b64_encode [fn] {
|
||||||
open $fn | encode base64
|
open -r $fn | encode base64
|
||||||
}
|
}
|
||||||
|
|
||||||
def --env b64_decode [fn] {
|
# Print an image using the iTerm protocol
|
||||||
$fn | decode base64
|
def print_iterm_image [
|
||||||
}
|
filename: path
|
||||||
|
base64contents: string
|
||||||
def print_image [
|
--inline
|
||||||
filename # Filename to convey to client
|
--print_filename
|
||||||
inline # 0 or 1
|
|
||||||
base64contents # Base64-encoded contents
|
|
||||||
print_filename # If non-empty, print the filename before outputting the image
|
|
||||||
] {
|
] {
|
||||||
let a = (print_osc)
|
|
||||||
let b = "1337;File="
|
|
||||||
let c = (if ($filename | length) > 0 {
|
|
||||||
let b64_enc_data = (b64_encode $filename)
|
let b64_enc_data = (b64_encode $filename)
|
||||||
|
let b64_dec_data = ($base64contents | decode base64)
|
||||||
|
let query = (
|
||||||
|
[(print_osc)]
|
||||||
|
| append "1337;File="
|
||||||
|
| append (
|
||||||
|
if ($filename | str length) > 0 {
|
||||||
$"name=($b64_enc_data);"
|
$"name=($b64_enc_data);"
|
||||||
})
|
}
|
||||||
let b64_dec_data = (b64_decode $base64contents)
|
)
|
||||||
let d = $"size=($b64_dec_data | bytes length)"
|
| append $"size=($b64_dec_data | bytes length)"
|
||||||
let e = $";inline=($inline)"
|
| append (
|
||||||
let f = ":"
|
if $inline {
|
||||||
let g = $base64contents
|
$";inline=1"
|
||||||
let h = print_st
|
}
|
||||||
let i = "\n"
|
)
|
||||||
let j = (if ($print_filename | length) > 0 {
|
| append ":"
|
||||||
print -n $filename
|
| append $base64contents
|
||||||
})
|
| append (print_st)
|
||||||
|
| append "\n"
|
||||||
|
)
|
||||||
|
|
||||||
[ $a $b $c $d $e $f $g $h $i $j ] | str join
|
if $print_filename {
|
||||||
|
print -n $filename
|
||||||
|
}
|
||||||
|
|
||||||
|
$query | str join
|
||||||
|
}
|
||||||
|
|
||||||
|
# Print an image using the Kitty protocol
|
||||||
|
def print_kitty_image [
|
||||||
|
filename: path
|
||||||
|
base64contents: string
|
||||||
|
--print_filename
|
||||||
|
] {
|
||||||
|
|
||||||
|
let b64_dec_data = ($base64contents | decode base64)
|
||||||
|
let size = ($b64_dec_data | bytes length)
|
||||||
|
|
||||||
|
let result = (
|
||||||
|
["\e"]
|
||||||
|
| append "_G"
|
||||||
|
| append $"a=T,f=100,t=d,s=($size);"
|
||||||
|
| append $base64contents
|
||||||
|
| append (ansi st)
|
||||||
|
)
|
||||||
|
|
||||||
|
if $print_filename {
|
||||||
|
print -n $filename
|
||||||
|
}
|
||||||
|
|
||||||
|
$result | str join
|
||||||
|
}
|
||||||
|
|
||||||
|
# Determine the appropriate protocol and print the image
|
||||||
|
def print_image [
|
||||||
|
filename: path
|
||||||
|
base64contents: string
|
||||||
|
--inline # Only for iTerm; ignored for Kitty
|
||||||
|
--print_filename
|
||||||
|
] {
|
||||||
|
if (detect_kitty_support) {
|
||||||
|
print_kitty_image $filename $base64contents --print_filename=$print_filename
|
||||||
|
} else {
|
||||||
|
print_iterm_image $filename $base64contents --inline=$inline --print_filename=$print_filename
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def error [] {
|
def error [] {
|
||||||
|
@ -59,32 +107,41 @@ def show_help [] {
|
||||||
print " or: cat filename | imgcat"
|
print " or: cat filename | imgcat"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Detect Kitty support by querying the terminal
|
||||||
|
# https://sw.kovidgoyal.net/kitty/graphics-protocol/#querying-support-and-available-transmission-mediums
|
||||||
|
def detect_kitty_support [] {
|
||||||
|
let is_kitty_supported = $"(ansi esc)_Gi=1234,s=1,v=1,a=q,t=d,f=24;AAAA(ansi st)"
|
||||||
|
return (term query $is_kitty_supported --terminator (ansi st) | "OK" in ($in | decode))
|
||||||
|
}
|
||||||
|
|
||||||
|
export def detected [] {
|
||||||
|
print $"Kitty support: (detect_kitty_support)"
|
||||||
|
print $"Is Screen term: (is-screen-term)"
|
||||||
|
}
|
||||||
|
|
||||||
# imgcat.nu shows images in your terminal if your terminal supports it
|
# imgcat.nu shows images in your terminal if your terminal supports it
|
||||||
def imgcat [
|
# it uses either the Iterm or the Kitty protocol
|
||||||
--help(-h) # Help/Usage message
|
export def main [
|
||||||
--print(-p) # Print filename
|
--help (-h) # Help/Usage message
|
||||||
--url(-u) # Use a URL
|
--print (-p) # Print filename
|
||||||
|
--url (-u) # Use a URL
|
||||||
|
--inline (-i) # Inline image (only for iTerm; ignored for Kitty)
|
||||||
filename # The filename to show
|
filename # The filename to show
|
||||||
] {
|
] {
|
||||||
if $help {
|
if $help {
|
||||||
show_help
|
show_help
|
||||||
}
|
}
|
||||||
|
|
||||||
let print_filename = (
|
|
||||||
if $print {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
let url_img = (
|
let url_img = (
|
||||||
if $url {
|
if $url {
|
||||||
let encoded_image = (b64_encode (http get $url))
|
let encoded_image = (b64_encode (http get $url))
|
||||||
print_image $url 1 $encoded_image $print_filename
|
print_image $url $encoded_image --inline=$inline --print_filename=$print
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
if ($filename | path exists) {
|
if ($filename | path exists) {
|
||||||
print_image $filename 1 (b64_encode $filename) $print_filename
|
let filename = ($filename | path expand)
|
||||||
|
print_image $filename (b64_encode $filename) --inline=$inline --print_filename=$print
|
||||||
} else {
|
} else {
|
||||||
print $"imgcat: ($filename): No such file or directory"
|
print $"imgcat: ($filename): No such file or directory"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue