snibbets 2.0.32 → 2.0.34
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -0
- data/Gemfile.lock +26 -16
- data/README.md +1 -1
- data/bin/snibbets +7 -6
- data/lib/snibbets/colors.rb +329 -0
- data/lib/snibbets/highlight.rb +10 -4
- data/lib/snibbets/lexers.rb +9 -0
- data/lib/snibbets/lexers_db.rb +162 -1
- data/lib/snibbets/os.rb +3 -3
- data/lib/snibbets/string.rb +23 -14
- data/lib/snibbets/version.rb +1 -1
- data/lib/snibbets.rb +22 -18
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4bfb107dedc2d689ec1ed371dcaf1ef7fb240218af43831856acb552789a0ce2
|
4
|
+
data.tar.gz: 482355a0c4a27d707d92633c47adeb2be0db027a37311412cb903f7d47cdb0b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40afbc2d33dc562db0e5e21ec7fe40fc10872f279c1bc95b67deb6aac9cff90e663abc0100e49c2a08ea4500e0e2eb4f442bcf34259a54b0b901cf53c883c7ce
|
7
|
+
data.tar.gz: 072cccc7e2c90dc557f4af25b9338a7cec21b00c0545b5155b07f413d6560b7d964008a8919a638b0c0b67089ccd12d568951c314c75c29d896694a3de45e19e
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,30 @@
|
|
1
|
+
### 2.0.34
|
2
|
+
|
3
|
+
2023-06-17 09:17
|
4
|
+
|
5
|
+
#### IMPROVED
|
6
|
+
|
7
|
+
- If a section of a snippet contains no code blocks, output the whole thing
|
8
|
+
|
9
|
+
#### FIXED
|
10
|
+
|
11
|
+
- Correct error if searching name only and no results are returned
|
12
|
+
|
13
|
+
### 2.0.33
|
14
|
+
|
15
|
+
2023-04-26 10:23
|
16
|
+
|
17
|
+
#### IMPROVED
|
18
|
+
|
19
|
+
- More coloring for prompts and messages
|
20
|
+
- Colorize headers in multi-snippet output
|
21
|
+
- If skylighting highlighting is enabled and the specified
|
22
|
+
- Add some normalization for some skylighting lexers
|
23
|
+
|
24
|
+
#### FIXED
|
25
|
+
|
26
|
+
- Highlighting returns empty if language contains non-alphanumeric characters
|
27
|
+
|
1
28
|
### 2.0.32
|
2
29
|
|
3
30
|
2023-04-25 17:14
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
snibbets (2.0.
|
4
|
+
snibbets (2.0.34)
|
5
5
|
mdless (~> 1.0, >= 1.0.32)
|
6
6
|
tty-reader (~> 0.9, >= 0.9.0)
|
7
7
|
tty-which (~> 0.5, >= 0.5.0)
|
@@ -17,47 +17,50 @@ GEM
|
|
17
17
|
gem-release (2.2.2)
|
18
18
|
json (2.6.3)
|
19
19
|
language_server-protocol (3.17.0.3)
|
20
|
-
|
20
|
+
lint_roller (1.0.0)
|
21
|
+
mdless (1.0.33)
|
21
22
|
multi_json (1.15.0)
|
22
|
-
parallel (1.
|
23
|
+
parallel (1.23.0)
|
23
24
|
parse_gemspec (1.0.0)
|
24
25
|
parse_gemspec-cli (1.0.0)
|
25
26
|
multi_json
|
26
27
|
parse_gemspec
|
27
28
|
thor
|
28
|
-
parser (3.2.2.
|
29
|
+
parser (3.2.2.3)
|
29
30
|
ast (~> 2.4.1)
|
31
|
+
racc
|
32
|
+
racc (1.7.1)
|
30
33
|
rainbow (3.1.1)
|
31
34
|
rake (13.0.6)
|
32
35
|
rdoc (6.3.3)
|
33
|
-
regexp_parser (2.
|
36
|
+
regexp_parser (2.8.1)
|
34
37
|
rexml (3.2.5)
|
35
38
|
rspec (3.12.0)
|
36
39
|
rspec-core (~> 3.12.0)
|
37
40
|
rspec-expectations (~> 3.12.0)
|
38
41
|
rspec-mocks (~> 3.12.0)
|
39
|
-
rspec-core (3.12.
|
42
|
+
rspec-core (3.12.2)
|
40
43
|
rspec-support (~> 3.12.0)
|
41
|
-
rspec-expectations (3.12.
|
44
|
+
rspec-expectations (3.12.3)
|
42
45
|
diff-lcs (>= 1.2.0, < 2.0)
|
43
46
|
rspec-support (~> 3.12.0)
|
44
47
|
rspec-mocks (3.12.5)
|
45
48
|
diff-lcs (>= 1.2.0, < 2.0)
|
46
49
|
rspec-support (~> 3.12.0)
|
47
50
|
rspec-support (3.12.0)
|
48
|
-
rubocop (1.
|
51
|
+
rubocop (1.52.1)
|
49
52
|
json (~> 2.3)
|
50
53
|
parallel (~> 1.10)
|
51
|
-
parser (>= 3.2.
|
54
|
+
parser (>= 3.2.2.3)
|
52
55
|
rainbow (>= 2.2.2, < 4.0)
|
53
56
|
regexp_parser (>= 1.8, < 3.0)
|
54
57
|
rexml (>= 3.2.5, < 4.0)
|
55
|
-
rubocop-ast (>= 1.
|
58
|
+
rubocop-ast (>= 1.28.0, < 2.0)
|
56
59
|
ruby-progressbar (~> 1.7)
|
57
60
|
unicode-display_width (>= 2.4.0, < 3.0)
|
58
|
-
rubocop-ast (1.
|
61
|
+
rubocop-ast (1.29.0)
|
59
62
|
parser (>= 3.2.1.0)
|
60
|
-
rubocop-performance (1.
|
63
|
+
rubocop-performance (1.18.0)
|
61
64
|
rubocop (>= 1.7.0, < 2.0)
|
62
65
|
rubocop-ast (>= 0.4.0)
|
63
66
|
ruby-progressbar (1.13.0)
|
@@ -71,13 +74,20 @@ GEM
|
|
71
74
|
terminal-table
|
72
75
|
simplecov-html (0.12.3)
|
73
76
|
simplecov_json_formatter (0.1.4)
|
74
|
-
standard (1.
|
77
|
+
standard (1.29.0)
|
75
78
|
language_server-protocol (~> 3.17.0.2)
|
76
|
-
|
77
|
-
rubocop
|
79
|
+
lint_roller (~> 1.0)
|
80
|
+
rubocop (~> 1.52.0)
|
81
|
+
standard-custom (~> 1.0.0)
|
82
|
+
standard-performance (~> 1.1.0)
|
83
|
+
standard-custom (1.0.1)
|
84
|
+
lint_roller (~> 1.0)
|
85
|
+
standard-performance (1.1.0)
|
86
|
+
lint_roller (~> 1.0)
|
87
|
+
rubocop-performance (~> 1.18.0)
|
78
88
|
terminal-table (3.0.2)
|
79
89
|
unicode-display_width (>= 1.1.1, < 3)
|
80
|
-
thor (1.2.
|
90
|
+
thor (1.2.2)
|
81
91
|
tty-cursor (0.7.1)
|
82
92
|
tty-reader (0.9.0)
|
83
93
|
tty-cursor (~> 0.7)
|
data/README.md
CHANGED
@@ -159,7 +159,7 @@ Snibbet's implementation of Skylighting has limited but better-looking themes, a
|
|
159
159
|
### Usage
|
160
160
|
|
161
161
|
```
|
162
|
-
Snibbets v2.0.
|
162
|
+
Snibbets v2.0.34
|
163
163
|
|
164
164
|
Usage: snibbets [options] query
|
165
165
|
-a, --all If a file contains multiple snippets, output all of them (no menu)
|
data/bin/snibbets
CHANGED
@@ -26,6 +26,7 @@ module Snibbets
|
|
26
26
|
def run
|
27
27
|
options = Snibbets.options
|
28
28
|
arguments = Snibbets.arguments
|
29
|
+
Color.coloring = $stdout.isatty
|
29
30
|
|
30
31
|
optparse = OptionParser.new do |opts|
|
31
32
|
opts.banner = "Usage: #{File.basename(__FILE__)} [options] query"
|
@@ -94,14 +95,14 @@ module Snibbets
|
|
94
95
|
end
|
95
96
|
|
96
97
|
opts.on('-h', '--help', 'Display this screen') do
|
97
|
-
puts "Snibbets v#{VERSION}"
|
98
|
+
puts "{by}Snibbets {w}v#{VERSION}".x
|
98
99
|
puts
|
99
100
|
puts optparse
|
100
101
|
Process.exit 0
|
101
102
|
end
|
102
103
|
|
103
104
|
opts.on('-v', '--version', 'Display version information') do
|
104
|
-
puts "Snibbets v#{VERSION}"
|
105
|
+
puts "{by}Snibbets {w}v#{VERSION}".x
|
105
106
|
Process.exit 0
|
106
107
|
end
|
107
108
|
|
@@ -116,7 +117,7 @@ module Snibbets
|
|
116
117
|
if arguments[:save_config]
|
117
118
|
config = Snibbets::Config.new
|
118
119
|
config.write_config
|
119
|
-
puts "Configuration saved to #{config.config_file}"
|
120
|
+
puts "{bg}Configuration saved to {w}#{config.config_file}".x
|
120
121
|
end
|
121
122
|
|
122
123
|
if arguments[:edit_config]
|
@@ -127,8 +128,8 @@ module Snibbets
|
|
127
128
|
end
|
128
129
|
|
129
130
|
unless File.directory?(options[:source])
|
130
|
-
puts 'The Snippets folder doesn\'t exist, please configure it.'
|
131
|
-
puts 'Run `snibbets --configure` to open the config file for editing.'
|
131
|
+
puts '{br}The Snippets folder doesn\'t exist, please configure it.'.x
|
132
|
+
puts '{bg}Run `{bw}snibbets --configure{bg}` to open the config file for editing.'.x
|
132
133
|
Process.exit 1
|
133
134
|
end
|
134
135
|
|
@@ -155,7 +156,7 @@ module Snibbets
|
|
155
156
|
if arguments[:save_config]
|
156
157
|
Process.exit 0
|
157
158
|
else
|
158
|
-
puts 'No search query'
|
159
|
+
puts '{br}No search query'.x
|
159
160
|
puts optparse
|
160
161
|
Process.exit 1
|
161
162
|
end
|
@@ -0,0 +1,329 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Cribbed from <https://github.com/flori/term-ansicolor>
|
4
|
+
module Snibbets
|
5
|
+
class ::String
|
6
|
+
def x
|
7
|
+
Color.template(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
def uncolor
|
11
|
+
Color.uncolor(self)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Terminal output color functions.
|
16
|
+
module Color
|
17
|
+
ESCAPE_REGEX = /(?<=\[)(?:(?:(?:[349]|10)[0-9]|[0-9])?;?)+(?=m)/.freeze
|
18
|
+
# All available color names. Available as methods and string extensions.
|
19
|
+
#
|
20
|
+
# @example Use a color as a method. Color reset will be added to end of string.
|
21
|
+
# Color.yellow('This text is yellow') => "\e[33mThis text is yellow\e[0m"
|
22
|
+
#
|
23
|
+
# @example Use a color as a string extension. Color reset added automatically.
|
24
|
+
# 'This text is green'.green => "\e[1;32mThis text is green\e[0m"
|
25
|
+
#
|
26
|
+
# @example Send a text string as a color
|
27
|
+
# Color.send('red') => "\e[31m"
|
28
|
+
ATTRIBUTES = [
|
29
|
+
[:clear, 0], # String#clear is already used to empty string in Ruby 1.9
|
30
|
+
[:reset, 0], # synonym for :clear
|
31
|
+
[:bold, 1],
|
32
|
+
[:dark, 2],
|
33
|
+
[:italic, 3], # not widely implemented
|
34
|
+
[:underline, 4],
|
35
|
+
[:underscore, 4], # synonym for :underline
|
36
|
+
[:blink, 5],
|
37
|
+
[:rapid_blink, 6], # not widely implemented
|
38
|
+
[:negative, 7], # no reverse because of String#reverse
|
39
|
+
[:concealed, 8],
|
40
|
+
[:strikethrough, 9], # not widely implemented
|
41
|
+
[:strike, 9], # not widely implemented
|
42
|
+
[:black, 30],
|
43
|
+
[:red, 31],
|
44
|
+
[:green, 32],
|
45
|
+
[:yellow, 33],
|
46
|
+
[:blue, 34],
|
47
|
+
[:magenta, 35],
|
48
|
+
[:purple, 35],
|
49
|
+
[:cyan, 36],
|
50
|
+
[:white, 37],
|
51
|
+
[:bgblack, 40],
|
52
|
+
[:bgred, 41],
|
53
|
+
[:bggreen, 42],
|
54
|
+
[:bgyellow, 43],
|
55
|
+
[:bgblue, 44],
|
56
|
+
[:bgmagenta, 45],
|
57
|
+
[:bgpurple, 45],
|
58
|
+
[:bgcyan, 46],
|
59
|
+
[:bgwhite, 47],
|
60
|
+
[:boldblack, 90],
|
61
|
+
[:boldred, 91],
|
62
|
+
[:boldgreen, 92],
|
63
|
+
[:boldyellow, 93],
|
64
|
+
[:boldblue, 94],
|
65
|
+
[:boldmagenta, 95],
|
66
|
+
[:boldpurple, 95],
|
67
|
+
[:boldcyan, 96],
|
68
|
+
[:boldwhite, 97],
|
69
|
+
[:boldbgblack, 100],
|
70
|
+
[:boldbgred, 101],
|
71
|
+
[:boldbggreen, 102],
|
72
|
+
[:boldbgyellow, 103],
|
73
|
+
[:boldbgblue, 104],
|
74
|
+
[:boldbgmagenta, 105],
|
75
|
+
[:boldbgpurple, 105],
|
76
|
+
[:boldbgcyan, 106],
|
77
|
+
[:boldbgwhite, 107],
|
78
|
+
[:softpurple, '0;35;40'],
|
79
|
+
[:hotpants, '7;34;40'],
|
80
|
+
[:knightrider, '7;30;40'],
|
81
|
+
[:flamingo, '7;31;47'],
|
82
|
+
[:yeller, '1;37;43'],
|
83
|
+
[:whiteboard, '1;30;47'],
|
84
|
+
[:chalkboard, '1;37;40'],
|
85
|
+
[:led, '0;32;40'],
|
86
|
+
[:redacted, '0;30;40'],
|
87
|
+
[:alert, '1;31;43'],
|
88
|
+
[:error, '1;37;41'],
|
89
|
+
[:default, '0;39']
|
90
|
+
].map(&:freeze).freeze
|
91
|
+
|
92
|
+
ATTRIBUTE_NAMES = ATTRIBUTES.transpose.first
|
93
|
+
|
94
|
+
# Returns true if Snibbets::Color supports the +feature+.
|
95
|
+
#
|
96
|
+
# The feature :clear, that is mixing the clear color attribute into String,
|
97
|
+
# is only supported on ruby implementations, that do *not* already
|
98
|
+
# implement the String#clear method. It's better to use the reset color
|
99
|
+
# attribute instead.
|
100
|
+
def support?(feature)
|
101
|
+
case feature
|
102
|
+
when :clear
|
103
|
+
!String.instance_methods(false).map(&:to_sym).include?(:clear)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Template coloring
|
108
|
+
class ::String
|
109
|
+
##
|
110
|
+
## Extract the longest valid %color name from a string.
|
111
|
+
##
|
112
|
+
## Allows %colors to bleed into other text and still
|
113
|
+
## be recognized, e.g. %greensomething still finds
|
114
|
+
## %green.
|
115
|
+
##
|
116
|
+
## @return [String] a valid color name
|
117
|
+
##
|
118
|
+
def validate_color
|
119
|
+
valid_color = nil
|
120
|
+
compiled = ''
|
121
|
+
normalize_color.split('').each do |char|
|
122
|
+
compiled += char
|
123
|
+
valid_color = compiled if Color.attributes.include?(compiled.to_sym) || compiled =~ /^([fb]g?)?#([a-f0-9]{6})$/i
|
124
|
+
end
|
125
|
+
|
126
|
+
valid_color
|
127
|
+
end
|
128
|
+
|
129
|
+
##
|
130
|
+
## Normalize a color name, removing underscores,
|
131
|
+
## replacing "bright" with "bold", and converting
|
132
|
+
## bgbold to boldbg
|
133
|
+
##
|
134
|
+
## @return [String] Normalized color name
|
135
|
+
##
|
136
|
+
def normalize_color
|
137
|
+
gsub(/_/, '').sub(/bright/i, 'bold').sub(/bgbold/, 'boldbg')
|
138
|
+
end
|
139
|
+
|
140
|
+
# Get the calculated ANSI color at the end of the
|
141
|
+
# string
|
142
|
+
#
|
143
|
+
# @return ANSI escape sequence to match color
|
144
|
+
#
|
145
|
+
def last_color_code
|
146
|
+
m = scan(ESCAPE_REGEX)
|
147
|
+
|
148
|
+
em = ['0']
|
149
|
+
fg = nil
|
150
|
+
bg = nil
|
151
|
+
rgbf = nil
|
152
|
+
rgbb = nil
|
153
|
+
|
154
|
+
m.each do |c|
|
155
|
+
case c
|
156
|
+
when '0'
|
157
|
+
em = ['0']
|
158
|
+
fg, bg, rgbf, rgbb = nil
|
159
|
+
when /^[34]8/
|
160
|
+
case c
|
161
|
+
when /^3/
|
162
|
+
fg = nil
|
163
|
+
rgbf = c
|
164
|
+
when /^4/
|
165
|
+
bg = nil
|
166
|
+
rgbb = c
|
167
|
+
end
|
168
|
+
else
|
169
|
+
c.split(/;/).each do |i|
|
170
|
+
x = i.to_i
|
171
|
+
if x <= 9
|
172
|
+
em << x
|
173
|
+
elsif x >= 30 && x <= 39
|
174
|
+
rgbf = nil
|
175
|
+
fg = x
|
176
|
+
elsif x >= 40 && x <= 49
|
177
|
+
rgbb = nil
|
178
|
+
bg = x
|
179
|
+
elsif x >= 90 && x <= 97
|
180
|
+
rgbf = nil
|
181
|
+
fg = x
|
182
|
+
elsif x >= 100 && x <= 107
|
183
|
+
rgbb = nil
|
184
|
+
bg = x
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
escape = "\e[#{em.join(';')}m"
|
191
|
+
escape += "\e[#{rgbb}m" if rgbb
|
192
|
+
escape += "\e[#{rgbf}m" if rgbf
|
193
|
+
escape + "\e[#{[fg, bg].delete_if(&:nil?).join(';')}m"
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
class << self
|
198
|
+
# Returns true if the coloring function of this module
|
199
|
+
# is switched on, false otherwise.
|
200
|
+
def coloring?
|
201
|
+
@coloring
|
202
|
+
end
|
203
|
+
|
204
|
+
attr_writer :coloring
|
205
|
+
|
206
|
+
##
|
207
|
+
## Enables colored output
|
208
|
+
##
|
209
|
+
## @example Turn color on or off based on TTY
|
210
|
+
## Snibbets::Color.coloring = STDOUT.isatty
|
211
|
+
def coloring
|
212
|
+
@coloring ||= true
|
213
|
+
end
|
214
|
+
|
215
|
+
##
|
216
|
+
## Convert a template string to a colored string.
|
217
|
+
## Colors are specified with single letters inside
|
218
|
+
## curly braces. Uppercase changes background color.
|
219
|
+
##
|
220
|
+
## w: white, k: black, g: green, l: blue, y: yellow, c: cyan,
|
221
|
+
## m: magenta, r: red, b: bold, u: underline, i: italic,
|
222
|
+
## x: reset (remove background, color, emphasis)
|
223
|
+
##
|
224
|
+
## @example Convert a templated string
|
225
|
+
## Color.template('{Rwb}Warning:{x} {w}you look a little {g}ill{x}')
|
226
|
+
##
|
227
|
+
## @param input [String, Array] The template
|
228
|
+
## string. If this is an array, the
|
229
|
+
## elements will be joined with a
|
230
|
+
## space.
|
231
|
+
##
|
232
|
+
## @return [String] Colorized string
|
233
|
+
##
|
234
|
+
def template(input)
|
235
|
+
input = input.join(' ') if input.is_a? Array
|
236
|
+
fmt = input.gsub(/%/, '%%')
|
237
|
+
fmt = fmt.gsub(/(?<!\\u|\$)\{(\w+)\}/i) do
|
238
|
+
Regexp.last_match(1).split('').map { |c| "%<#{c}>s" }.join('')
|
239
|
+
end
|
240
|
+
|
241
|
+
colors = { w: white, k: black, g: green, l: blue,
|
242
|
+
y: yellow, c: cyan, m: magenta, r: red,
|
243
|
+
W: bgwhite, K: bgblack, G: bggreen, L: bgblue,
|
244
|
+
Y: bgyellow, C: bgcyan, M: bgmagenta, R: bgred,
|
245
|
+
d: dark, b: bold, u: underline, i: italic, x: reset }
|
246
|
+
|
247
|
+
fmt.empty? ? input : format(fmt, colors)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
ATTRIBUTES.each do |c, v|
|
252
|
+
new_method = <<-EOSCRIPT
|
253
|
+
def #{c}(string = nil)
|
254
|
+
result = ''
|
255
|
+
result << "\e[#{v}m" if Snibbets::Color.coloring?
|
256
|
+
if block_given?
|
257
|
+
result << yield
|
258
|
+
elsif string.respond_to?(:to_str)
|
259
|
+
result << string.to_str
|
260
|
+
elsif respond_to?(:to_str)
|
261
|
+
result << to_str
|
262
|
+
else
|
263
|
+
return result #only switch on
|
264
|
+
end
|
265
|
+
result << "\e[0m" if Snibbets::Color.coloring?
|
266
|
+
result
|
267
|
+
end
|
268
|
+
EOSCRIPT
|
269
|
+
|
270
|
+
module_eval(new_method)
|
271
|
+
|
272
|
+
next unless c =~ /bold/
|
273
|
+
|
274
|
+
# Accept brightwhite in addition to boldwhite
|
275
|
+
new_method = <<-EOSCRIPT
|
276
|
+
def #{c.to_s.sub(/bold/, 'bright')}(string = nil)
|
277
|
+
result = ''
|
278
|
+
result << "\e[#{v}m" if Snibbets::Color.coloring?
|
279
|
+
if block_given?
|
280
|
+
result << yield
|
281
|
+
elsif string.respond_to?(:to_str)
|
282
|
+
result << string.to_str
|
283
|
+
elsif respond_to?(:to_str)
|
284
|
+
result << to_str
|
285
|
+
else
|
286
|
+
return result #only switch on
|
287
|
+
end
|
288
|
+
result << "\e[0m" if Snibbets::Color.coloring?
|
289
|
+
result
|
290
|
+
end
|
291
|
+
EOSCRIPT
|
292
|
+
|
293
|
+
module_eval(new_method)
|
294
|
+
end
|
295
|
+
|
296
|
+
def rgb(hex)
|
297
|
+
is_bg = hex.match(/^bg?#/) ? true : false
|
298
|
+
hex_string = hex.sub(/^([fb]g?)?#/, '')
|
299
|
+
|
300
|
+
parts = hex_string.match(/(?<r>..)(?<g>..)(?<b>..)/)
|
301
|
+
t = []
|
302
|
+
%w[r g b].each do |e|
|
303
|
+
t << parts[e].hex
|
304
|
+
end
|
305
|
+
color =
|
306
|
+
"\e[#{is_bg ? '48' : '38'};2;#{t.join(';')}m"
|
307
|
+
end
|
308
|
+
|
309
|
+
# Returns an uncolored version of the string, that is all
|
310
|
+
# ANSI-sequences are stripped from the string.
|
311
|
+
def uncolor(string = nil) # :yields:
|
312
|
+
if block_given?
|
313
|
+
yield.to_str.gsub(ESCAPE_REGEX, '')
|
314
|
+
elsif string.respond_to?(:to_str)
|
315
|
+
string.to_str.gsub(ESCAPE_REGEX, '')
|
316
|
+
elsif respond_to?(:to_str)
|
317
|
+
to_str.gsub(ESCAPE_REGEX, '')
|
318
|
+
else
|
319
|
+
''
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
# Returns an array of all Snibbets::Color attributes as symbols.
|
324
|
+
def attributes
|
325
|
+
ATTRIBUTE_NAMES
|
326
|
+
end
|
327
|
+
extend self
|
328
|
+
end
|
329
|
+
end
|
data/lib/snibbets/highlight.rb
CHANGED
@@ -2,6 +2,7 @@ module Snibbets
|
|
2
2
|
module Highlight
|
3
3
|
class << self
|
4
4
|
def run_command_with_input(*cmd, input: nil, fallback: nil)
|
5
|
+
|
5
6
|
stdout, _stderr, status = Open3.capture3(*cmd, stdin_data: input)
|
6
7
|
if status.success?
|
7
8
|
stdout
|
@@ -35,10 +36,9 @@ module Snibbets
|
|
35
36
|
else
|
36
37
|
''
|
37
38
|
end
|
38
|
-
return code if syntax.nil? || syntax.empty?
|
39
|
+
return code if syntax.nil? || syntax.empty? || !Lexers.skylight_lexer?(syntax)
|
39
40
|
|
40
41
|
run_command_with_input("#{executable} #{theme}--syntax #{syntax}", input: code)
|
41
|
-
# `echo #{Shellwords.escape(code)} | #{executable} #{theme}--syntax #{syntax}`
|
42
42
|
end
|
43
43
|
|
44
44
|
def highlight_fences(code, filename, syntax)
|
@@ -46,7 +46,8 @@ module Snibbets
|
|
46
46
|
|
47
47
|
content.fences.each do |f|
|
48
48
|
rx = Regexp.new(Regexp.escape(f[:code]))
|
49
|
-
|
49
|
+
syn = Lexers.normalize_lexer(f[:lang] || syntax)
|
50
|
+
highlighted = highlight(f[:code].gsub(/\\k</, '\k\<'), filename, syn).strip
|
50
51
|
code.sub!(/#{rx}/, highlighted)
|
51
52
|
end
|
52
53
|
|
@@ -62,6 +63,7 @@ module Snibbets
|
|
62
63
|
|
63
64
|
theme ||= Snibbets.options[:highlight_theme] || 'monokai'
|
64
65
|
syntax ||= Lexers.syntax_from_extension(filename)
|
66
|
+
syntax = Lexers.normalize_lexer(syntax)
|
65
67
|
|
66
68
|
return code if ['text'].include?(syntax)
|
67
69
|
|
@@ -69,7 +71,11 @@ module Snibbets
|
|
69
71
|
pygments = TTY::Which.which('pygmentize')
|
70
72
|
|
71
73
|
if Snibbets.options[:highlighter] =~ /^s/ && !skylight.nil?
|
72
|
-
|
74
|
+
if !Lexers.skylight_lexer?(syntax) && !pygments.nil?
|
75
|
+
return highlight_pygments(pygments, code, syntax, 'monokai')
|
76
|
+
else
|
77
|
+
return highlight_skylight(skylight, code, syntax, theme)
|
78
|
+
end
|
73
79
|
elsif Snibbets.options[:highlighter] =~ /^p/ && !pygments.nil?
|
74
80
|
return highlight_pygments(pygments, code, syntax, theme)
|
75
81
|
elsif !skylight.nil?
|
data/lib/snibbets/lexers.rb
CHANGED
@@ -27,6 +27,15 @@ module Snibbets
|
|
27
27
|
lex
|
28
28
|
end
|
29
29
|
|
30
|
+
def skylight_lexer?(lexer)
|
31
|
+
SKYLIGHT_LEXERS_DB.split(/\n/).include?(lexer)
|
32
|
+
end
|
33
|
+
|
34
|
+
def normalize_lexer(lexer)
|
35
|
+
matches = LEXER_NORMALIZERS.select { |k, v| v.include? lexer }
|
36
|
+
matches.count.positive? ? matches.keys[0] : lexer
|
37
|
+
end
|
38
|
+
|
30
39
|
def ext_to_lang(ext)
|
31
40
|
return nil if ext.nil?
|
32
41
|
|
data/lib/snibbets/lexers_db.rb
CHANGED
@@ -233,7 +233,7 @@ nsis, nsi, nsh [nsi, nsh]
|
|
233
233
|
nusmv [smv]
|
234
234
|
objdump [objdump]
|
235
235
|
objective-c++, objectivec++, obj-c++, objc++ [mm, hh]
|
236
|
-
objective-c,
|
236
|
+
objectivec, objective-c, obj-c, objc [m, h]
|
237
237
|
objective-j, objectivej, obj-j, objj [j]
|
238
238
|
ocaml [ml, mli, mll, mly]
|
239
239
|
odin [odin]
|
@@ -377,3 +377,164 @@ bro [bro]
|
|
377
377
|
zephir [zep]
|
378
378
|
zig [zig]
|
379
379
|
EOLEXERS
|
380
|
+
|
381
|
+
SKYLIGHT_LEXERS_DB=<<EOSKYLEX
|
382
|
+
abc
|
383
|
+
asn1
|
384
|
+
asp
|
385
|
+
ats
|
386
|
+
awk
|
387
|
+
actionscript
|
388
|
+
ada
|
389
|
+
agda
|
390
|
+
alert
|
391
|
+
apache
|
392
|
+
bash
|
393
|
+
bibtex
|
394
|
+
boo
|
395
|
+
c
|
396
|
+
cs
|
397
|
+
cpp
|
398
|
+
cmake
|
399
|
+
css
|
400
|
+
changelog
|
401
|
+
clojure
|
402
|
+
coffee
|
403
|
+
coldfusion
|
404
|
+
comments
|
405
|
+
commonlisp
|
406
|
+
curry
|
407
|
+
d
|
408
|
+
dtd
|
409
|
+
default
|
410
|
+
diff
|
411
|
+
djangotemplate
|
412
|
+
dockerfile
|
413
|
+
doxygen
|
414
|
+
doxygenlua
|
415
|
+
eiffel
|
416
|
+
elixir
|
417
|
+
elm
|
418
|
+
email
|
419
|
+
erlang
|
420
|
+
fsharp
|
421
|
+
fortranfixed
|
422
|
+
fortranfree
|
423
|
+
gcc
|
424
|
+
glsl
|
425
|
+
gnuassembler
|
426
|
+
m4
|
427
|
+
go
|
428
|
+
graphql
|
429
|
+
groovy
|
430
|
+
html
|
431
|
+
hamlet
|
432
|
+
haskell
|
433
|
+
haxe
|
434
|
+
ini
|
435
|
+
isocpp
|
436
|
+
idris
|
437
|
+
fasm
|
438
|
+
nasm
|
439
|
+
j
|
440
|
+
json
|
441
|
+
jsp
|
442
|
+
java
|
443
|
+
javascript
|
444
|
+
javascriptreact
|
445
|
+
javadoc
|
446
|
+
julia
|
447
|
+
kotlin
|
448
|
+
llvm
|
449
|
+
latex
|
450
|
+
lex
|
451
|
+
lilypond
|
452
|
+
literatecurry
|
453
|
+
literatehaskell
|
454
|
+
lua
|
455
|
+
mips
|
456
|
+
makefile
|
457
|
+
markdown
|
458
|
+
mathematica
|
459
|
+
matlab
|
460
|
+
maxima
|
461
|
+
mediawiki
|
462
|
+
metafont
|
463
|
+
modelines
|
464
|
+
modula2
|
465
|
+
modula3
|
466
|
+
monobasic
|
467
|
+
mustache
|
468
|
+
nim
|
469
|
+
nix
|
470
|
+
ocaml
|
471
|
+
objectivec
|
472
|
+
objectivecpp
|
473
|
+
octave
|
474
|
+
opencl
|
475
|
+
orgmode
|
476
|
+
php
|
477
|
+
povray
|
478
|
+
pascal
|
479
|
+
perl
|
480
|
+
pike
|
481
|
+
postscript
|
482
|
+
powershell
|
483
|
+
prolog
|
484
|
+
protobuf
|
485
|
+
pure
|
486
|
+
purebasic
|
487
|
+
python
|
488
|
+
qml
|
489
|
+
r
|
490
|
+
relaxng
|
491
|
+
raku
|
492
|
+
relaxngcompact
|
493
|
+
roff
|
494
|
+
ruby
|
495
|
+
rhtml
|
496
|
+
rust
|
497
|
+
sass
|
498
|
+
scss
|
499
|
+
sgml
|
500
|
+
sml
|
501
|
+
spdxcomments
|
502
|
+
sql
|
503
|
+
sqlmysql
|
504
|
+
sqlpostgresql
|
505
|
+
scala
|
506
|
+
scheme
|
507
|
+
stan
|
508
|
+
stata
|
509
|
+
swift
|
510
|
+
systemverilog
|
511
|
+
toml
|
512
|
+
tcl
|
513
|
+
tcsh
|
514
|
+
texinfo
|
515
|
+
mandoc
|
516
|
+
typescript
|
517
|
+
vhdl
|
518
|
+
verilog
|
519
|
+
xml
|
520
|
+
xul
|
521
|
+
yaml
|
522
|
+
yacc
|
523
|
+
zsh
|
524
|
+
dot
|
525
|
+
noweb
|
526
|
+
rest
|
527
|
+
sci
|
528
|
+
sed
|
529
|
+
xorg
|
530
|
+
xslt
|
531
|
+
EOSKYLEX
|
532
|
+
|
533
|
+
LEXER_NORMALIZERS = {
|
534
|
+
'objectivec' => %w[objective-c obj-c],
|
535
|
+
'markdown' => %w[md mmd mkdn multimarkdown],
|
536
|
+
'bash' => %w[csh sh shell],
|
537
|
+
'javascript' => %w[js],
|
538
|
+
'javascriptreact' => %w[react],
|
539
|
+
'yaml' => %w[yml]
|
540
|
+
}.freeze
|
data/lib/snibbets/os.rb
CHANGED
@@ -12,12 +12,12 @@ module Snibbets
|
|
12
12
|
os = RbConfig::CONFIG['target_os']
|
13
13
|
case os
|
14
14
|
when /darwin.*/i
|
15
|
-
`echo #{Shellwords.escape(text)} | pbcopy`
|
15
|
+
`echo #{Shellwords.escape(text.uncolor)} | pbcopy`
|
16
16
|
else
|
17
17
|
if TTY::Which.exist?('xclip')
|
18
|
-
`echo #{Shellwords.escape(text)} | xclip -sel c`
|
18
|
+
`echo #{Shellwords.escape(text.uncolor)} | xclip -sel c`
|
19
19
|
elsif TTY::Which.exist('xsel')
|
20
|
-
`echo #{Shellwords.escape(text)} | xsel -ib`
|
20
|
+
`echo #{Shellwords.escape(text.uncolor)} | xsel -ib`
|
21
21
|
else
|
22
22
|
puts 'Copy not supported on this system, please install xclip or xsel.'
|
23
23
|
end
|
data/lib/snibbets/string.rb
CHANGED
@@ -84,7 +84,7 @@ module Snibbets
|
|
84
84
|
# if it's a fenced code block, just discard the fence and everything
|
85
85
|
# outside it
|
86
86
|
if block.fenced?
|
87
|
-
code_blocks = block.scan(/(`{3,})(\
|
87
|
+
code_blocks = block.scan(/(`{3,})(\S+)?\s*\n(.*?)\n\1/m)
|
88
88
|
code_blocks.map! { |b| b[2].strip }
|
89
89
|
return code_blocks.join("\n\n")
|
90
90
|
end
|
@@ -169,7 +169,7 @@ module Snibbets
|
|
169
169
|
|
170
170
|
next if lines.blocks.zero? && !notes
|
171
171
|
|
172
|
-
title = if lines.count > 1 && lines[0] !~ /<block\d+>/
|
172
|
+
title = if lines.count > 1 && lines[0] !~ /<block\d+>/
|
173
173
|
lines.shift.strip.sub(/[.:]$/, '')
|
174
174
|
else
|
175
175
|
'Default snippet'
|
@@ -208,18 +208,27 @@ module Snibbets
|
|
208
208
|
# becomes the title, code is gleaned from text between that and the
|
209
209
|
# next ATX header (or end)
|
210
210
|
sans_blocks, code_blocks = content.replace_blocks
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
211
|
+
parts = sans_blocks.gsub(/\n{2,}/, "\n\n").split(/^#+/)
|
212
|
+
|
213
|
+
unless Snibbets.options[:all_notes]
|
214
|
+
parts.map! do |part|
|
215
|
+
if part =~ /<block\d+>/
|
216
|
+
lines = part.split(/\n/)
|
217
|
+
title = lines.shift
|
218
|
+
out = [title.gsub(/(^#+|#+$)/, '').strip]
|
219
|
+
out.concat(lines.each_with_object([]) do |line, arr|
|
220
|
+
arr << line.gsub(/(^#+|#+$)/, '').strip if line =~ /^#/ || line =~ /<block\d+>/
|
221
|
+
end)
|
222
|
+
out.join("\n")
|
223
|
+
else
|
224
|
+
lines = part.split(/\n/)
|
225
|
+
title = lines.shift
|
226
|
+
out = title.nil? ? [] : [title.gsub(/(^#+|#+$)/, '').strip]
|
227
|
+
out.concat(lines)
|
228
|
+
out.join("\n")
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
223
232
|
|
224
233
|
restore_blocks(parts, code_blocks)
|
225
234
|
end
|
data/lib/snibbets/version.rb
CHANGED
data/lib/snibbets.rb
CHANGED
@@ -12,6 +12,7 @@ require 'tty-reader'
|
|
12
12
|
require 'tty-which'
|
13
13
|
require 'yaml'
|
14
14
|
require_relative 'snibbets/version'
|
15
|
+
require_relative 'snibbets/colors'
|
15
16
|
require_relative 'snibbets/config'
|
16
17
|
require_relative 'snibbets/which'
|
17
18
|
require_relative 'snibbets/string'
|
@@ -60,9 +61,7 @@ module Snibbets
|
|
60
61
|
ag = TTY::Which.which('ag')
|
61
62
|
ack = TTY::Which.which('ack')
|
62
63
|
grep = TTY::Which.which('grep')
|
63
|
-
if
|
64
|
-
nil
|
65
|
-
elsif !rg.empty?
|
64
|
+
if !rg.empty?
|
66
65
|
%(#{rg} -li --color=never --glob='*.#{ext}' '#{@query.rx}' "#{folder}")
|
67
66
|
elsif !ag.empty?
|
68
67
|
%(#{ag} -li --nocolor -G '.*.#{ext}' '#{@query.rx}' "#{folder}")
|
@@ -83,9 +82,14 @@ module Snibbets
|
|
83
82
|
end
|
84
83
|
end
|
85
84
|
|
86
|
-
if try == 2
|
87
|
-
|
88
|
-
|
85
|
+
if try == 2
|
86
|
+
if Snibbets.options[:name_only]
|
87
|
+
puts '{br}No name matches found'.x
|
88
|
+
Process.exit 1
|
89
|
+
elsif cmd.nil?
|
90
|
+
puts '{br}No search method available on this system. Please install ripgrep, silver surfer, ack, or grep.'.x
|
91
|
+
Process.exit 1
|
92
|
+
end
|
89
93
|
end
|
90
94
|
|
91
95
|
res = cmd.nil? ? '' : `#{cmd}`.strip
|
@@ -138,14 +142,14 @@ module Snibbets
|
|
138
142
|
editor = TTY::Which.which(editor)
|
139
143
|
system %(#{editor} "#{filepath}") if editor
|
140
144
|
else
|
141
|
-
puts "No editor configured, or editor is missing"
|
145
|
+
puts "{br}No editor configured, or editor is missing".x
|
142
146
|
Process.exit 1
|
143
147
|
end
|
144
148
|
elsif TTY::Which.exist?(editor)
|
145
149
|
editor = TTY::Which.which(editor)
|
146
150
|
system %(#{editor} "#{filepath}") if editor
|
147
151
|
else
|
148
|
-
puts "No editor configured, or editor is missing"
|
152
|
+
puts "{br}No editor configured, or editor is missing".x
|
149
153
|
Process.exit 1
|
150
154
|
end
|
151
155
|
end
|
@@ -163,12 +167,12 @@ module Snibbets
|
|
163
167
|
reader = TTY::Reader.new
|
164
168
|
|
165
169
|
# printf 'What does this snippet do? '
|
166
|
-
input = reader.read_line('What does this snippet do? ').strip
|
170
|
+
input = reader.read_line('{by}What does this snippet do{bw}? '.x).strip
|
167
171
|
# input = $stdin.gets.chomp
|
168
172
|
title = input unless input.empty?
|
169
173
|
|
170
174
|
# printf 'What language(s) does it use (separate with spaces, full names or file extensions will work)? '
|
171
|
-
input = reader.read_line('What language(s) does it use (separate with spaces, full names or file extensions
|
175
|
+
input = reader.read_line('{by}What language(s) does it use ({xw}separate with spaces, full names or file extensions{by}){bw}? '.x).strip
|
172
176
|
# input = $stdin.gets.chomp
|
173
177
|
langs = input.split(/ +/).map(&:strip) unless input.empty?
|
174
178
|
exts = langs.map { |lang| Lexers.lang_to_ext(lang) }.delete_if(&:nil?)
|
@@ -176,7 +180,7 @@ module Snibbets
|
|
176
180
|
|
177
181
|
exts = langs if exts.empty?
|
178
182
|
|
179
|
-
filename = "#{title}#{exts.map { |x| ".#{x}"}.join('')}.#{Snibbets.options[:extension]}"
|
183
|
+
filename = "#{title}#{exts.map { |x| ".#{x}" }.join('')}.#{Snibbets.options[:extension]}"
|
180
184
|
filepath = File.join(File.expand_path(Snibbets.options[:source]), filename)
|
181
185
|
File.open(filepath, 'w') do |f|
|
182
186
|
f.puts "tags: #{tags.join(', ')}
|
@@ -186,7 +190,7 @@ module Snibbets
|
|
186
190
|
```"
|
187
191
|
end
|
188
192
|
|
189
|
-
puts "New snippet written to #{filename}."
|
193
|
+
puts "{bg}New snippet written to {bw}#{filename}.".x
|
190
194
|
|
191
195
|
open_snippet_in_editor(filepath) if Snibbets.arguments[:edit_snippet]
|
192
196
|
open_snippet_in_nvultra(filepath) if Snibbets.arguments[:nvultra]
|
@@ -280,8 +284,8 @@ module Snibbets
|
|
280
284
|
snippets.each do |snip|
|
281
285
|
header = File.basename(filepath, '.md')
|
282
286
|
if $stdout.isatty
|
283
|
-
puts header
|
284
|
-
puts '-' * header.length
|
287
|
+
puts "{bw}#{header}{x}".x
|
288
|
+
puts "{dw}#{'-' * header.length}{x}".x
|
285
289
|
puts ''
|
286
290
|
end
|
287
291
|
code = snip['code']
|
@@ -311,8 +315,8 @@ module Snibbets
|
|
311
315
|
else
|
312
316
|
if $stdout.isatty
|
313
317
|
header = File.basename(filepath, '.md')
|
314
|
-
warn header
|
315
|
-
warn '=' * header.length
|
318
|
+
warn "{bw}#{header}{x}".x
|
319
|
+
warn "{dw}#{'=' * header.length}{x}".x
|
316
320
|
warn ''
|
317
321
|
end
|
318
322
|
print_all(snippets, filepath)
|
@@ -321,7 +325,7 @@ module Snibbets
|
|
321
325
|
print(answer.to_json, filepath)
|
322
326
|
else
|
323
327
|
if $stdout.isatty
|
324
|
-
header = "#{File.basename(filepath, '.md')}: #{answer['title']}"
|
328
|
+
header = "{bw}#{File.basename(filepath, '.md')}: {c}#{answer['title']}{x}".x
|
325
329
|
warn header
|
326
330
|
warn '-' * header.length
|
327
331
|
warn ''
|
@@ -340,7 +344,7 @@ module Snibbets
|
|
340
344
|
snippets.each do |snippet|
|
341
345
|
lang = snippet['language']
|
342
346
|
|
343
|
-
puts "### #{snippet['title']} ###"
|
347
|
+
puts "{dw}### {xbw}#{snippet['title']} {xdw}### {x}".x
|
344
348
|
puts ''
|
345
349
|
|
346
350
|
print(snippet['code'], filepath, lang)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snibbets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.34
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brett Terpstra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -257,6 +257,7 @@ files:
|
|
257
257
|
- bin/snibbets
|
258
258
|
- lib/snibbets.rb
|
259
259
|
- lib/snibbets/array.rb
|
260
|
+
- lib/snibbets/colors.rb
|
260
261
|
- lib/snibbets/config.rb
|
261
262
|
- lib/snibbets/hash.rb
|
262
263
|
- lib/snibbets/highlight.rb
|