Skip to content

Commit a17a62a

Browse files
authored
Merge pull request #5 from ArkieCoder/kittysupport
add support for Kitty
2 parents c5343b5 + df5ede8 commit a17a62a

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

install.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Detects dependencies and installs the Ruby Primer app.
55

66
REQUIRED_RUBY_VERSION="3.2"
7-
REQUIRED_UTILITIES=("pandoc" "xelatex")
7+
REQUIRED_UTILITIES=("pandoc" "xelatex" "convert")
88
FONT_NAME="IBM Plex Serif Light"
99

1010
# Colors for output
@@ -112,19 +112,19 @@ if [ ${#missing_utils[@]} -ne 0 ]; then
112112
echo -e "${YELLOW}Missing utilities: ${missing_utils[*]}${NC}"
113113
case "$PACKAGE_MANAGER" in
114114
brew)
115-
echo "Install with: brew install pandoc mactex"
115+
echo "Install with: brew install pandoc mactex imagemagick"
116116
echo "Note: Ensure IBM Plex Serif Light font is available (included in mactex or install separately)."
117117
;;
118118
apt)
119-
echo "Install with: sudo apt update && sudo apt install pandoc texlive-latex-base texlive-fonts-recommended texlive-xelatex"
119+
echo "Install with: sudo apt update && sudo apt install pandoc texlive-latex-base texlive-fonts-recommended texlive-xelatex imagemagick"
120120
echo "Note: Ensure IBM Plex Serif Light font is installed (may require additional font packages)."
121121
;;
122122
yum)
123-
echo "Install with: sudo yum install pandoc texlive texlive-fonts"
123+
echo "Install with: sudo yum install pandoc texlive texlive-fonts ImageMagick"
124124
echo "Note: Ensure IBM Plex Serif Light font is installed."
125125
;;
126126
*)
127-
echo "Please install pandoc and xelatex manually."
127+
echo "Please install pandoc, xelatex, and imagemagick manually."
128128
echo "Note: Ensure IBM Plex Serif Light font is available for xelatex."
129129
;;
130130
esac

lib/stringformat.rb

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ def format(color: 'white')
33
require 'tempfile'
44
require 'base64'
55

6+
# Terminal detection
7+
def kitty?
8+
ENV['TERM'] == 'xterm-kitty' || ENV['TERM_PROGRAM'] == 'Kitty'
9+
end
10+
11+
def iterm2?
12+
ENV['TERM_PROGRAM'] == 'iTerm.app'
13+
end
14+
615
# Get terminal columns, similar to tput cols
716
cols = `tput cols`.to_i
817
em = (cols * 0.60).to_f
@@ -38,15 +47,42 @@ def format(color: 'white')
3847
system("pandoc --pdf-engine=xelatex -V mainfont=\"IBM Plex Serif Light\" -f commonmark_x #{tmp_file.path} -o #{pdf_file} >/dev/null 2>&1")
3948

4049
if File.exist?(pdf_file)
41-
pdf_data = File.read(pdf_file)
42-
base64_data = Base64.strict_encode64(pdf_data)
43-
# Clean up
44-
tmp_file.unlink
45-
File.unlink(pdf_file)
46-
# Return iTerm2 inline image sequence
47-
"\e]1337;File=inline=1;preserveAspectRatio=1:#{base64_data}\a"
50+
if kitty?
51+
# Convert PDF to PNG for Kitty
52+
png_file = tmp_file.path.sub('.md', '.png')
53+
system("convert #{pdf_file} #{png_file} >/dev/null 2>&1")
54+
if File.exist?(png_file)
55+
png_data = File.read(png_file)
56+
base64_data = Base64.strict_encode64(png_data)
57+
# Clean up
58+
tmp_file.unlink
59+
File.unlink(pdf_file)
60+
File.unlink(png_file)
61+
# Return Kitty inline image sequence
62+
"\e_Gf=100,a=T,t=d;#{base64_data}\e\\"
63+
else
64+
# Fallback if PNG conversion fails
65+
tmp_file.unlink
66+
File.unlink(pdf_file)
67+
self
68+
end
69+
elsif iterm2?
70+
pdf_data = File.read(pdf_file)
71+
base64_data = Base64.strict_encode64(pdf_data)
72+
# Clean up
73+
tmp_file.unlink
74+
File.unlink(pdf_file)
75+
# Return iTerm2 inline image sequence
76+
"\e]1337;File=inline=1;preserveAspectRatio=1:#{base64_data}\a"
77+
else
78+
# Fallback for other terminals
79+
tmp_file.unlink
80+
File.unlink(pdf_file)
81+
self
82+
end
4883
else
4984
# Fallback if PDF generation fails
85+
tmp_file.unlink
5086
self
5187
end
5288
end

0 commit comments

Comments
 (0)