code_snippet 0.2.7 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +7 -5
- data/README.md +4 -7
- data/bin/code_snippet +20 -0
- data/bin/snippet +11 -53
- data/code_snippet.gemspec +1 -0
- data/lib/code_snippet/cli.rb +71 -27
- data/lib/code_snippet/commands/list_snippets.rb +41 -0
- data/lib/code_snippet/commands/print_path.rb +20 -0
- data/lib/code_snippet/commands/print_version.rb +19 -0
- data/lib/code_snippet/commands/show_snippet.rb +57 -0
- data/lib/code_snippet/commands.rb +13 -0
- data/lib/code_snippet/manager.rb +5 -1
- data/lib/code_snippet/version.rb +1 -1
- data/spec/code_snippet/cli_spec.rb +203 -0
- data/spec/code_snippet/commands/list_snippets_spec.rb +67 -0
- data/spec/code_snippet/commands/print_path_spec.rb +22 -0
- data/spec/code_snippet/commands/print_version_spec.rb +21 -0
- data/spec/code_snippet/commands/show_snippet_spec.rb +202 -0
- data/spec/{snippet → code_snippet}/manager_spec.rb +25 -2
- data/spec/{snippet → code_snippet}/snip_spec.rb +0 -0
- metadata +37 -15
- data/bitbucket-pipelines.yml +0 -18
- data/lib/code_snippet/cli/commands.rb +0 -82
- data/lib/code_snippet/cli/presenters.rb +0 -42
- data/spec/snippet/cli/commands_spec.rb +0 -104
- data/spec/snippet/cli/presenters_spec.rb +0 -72
- data/spec/snippet/cli_spec.rb +0 -91
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe CodeSnippet::Commands::ListSnippets do
|
4
|
+
let(:options) { Hashie::Mash.new }
|
5
|
+
|
6
|
+
let(:snippet_list_table) do
|
7
|
+
<<~TABLE
|
8
|
+
NAME LANG
|
9
|
+
some-snippet .go
|
10
|
+
TABLE
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:manager) { double(CodeSnippet::Manager) }
|
14
|
+
let(:snippets) { [snippet] }
|
15
|
+
|
16
|
+
let(:snip_name) { 'some-snippet' }
|
17
|
+
let(:snip_ext) { '.go' }
|
18
|
+
|
19
|
+
let(:snippet) do
|
20
|
+
double(
|
21
|
+
CodeSnippet::Snip,
|
22
|
+
name: snip_name,
|
23
|
+
ext: snip_ext,
|
24
|
+
content: 'stuff'
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
subject { described_class.new(manager, options) }
|
29
|
+
|
30
|
+
before do
|
31
|
+
allow(manager)
|
32
|
+
.to receive(:snippets)
|
33
|
+
.and_return(snippets)
|
34
|
+
|
35
|
+
allow(snippet)
|
36
|
+
.to receive(:name)
|
37
|
+
.and_return(snip_name)
|
38
|
+
|
39
|
+
allow(snippet)
|
40
|
+
.to receive(:ext)
|
41
|
+
.and_return(snip_ext)
|
42
|
+
|
43
|
+
allow(TTY::Table).to receive(:new).and_call_original
|
44
|
+
allow(subject).to receive(:puts)
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#run' do
|
48
|
+
before { subject.run }
|
49
|
+
|
50
|
+
it 'retrieves snippets from manager' do
|
51
|
+
expect(manager).to have_received(:snippets)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'renders table' do
|
55
|
+
expect(TTY::Table).to have_received(:new).with(
|
56
|
+
header: CodeSnippet::Commands::ListSnippets::RESULT_HEADER,
|
57
|
+
rows: [['some-snippet', '.go']]
|
58
|
+
)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'prints table to STDOUT' do
|
62
|
+
expect(subject)
|
63
|
+
.to have_received(:puts)
|
64
|
+
.with(snippet_list_table.strip)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe CodeSnippet::Commands::PrintPath do
|
4
|
+
let(:options) { Hashie::Mash.new }
|
5
|
+
let(:snippet_dir) { '/path/to/snippets' }
|
6
|
+
|
7
|
+
subject { described_class.new(snippet_dir, options) }
|
8
|
+
|
9
|
+
before do
|
10
|
+
allow(subject).to receive(:puts)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#run' do
|
14
|
+
before { subject.run }
|
15
|
+
|
16
|
+
it 'prints given template path to STDOUT' do
|
17
|
+
expect(subject)
|
18
|
+
.to have_received(:puts)
|
19
|
+
.with(snippet_dir)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe CodeSnippet::Commands::PrintVersion do
|
4
|
+
let(:options) { Hashie::Mash.new }
|
5
|
+
|
6
|
+
subject { described_class.new(options) }
|
7
|
+
|
8
|
+
before do
|
9
|
+
allow(subject).to receive(:puts)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#run' do
|
13
|
+
before { subject.run }
|
14
|
+
|
15
|
+
it 'prints current version to STDOUT' do
|
16
|
+
expect(subject)
|
17
|
+
.to have_received(:puts)
|
18
|
+
.with(CodeSnippet::VERSION)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,202 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe CodeSnippet::Commands::ShowSnippet do
|
4
|
+
let(:snip_name) { 'some-snippet' }
|
5
|
+
let(:manager) { double(CodeSnippet::Manager) }
|
6
|
+
let(:options) do
|
7
|
+
Hashie::Mash.new(
|
8
|
+
name: snip_name
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:prompt) { double(TTY::Prompt) }
|
13
|
+
|
14
|
+
subject { described_class.new(manager, options) }
|
15
|
+
|
16
|
+
before do
|
17
|
+
subject.instance_variable_set(:@prompt, prompt)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#run' do
|
21
|
+
let(:snip_path) { 'path/to/snippet' }
|
22
|
+
let(:snip) do
|
23
|
+
double(
|
24
|
+
CodeSnippet::Snip,
|
25
|
+
name: snip_name,
|
26
|
+
path: snip_path
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
let(:found_snips) { [snip, snip] }
|
31
|
+
let(:snip_content) { 'snippet content' }
|
32
|
+
let(:snip_names) { [snip_name, snip_name] }
|
33
|
+
|
34
|
+
before do
|
35
|
+
allow(prompt).to receive(:select)
|
36
|
+
.and_return(snip_name)
|
37
|
+
|
38
|
+
allow(manager)
|
39
|
+
.to receive(:snippet_names)
|
40
|
+
.and_return(snip_names)
|
41
|
+
|
42
|
+
allow(subject)
|
43
|
+
.to receive(:find_snippets)
|
44
|
+
.and_return(found_snips)
|
45
|
+
|
46
|
+
allow(subject)
|
47
|
+
.to receive(:pick_snippet)
|
48
|
+
.and_return(snip)
|
49
|
+
|
50
|
+
allow(snip)
|
51
|
+
.to receive(:content)
|
52
|
+
.and_return(snip_content)
|
53
|
+
|
54
|
+
allow(Clipboard).to receive(:copy)
|
55
|
+
allow(subject).to receive(:puts)
|
56
|
+
|
57
|
+
subject.run
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'when name option is provided' do
|
61
|
+
it 'finds snippets' do
|
62
|
+
expect(subject)
|
63
|
+
.to have_received(:find_snippets)
|
64
|
+
.with(snip_name)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'prompts user to choose snippet' do
|
68
|
+
expect(subject)
|
69
|
+
.to have_received(:pick_snippet)
|
70
|
+
.with(found_snips)
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'prints snippet content' do
|
74
|
+
expect(subject)
|
75
|
+
.to have_received(:puts)
|
76
|
+
.with(snip_content)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'when name option is not provided' do
|
81
|
+
let(:options) { Hashie::Mash.new }
|
82
|
+
|
83
|
+
it 'asks user to choose snippet' do
|
84
|
+
expect(prompt)
|
85
|
+
.to have_received(:select)
|
86
|
+
.with(
|
87
|
+
'Please choose snippet',
|
88
|
+
snip_names
|
89
|
+
)
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'finds snippets' do
|
93
|
+
expect(subject)
|
94
|
+
.to have_received(:find_snippets)
|
95
|
+
.with(snip_name)
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'prompts user to choose snippet' do
|
99
|
+
expect(subject)
|
100
|
+
.to have_received(:pick_snippet)
|
101
|
+
.with(found_snips)
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'prints snippet content' do
|
105
|
+
expect(subject)
|
106
|
+
.to have_received(:puts)
|
107
|
+
.with(snip_content)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context 'when copy flag is provided' do
|
112
|
+
let(:options) do
|
113
|
+
Hashie::Mash.new(
|
114
|
+
copy: true
|
115
|
+
)
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'copies snippet content to clipboard' do
|
119
|
+
expect(Clipboard)
|
120
|
+
.to have_received(:copy)
|
121
|
+
.with(snip_content)
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'prints copied message' do
|
125
|
+
expect(subject)
|
126
|
+
.to have_received(:puts)
|
127
|
+
.with("COPIED: #{snip_path}")
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe '#find_snippets' do
|
133
|
+
let(:snip) { double(CodeSnippet::Snip, name: snip_name) }
|
134
|
+
let(:found_snips) { [snip, snip] }
|
135
|
+
|
136
|
+
before do
|
137
|
+
allow(manager).to receive(:find).and_return(found_snips)
|
138
|
+
end
|
139
|
+
|
140
|
+
context 'when snippet exists' do
|
141
|
+
before { @result = subject.find_snippets(snip_name) }
|
142
|
+
|
143
|
+
it 'returns snippets that match the search term' do
|
144
|
+
expect(@result).to eq found_snips
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context 'when snippet does not exist' do
|
149
|
+
let(:found_snips) { [] }
|
150
|
+
|
151
|
+
it 'raises unable to find snippet runtime error' do
|
152
|
+
expect do
|
153
|
+
subject.find_snippets(snip_name)
|
154
|
+
end.to raise_error(
|
155
|
+
RuntimeError,
|
156
|
+
"unable to find #{snip_name}"
|
157
|
+
)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe '#pick_snippet' do
|
163
|
+
let(:snip) { double(CodeSnippet::Snip, name: snip_name) }
|
164
|
+
let(:found_snips) { [snip, snip] }
|
165
|
+
|
166
|
+
before do
|
167
|
+
allow(prompt).to receive(:select).and_return(snip_name)
|
168
|
+
allow(manager).to receive(:find).and_return(found_snips)
|
169
|
+
|
170
|
+
@result = subject.pick_snippet(found_snips)
|
171
|
+
end
|
172
|
+
|
173
|
+
context 'when there are multiple snippets' do
|
174
|
+
it 'prompts user to select a snippet from list' do
|
175
|
+
expect(prompt)
|
176
|
+
.to have_received(:select)
|
177
|
+
.with(
|
178
|
+
'Multiple snippets found, which one would you like to view?',
|
179
|
+
[snip_name, snip_name]
|
180
|
+
)
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'finds snippet' do
|
184
|
+
expect(manager).to have_received(:find).with(snip_name)
|
185
|
+
end
|
186
|
+
|
187
|
+
it 'returns snippet' do
|
188
|
+
expect(@result).to eq snip
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
context 'when there is one snippet' do
|
193
|
+
it 'finds snippet' do
|
194
|
+
expect(manager).to have_received(:find).with(snip_name)
|
195
|
+
end
|
196
|
+
|
197
|
+
it 'returns snippet' do
|
198
|
+
expect(@result).to eq snip
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
@@ -7,7 +7,7 @@ RSpec.describe CodeSnippet::Manager do
|
|
7
7
|
described_class.new(snippet_dir)
|
8
8
|
end
|
9
9
|
|
10
|
-
describe '#
|
10
|
+
describe '#load' do
|
11
11
|
let(:directory) { File.join(snippet_dir, 'path/to/dir') }
|
12
12
|
let(:file) { File.join(snippet_dir, 'path/to/dir/file') }
|
13
13
|
|
@@ -32,7 +32,7 @@ RSpec.describe CodeSnippet::Manager do
|
|
32
32
|
allow(CodeSnippet::Snip).to receive(:new_from_file)
|
33
33
|
.and_return(file_snip)
|
34
34
|
|
35
|
-
subject.
|
35
|
+
subject.load
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'creates snips from files' do
|
@@ -163,4 +163,27 @@ RSpec.describe CodeSnippet::Manager do
|
|
163
163
|
expect(@result[1]).to be another_target_snip
|
164
164
|
end
|
165
165
|
end
|
166
|
+
|
167
|
+
describe '#snippet_names' do
|
168
|
+
let(:snip) do
|
169
|
+
CodeSnippet::Snip.new('path/to/snip.rb', 'snip.go', '.go')
|
170
|
+
end
|
171
|
+
|
172
|
+
let(:snips) do
|
173
|
+
[
|
174
|
+
snip,
|
175
|
+
snip,
|
176
|
+
snip
|
177
|
+
]
|
178
|
+
end
|
179
|
+
|
180
|
+
before do
|
181
|
+
subject.instance_variable_set(:@snippets, snips)
|
182
|
+
@result = subject.snippet_names
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'returns array of snippet names' do
|
186
|
+
expect(@result).to eq %w[snip snip snip]
|
187
|
+
end
|
188
|
+
end
|
166
189
|
end
|
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code_snippet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Bigger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: clipboard
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '1.4'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: thor
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: tty-prompt
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -250,6 +264,7 @@ description:
|
|
250
264
|
email:
|
251
265
|
- andrew.bigger@gmail.com
|
252
266
|
executables:
|
267
|
+
- code_snippet
|
253
268
|
- snippet
|
254
269
|
extensions: []
|
255
270
|
extra_rdoc_files: []
|
@@ -264,21 +279,26 @@ files:
|
|
264
279
|
- LICENCE
|
265
280
|
- README.md
|
266
281
|
- Rakefile
|
282
|
+
- bin/code_snippet
|
267
283
|
- bin/snippet
|
268
|
-
- bitbucket-pipelines.yml
|
269
284
|
- code_snippet.gemspec
|
270
285
|
- lib/code_snippet.rb
|
271
286
|
- lib/code_snippet/cli.rb
|
272
|
-
- lib/code_snippet/
|
273
|
-
- lib/code_snippet/
|
287
|
+
- lib/code_snippet/commands.rb
|
288
|
+
- lib/code_snippet/commands/list_snippets.rb
|
289
|
+
- lib/code_snippet/commands/print_path.rb
|
290
|
+
- lib/code_snippet/commands/print_version.rb
|
291
|
+
- lib/code_snippet/commands/show_snippet.rb
|
274
292
|
- lib/code_snippet/manager.rb
|
275
293
|
- lib/code_snippet/snip.rb
|
276
294
|
- lib/code_snippet/version.rb
|
277
|
-
- spec/
|
278
|
-
- spec/
|
279
|
-
- spec/
|
280
|
-
- spec/
|
281
|
-
- spec/
|
295
|
+
- spec/code_snippet/cli_spec.rb
|
296
|
+
- spec/code_snippet/commands/list_snippets_spec.rb
|
297
|
+
- spec/code_snippet/commands/print_path_spec.rb
|
298
|
+
- spec/code_snippet/commands/print_version_spec.rb
|
299
|
+
- spec/code_snippet/commands/show_snippet_spec.rb
|
300
|
+
- spec/code_snippet/manager_spec.rb
|
301
|
+
- spec/code_snippet/snip_spec.rb
|
282
302
|
- spec/spec_helper.rb
|
283
303
|
homepage: https://github.com/andrewbigger/code_snippet
|
284
304
|
licenses:
|
@@ -304,9 +324,11 @@ signing_key:
|
|
304
324
|
specification_version: 4
|
305
325
|
summary: A code snippet handling tool
|
306
326
|
test_files:
|
307
|
-
- spec/
|
308
|
-
- spec/
|
309
|
-
- spec/
|
310
|
-
- spec/
|
311
|
-
- spec/
|
327
|
+
- spec/code_snippet/cli_spec.rb
|
328
|
+
- spec/code_snippet/commands/list_snippets_spec.rb
|
329
|
+
- spec/code_snippet/commands/print_path_spec.rb
|
330
|
+
- spec/code_snippet/commands/print_version_spec.rb
|
331
|
+
- spec/code_snippet/commands/show_snippet_spec.rb
|
332
|
+
- spec/code_snippet/manager_spec.rb
|
333
|
+
- spec/code_snippet/snip_spec.rb
|
312
334
|
- spec/spec_helper.rb
|
data/bitbucket-pipelines.yml
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
image: ruby:2.6.4
|
2
|
-
|
3
|
-
pipelines:
|
4
|
-
branches:
|
5
|
-
master:
|
6
|
-
- step:
|
7
|
-
name: Build and Test
|
8
|
-
caches:
|
9
|
-
- bundler
|
10
|
-
script:
|
11
|
-
- gem install bundler
|
12
|
-
- bundle install --path .bundle
|
13
|
-
- bundle exec rake
|
14
|
-
deployment: production
|
15
|
-
|
16
|
-
definitions:
|
17
|
-
caches:
|
18
|
-
bundler: .bundle
|
@@ -1,82 +0,0 @@
|
|
1
|
-
require 'clipboard'
|
2
|
-
|
3
|
-
module CodeSnippet
|
4
|
-
# Command line interface helpers and actions
|
5
|
-
module CLI
|
6
|
-
# CLI Actions
|
7
|
-
module Commands
|
8
|
-
##
|
9
|
-
# Show displays a snippet
|
10
|
-
#
|
11
|
-
def self.show(
|
12
|
-
lang,
|
13
|
-
copy,
|
14
|
-
args = []
|
15
|
-
)
|
16
|
-
name = args.first
|
17
|
-
snippet_dir = CodeSnippet::CLI.snip_dir
|
18
|
-
|
19
|
-
manager = CodeSnippet::Manager.new(snippet_dir)
|
20
|
-
manager.load_snippets
|
21
|
-
|
22
|
-
snips = manager.find(name, lang)
|
23
|
-
raise "unable to find #{name}" if snips.empty?
|
24
|
-
|
25
|
-
snip = snips.first
|
26
|
-
if snips.length > 1
|
27
|
-
snip = CLI::Presenters.pick_from(
|
28
|
-
'Multiple snippets found, which one would you like to view?',
|
29
|
-
snips
|
30
|
-
)
|
31
|
-
end
|
32
|
-
|
33
|
-
if copy
|
34
|
-
Clipboard.copy(snip.content)
|
35
|
-
CLI.print_message("COPIED: #{snip.path}")
|
36
|
-
end
|
37
|
-
|
38
|
-
CLI::Presenters.show(snip)
|
39
|
-
end
|
40
|
-
|
41
|
-
##
|
42
|
-
# List lists snippets
|
43
|
-
#
|
44
|
-
def self.list(
|
45
|
-
lang,
|
46
|
-
_copy,
|
47
|
-
_args = []
|
48
|
-
)
|
49
|
-
snippet_dir = CodeSnippet::CLI.snip_dir
|
50
|
-
|
51
|
-
manager = CodeSnippet::Manager.new(snippet_dir)
|
52
|
-
manager.load_snippets
|
53
|
-
|
54
|
-
snippets = lang ? manager.filter_by_extension(lang) : manager.snippets
|
55
|
-
|
56
|
-
CLI::Presenters.list_snippets(snippets)
|
57
|
-
end
|
58
|
-
|
59
|
-
##
|
60
|
-
# Show snippet directory path
|
61
|
-
#
|
62
|
-
def self.path(
|
63
|
-
_lang,
|
64
|
-
_copy,
|
65
|
-
_args = []
|
66
|
-
)
|
67
|
-
CLI.print_message(CLI.snip_dir)
|
68
|
-
end
|
69
|
-
|
70
|
-
##
|
71
|
-
# Show snippet gem version
|
72
|
-
#
|
73
|
-
def self.version(
|
74
|
-
_lang,
|
75
|
-
_copy,
|
76
|
-
_args = []
|
77
|
-
)
|
78
|
-
CLI.print_message(CodeSnippet::VERSION)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
require 'tty-table'
|
2
|
-
require 'tty-prompt'
|
3
|
-
|
4
|
-
module CodeSnippet
|
5
|
-
# Command line interface helpers and actions
|
6
|
-
module CLI
|
7
|
-
# CLI Presentation functions
|
8
|
-
module Presenters
|
9
|
-
def self.pick_from(question, snips)
|
10
|
-
prompt = TTY::Prompt.new
|
11
|
-
|
12
|
-
choice = prompt.select(
|
13
|
-
question,
|
14
|
-
snips.map(&:path)
|
15
|
-
)
|
16
|
-
|
17
|
-
snips.find { |snip| snip.path == choice }
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.show(snip)
|
21
|
-
CLI.print_message(snip.content)
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.list_snippets(snippets)
|
25
|
-
result_header = %w[NAME LANG PATH]
|
26
|
-
|
27
|
-
results = TTY::Table.new(
|
28
|
-
result_header,
|
29
|
-
snippets.map do |snippet|
|
30
|
-
[
|
31
|
-
snippet.name,
|
32
|
-
snippet.ext,
|
33
|
-
snippet.path
|
34
|
-
]
|
35
|
-
end
|
36
|
-
)
|
37
|
-
|
38
|
-
CLI.print_message(results.render(:ascii))
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
@@ -1,104 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
RSpec.describe CodeSnippet::CLI::Commands do
|
4
|
-
let(:snippet_dir) { 'path/to/snippets' }
|
5
|
-
let(:lang) { '.rb' }
|
6
|
-
let(:copy) { false }
|
7
|
-
let(:vars) { [] }
|
8
|
-
|
9
|
-
before do
|
10
|
-
allow(ENV)
|
11
|
-
.to receive(:[]).with('SNIPPET_DIR')
|
12
|
-
.and_return(snippet_dir)
|
13
|
-
|
14
|
-
allow(File)
|
15
|
-
.to receive(:exist?)
|
16
|
-
.with(snippet_dir)
|
17
|
-
.and_return(true)
|
18
|
-
end
|
19
|
-
|
20
|
-
describe '.show' do
|
21
|
-
end
|
22
|
-
|
23
|
-
describe '.list' do
|
24
|
-
let(:manager) { double(CodeSnippet::Manager) }
|
25
|
-
let(:lang) { nil }
|
26
|
-
|
27
|
-
let(:snip) { double(CodeSnippet::Snip) }
|
28
|
-
let(:filtered_snips) { [snip, snip] }
|
29
|
-
let(:all_snips) { [snip, snip, snip, snip] }
|
30
|
-
|
31
|
-
before do
|
32
|
-
allow(CodeSnippet::CLI).to receive(:snip_dir)
|
33
|
-
.and_return('path/to/snips')
|
34
|
-
|
35
|
-
allow(CodeSnippet::Manager)
|
36
|
-
.to receive(:new)
|
37
|
-
.and_return(manager)
|
38
|
-
|
39
|
-
allow(manager).to receive(:load_snippets)
|
40
|
-
allow(manager)
|
41
|
-
.to receive(:filter_by_extension)
|
42
|
-
.and_return(filtered_snips)
|
43
|
-
|
44
|
-
allow(manager)
|
45
|
-
.to receive(:snippets)
|
46
|
-
.and_return(all_snips)
|
47
|
-
|
48
|
-
allow(CodeSnippet::CLI::Presenters)
|
49
|
-
.to receive(:list_snippets)
|
50
|
-
|
51
|
-
described_class.list(lang, copy, vars)
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'loads snippets' do
|
55
|
-
expect(manager).to have_received(:load_snippets)
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'presents a list of snippets' do
|
59
|
-
expect(CodeSnippet::CLI::Presenters)
|
60
|
-
.to have_received(:list_snippets)
|
61
|
-
.with(all_snips)
|
62
|
-
end
|
63
|
-
|
64
|
-
context 'when language filter is applied' do
|
65
|
-
let(:lang) { '.rb' }
|
66
|
-
|
67
|
-
it 'presents a list of filtered snippets' do
|
68
|
-
expect(CodeSnippet::CLI::Presenters)
|
69
|
-
.to have_received(:list_snippets)
|
70
|
-
.with(filtered_snips)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
describe '.path' do
|
76
|
-
before do
|
77
|
-
allow(CodeSnippet::CLI)
|
78
|
-
.to receive(:print_message)
|
79
|
-
.and_return(nil)
|
80
|
-
|
81
|
-
described_class.path(lang, copy, vars)
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'prints snippet dir' do
|
85
|
-
expect(CodeSnippet::CLI).to have_received(:print_message)
|
86
|
-
.with(snippet_dir)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
describe '.version' do
|
91
|
-
before do
|
92
|
-
allow(CodeSnippet::CLI)
|
93
|
-
.to receive(:print_message)
|
94
|
-
.and_return(nil)
|
95
|
-
|
96
|
-
described_class.version(lang, copy, vars)
|
97
|
-
end
|
98
|
-
|
99
|
-
it 'prints snippet version' do
|
100
|
-
expect(CodeSnippet::CLI).to have_received(:print_message)
|
101
|
-
.with(CodeSnippet::VERSION)
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|