-
Notifications
You must be signed in to change notification settings - Fork 0
/
nyancat
executable file
·211 lines (191 loc) · 6.94 KB
/
nyancat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env bash
## source: https://copypastatext.com/nyan-cat-ascii/
nyancat="\
\e[1;31m_________________________________________▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄\e[0m\n\
\e[1;31m________________________________________█░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░█\e[0m\n\
\e[1;33m________________________________________█░▒▒▒▒▒▒▒▒▒▒▄▀▀▄▒▒▒░░█▄▀▀▄\e[0m\n\
\e[1;33m___________________________________▄▄___█░▒▒▒▒▒▒▒▒▒▒█▓▓▓▀▄▄▄▄▀▓▓▓█\e[0m\n\
\e[1;32m_________________________________█▓▓█▄▄█░▒▒▒▒▒▒▒▒▒▄▀▓▓▓▓▓▓▓▓▓▓▓▓▀▄\e[0m\n\
\e[1;32m__________________________________▀▄▄▓▓█░▒▒▒▒▒▒▒▒▒█▓▓▓▄█▓▓▓▄▓▄█▓▓█\e[0m\n\
\e[1;36m______________________________________▀▀█░▒▒▒▒▒▒▒▒▒█▓▒▒▓▄▓▓▄▓▓▄▓▒▒█\e[0m\n\
\e[1;36m_______________________________________▄█░░▒▒▒▒▒▒▒▒▒▀▄▓▓▀▀▀▀▀▀▀▓▄▀\e[0m\n\
\e[1;35m_____________________________________▄▀▓▀█▄▄▄▄▄▄▄▄▄▄▄▄██████▀█▀▀\e[0m\n\
\e[1;35m_____________________________________█▄▄▀_█▄▄▀_______█▄▄▀_▀▄▄█\e[0m\n\
"
help="\n
\e[1musage:\e[0m\tnyancat [?opts] [file] [?line]\n\n
\e[1mdescription:\e[0m\n
\tPretty print source code files in the command line.\n\n
\e[1mdependencies:\e[0m\n
\tPygments:\t\e[1;35mpip install Pygments\e[0m\n\n
\e[1mopts\e[0m:
\t-s= (--style=)\t\tThe syntax highlighting style (native)\n
\t-a (--available-styles)\tList available styles\n
\t-l (--lines)\t\tInclude line numbers in output\n
\t-x (--example)\t\tExample code with style\n
\t-v (--version)\t\tDisplay version of nyancat\n
\t-d (--debug)\t\tPrint the comand unevaluated\n
\t-h (--help)\t\tDisplay this text\n\n
\e[1mexamples:\e[0m\n
\t\e[32m#Print a whole file with syntax highlighting\e[0m\n
\tnyancat file.c
\n\n
\t\e[32m#Print a specific line of a file with syntax highlighting\e[0m\n
\tnyancat file.c 12
\n\n
\t\e[32m#Set the style of the output\e[0m\n
\tnyancat -s=nord file.c 12\n\n
\e[1;36mPowered by Pygments\e[0m"
shorthelp="usage:\tnyancat [?opts] [file] [?line]\n
\tnyancat -h for help
"
version="0.1.0"
# By default show shorthelp
cmd="echo -e $shorthelp"
# Default style is native use nyancat -a to list all available styles
style="native"
# Target file
file=""
# Target line
line=""
# Debug mode
debug=0
# Show example with style
example=0
# Show line numbers?
lines=0
# Pretty Print Command
pp_cmd="pygmentize -g -O style=[style] [file]"
# Pretty Print Command with lines
pp_line_cmd='pygmentize -g -O style=[style],linenos=1 [file]'
# Pretty Print Command with lines at line
pp_at_line_cmd='pygmentize -g -O style=[style],linenos=1 [file] | sed -n [line]p'
# Print help if 1
# Styles available in the latest Pygments
# Pygments version 2.13.0, (c) 2006-2022 by Georg Brandl, Matthäus Chajdas and contributors.
# This should be cleaned up to use the users version of pigments
available_styles='default \nemacs \nfriendly \nfriendly_grayscale \ncolorful \nautumn \nmurphy \nmanni \nmaterial \nmonokai \nperldoc \npastie \nborland \ntrac \nnative \nfruity \nbw \nvim \nvs \ntango \nrrt \nxcode \nigor \nparaiso-light \nparaiso-dark \nlovelace \nalgol \nalgol_nu \narduino \nrainbow_dash \nabap \nsolarized-dark \nsolarized-light \nsas \nstaroffice \nstata \nstata-light \nstata-dark \ninkpot \nzenburn \ngruvbox-dark \ngruvbox-light \ndracula \none-dark \nlilypond \nnord \nnord-darker \ngithub-dark'
for i in "$@"
do
case $i in
-v|--version) # print version and exit
echo -e "nyancat_v"$version
exit 0
;;
-h|--help) # print title nyancat help and exit
echo -e " \e[1mnyancat:\e[0m cat + syntax highlighting\n"$nyancat$help
exit 0
;;
-d|--debug) # enable debug mode
debug=1
;;
-l|--lines) # print lines
lines=1
;;
-x|--example) # display example code rather than file
example=1
;;
-s=*|--style=*) # select style, default native
style="${i#*=}"
;;
-a|--available-styles) # list available styles in Pygments 2.13
echo -e $available_styles
exit 0
;;
*) # Check for file and line
if [ "$file" = "" ]
then
file=$i
if [ ! -f $file ]
then
echo -e "\e[1;31mError! \e[4;31m$file\e[0m\e[1;31m is not a file.\e[0m"
exit 1
fi
elif [ "$line" = "" ]
then
line=$i
else # if there are more than two additional args exit on error
echo -e "ERROR: unrecognized arg $i\n"
echo -e $help
exit 1
fi
;;
esac
done
# Sample C text, used for example
# In future expand this to other languages
c_example_text="// nyancat c example for style=[style]\n\
// [cmd]\n\n\
#include <stdio.h>\n\
\n\
int main (void) {\n\
\tprintf(\"Hello World!\");\n\
\treturn 0;\n\
}"
# Generate the cmd for printing a whole file
print_file (){
# test if it is a file
cmd_tmp=$pp_cmd
if [ $lines -eq 1 ]
then
cmd_tmp=$pp_line_cmd
fi
cmd=$(echo -e \
$cmd_tmp | \
sed "s;\\[style\\];$style;g" | \
sed "s;\\[file\\];$file;g")
}
# Generate the cmd for printing a specific line
# Specific lines are always printed wiht line numbers
print_line (){
cmd=$(echo -e \
$pp_at_line_cmd | \
sed "s;\\[style\\];$style;g" | \
sed "s;\\[file\\];$file;g" | \
sed "s;\\[line\\];$line;g")
}
# Print an example
print_example (){
# we could pull this logic out as its also present in print_file
cmd_tmp=$pp_cmd
if [ $lines -eq 1 ]
then
cmd_tmp=$pp_line_cmd
fi
file="\\/tmp\\/nyancat.c"
if [ $debug -eq 1 ]
then
echo "Printing example: $pp_cmd"
fi
first=$(echo $cmd_tmp | sed s/\\[style\\]/$style/g)
if [ $debug -eq 1 ]
then
echo "First substitution: $first"
fi
second=$(echo $first | sed s/\\[file\\]/$file/g)
if [ $debug -eq 1 ]
then echo "Second substitution: $second"
fi
cmd=$second
echo -e $c_example_text | sed s/\\[style\\]/$style/g | sed -e "s;\\[cmd\\];$cmd;g" > /tmp/nyancat.c
}
nofile=0
if [ $example -eq 1 ]; then # the -x arg is passed in always print example
print_example
elif [ "$file" = "" ]; then # no file arg we should print help
nofile=1
# no file or
elif [ "$line" = "" ]; then # file is present but not line print whole file
print_file
else # file and line are present print single line of file
print_line
fi
if [ $debug -eq 1 ]
then
echo -e "$cmd" # in debug mode print the command
elif [ $nofile -eq 1 ]
then
$cmd # no file or examlpe, print help
else
eval $cmd # eval cmd in all other cases
fi