Class: Imgcat
- Inherits:
-
Object
- Object
- Imgcat
- Defined in:
- lib/imgcat.rb,
lib/imgcat/version.rb
Overview
Defined Under Namespace
Classes: Error
Constant Summary collapse
- RELEVANT_ENV =
%w[TERM TERM_PROGRAM LC_TERMINAL]
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#tty ⇒ Object
readonly
Returns the value of attribute tty.
Instance Method Summary collapse
- #display(image, name: nil, width: nil, height: nil, inline: true) ⇒ Object
- #guess_terminal_program ⇒ Object
-
#initialize(tty = STDOUT, env: ENV) ⇒ Imgcat
constructor
A new instance of Imgcat.
- #print_osc ⇒ Object
- #print_st ⇒ Object
- #supported? ⇒ Boolean
- #tmux? ⇒ Boolean
Constructor Details
#initialize(tty = STDOUT, env: ENV) ⇒ Imgcat
Returns a new instance of Imgcat.
15 16 17 18 |
# File 'lib/imgcat.rb', line 15 def initialize(tty = STDOUT, env: ENV) @tty = tty @env = env.slice(*RELEVANT_ENV) end |
Instance Attribute Details
#tty ⇒ Object (readonly)
Returns the value of attribute tty.
14 15 16 |
# File 'lib/imgcat.rb', line 14 def tty @tty end |
Instance Method Details
#display(image, name: nil, width: nil, height: nil, inline: true) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/imgcat.rb', line 39 def display(image, name: nil, width: nil, height: nil, inline: true) print_osc @tty.printf "1337;File=inline=%i", (inline ? 1 : 0) @tty.printf ";size=%d", image.length #[ -n "$1" ] && printf ";name=%s" "$(printf "%s" "$1" | b64_encode)" #[ -n "$5" ] && printf ";width=%s" "$5" #[ -n "$6" ] && printf ";height=%s" "$6" #[ -n "$7" ] && printf ";preserveAspectRatio=%s" "$7" #[ -n "$8" ] && printf ";type=%s" "$8" @tty.printf ":%s", Base64.encode64(image) print_st @tty.puts #[ "$4" == "1" ] && echo "$1" #has_image_displayed=t end |
#guess_terminal_program ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/imgcat.rb', line 20 def guess_terminal_program term = ENV["TERM"] term_program = ENV["TERM_PROGRAM"] if term_program == "iTerm.app" || ENV["LC_TERMINAL"] == "iTerm2" :iterm elsif term == "xterm-kitty" :kitty nil # unsupported for now elsif term_program == "WezTerm" :wezterm else nil end end |
#print_osc ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/imgcat.rb', line 60 def print_osc if tmux? @tty.write "\033Ptmux;\033\033]" else @tty.write "\033]" end end |
#print_st ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/imgcat.rb', line 68 def print_st if tmux? @tty.write "\a\033\\" else @tty.write "\a" end end |
#supported? ⇒ Boolean
35 36 37 |
# File 'lib/imgcat.rb', line 35 def supported? !!guess_terminal_program end |
#tmux? ⇒ Boolean
55 56 57 58 |
# File 'lib/imgcat.rb', line 55 def tmux? term = ENV["TERM"] term.start_with?("screen") || term.start_with?("tmux") end |