code_snippet 0.2.7 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59c5c839aadfe64707cd9263e404a696ae8309679428b36e9b774146a6a4f582
4
- data.tar.gz: 053cf881c099b8b860d87c6fa8a2e89542601c1fbb8007985e6ee5a3d05ed68b
3
+ metadata.gz: 8150df6bfc6ffaa3432d2bc2e867406e2600bf4c7a66c2a4f944ead7dbfa78b3
4
+ data.tar.gz: 6377217cee2607224d5716a353b8d27ffe4311858464650c24b1bef66561cb2c
5
5
  SHA512:
6
- metadata.gz: 38e4c0336de7caae9d98e89049eca65a8e5bf3c5e5c8ffdf24e9de5a29628bcc5bf55168a0989e83a7c7717c5320b73e1c5c809f3a9af61a976680ba091dbe04
7
- data.tar.gz: be6b4f54ea41148b9dc7e90a1b7df556e402bbed6e1d93e191cf7ac86be44f22cdff5c94eebb635cfba548aaa1f9e8acb00389e2a8849ee6c6e6c520214bb3b9
6
+ metadata.gz: 948f14c6989daf999bed5b262cb6c77eb5f8d7c2e5eeb2910fcf1ae80b31b6303ec1b0e821967c74680c90ab244cd0e9b1d4ed3cc016bd188143747a7fc18e86
7
+ data.tar.gz: 1ba523a6bf7278e7183fcca35150a01ddb5dac22186426344c5f2b995bd121b9ddf4d1f92d594f247f1db6f8e24552d386cab84bcc6b48d42c8361d100614c2c
data/Gemfile.lock CHANGED
@@ -1,12 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code_snippet (0.2.7)
4
+ code_snippet (0.3.0)
5
5
  clipboard
6
6
  ffi
7
7
  hashie
8
8
  highline (~> 2.0)
9
9
  logger (~> 1.4)
10
+ thor
10
11
  tty-prompt
11
12
  tty-table
12
13
 
@@ -22,7 +23,7 @@ GEM
22
23
  thread_safe (~> 0.3, >= 0.3.1)
23
24
  bump (0.6.1)
24
25
  byebug (11.0.1)
25
- clipboard (1.3.5)
26
+ clipboard (1.3.6)
26
27
  codeclimate-engine-rb (0.4.1)
27
28
  virtus (~> 1.0)
28
29
  coderay (1.1.2)
@@ -34,7 +35,7 @@ GEM
34
35
  docile (1.3.2)
35
36
  equalizer (0.0.11)
36
37
  erubis (2.7.0)
37
- ffi (1.15.3)
38
+ ffi (1.15.4)
38
39
  flay (2.12.1)
39
40
  erubis (~> 2.7.0)
40
41
  path_expander (~> 1.0)
@@ -51,7 +52,7 @@ GEM
51
52
  json (2.2.0)
52
53
  launchy (2.4.3)
53
54
  addressable (~> 2.3)
54
- logger (1.4.3)
55
+ logger (1.4.4)
55
56
  method_source (0.9.2)
56
57
  parallel (1.19.1)
57
58
  parser (2.5.3.0)
@@ -113,6 +114,7 @@ GEM
113
114
  unicode-display_width (>= 1.5, < 3.0)
114
115
  unicode_utils (~> 1.4)
115
116
  strings-ansi (0.2.0)
117
+ thor (1.1.0)
116
118
  thread_safe (0.3.6)
117
119
  tty-color (0.6.0)
118
120
  tty-cursor (0.7.1)
@@ -155,4 +157,4 @@ DEPENDENCIES
155
157
  yard (~> 0.9)
156
158
 
157
159
  BUNDLED WITH
158
- 2.2.16
160
+ 2.2.27
data/README.md CHANGED
@@ -35,12 +35,9 @@ Once you have a set of snippets, run `snippet list` to show a list of all your s
35
35
  ```bash
36
36
  $ snippet list
37
37
 
38
- +---------------------+----+-----------------------------------------------------------------+
39
- |NAME |LANG|PATH |
40
- +---------------------+----+-----------------------------------------------------------------+
41
- |for_i |.go |/Users/me/Code/templates/snippets/golang/for_i.go |
42
- |table_test |.go |/Users/me/Code/templates/snippets/golang/table_test.go |
43
- +---------------------+----+-----------------------------------------------------------------+
38
+ NAME LANG
39
+ for_i .go
40
+ table_test .go
44
41
  ```
45
42
 
46
43
  You can then show a snippet by executing `snippet show <name>`:
@@ -53,7 +50,7 @@ for 1 := 1; i < 10; i++ {
53
50
  }
54
51
  ```
55
52
 
56
- You can copy the snippet straight to the clipboard by adding the `-c=true` to the end of the command.
53
+ You can copy the snippet straight to the clipboard by adding the `--copy` to the end of the command.
57
54
 
58
55
  ## Tests and Quality
59
56
 
data/bin/code_snippet ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib_path = File.expand_path('../lib', __dir__)
4
+ $LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)
5
+
6
+ require 'pry'
7
+ require 'code_snippet/cli'
8
+
9
+ Signal.trap('INT') do
10
+ warn("\n#{caller.join("\n")}: interrupted")
11
+ exit(1)
12
+ end
13
+
14
+ begin
15
+ CodeSnippet::CLI.start
16
+ rescue StandardError => e
17
+ puts "ERROR: #{e.message}"
18
+ puts e.backtrace.join("\n")
19
+ exit 1
20
+ end
data/bin/snippet CHANGED
@@ -1,62 +1,20 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- $LOAD_PATH.unshift(__dir__ + '/../lib')
3
+ lib_path = File.expand_path('../lib', __dir__)
4
+ $LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)
4
5
 
5
- require 'rubygems'
6
- require 'optparse'
7
- require 'highline'
8
-
9
- require 'code_snippet'
6
+ require 'pry'
10
7
  require 'code_snippet/cli'
11
8
 
12
- USAGE = "Snippet is a CLI tool for quick access to code snippets
13
-
14
- Usage:\tsnippet <command> <variables> <opts>
15
- Options:
16
- \s\s\s\s--trace\t\t trace mode on
17
- -l, --language\t snippet language filter
18
- -c, --copy\t\t copy snippet to clipboard
19
-
20
- -h, --help\t\t print usage
21
-
22
- Commands:
23
- show\t\t\t finds and shows snippet
24
- list\t\t\t displays a list of snippets
25
-
26
- path\t\t\t prints configured SNIPPET_DIR
27
- version\t\t prints gem version
28
- ".freeze
29
-
30
- @trace = false
31
- @overwrite = false
32
-
33
- OptionParser.new do |opts|
34
- opts.banner = USAGE
35
- opts.on('--trace') { @trace = true }
36
-
37
- opts.on('-l', '--language STRING') { |l| @lang = l }
38
- opts.on('-c', '--copy BOOL') { @copy = true }
39
-
40
- opts.on_tail('-h', '--help') do
41
- CodeSnippet::CLI.print_message_and_exit(USAGE)
42
- end
43
- end.parse!
9
+ Signal.trap('INT') do
10
+ warn("\n#{caller.join("\n")}: interrupted")
11
+ exit(1)
12
+ end
44
13
 
45
14
  begin
46
- raise 'ERROR: no command provided' unless ARGV[0]
47
-
48
- cmd = ARGV.shift
49
- CodeSnippet::CLI::Commands.public_send(
50
- cmd.to_sym,
51
- @lang,
52
- !@copy.nil?,
53
- ARGV
54
- )
15
+ CodeSnippet::CLI.start
55
16
  rescue StandardError => e
56
- CodeSnippet::CLI.print_message(e)
57
- CodeSnippet::CLI.print_message(e.backtrace) if @trace
58
- CodeSnippet::CLI.print_message("\n")
59
- CodeSnippet::CLI.print_message_and_exit(USAGE)
17
+ puts "ERROR: #{e.message}"
18
+ puts e.backtrace.join("\n")
19
+ exit 1
60
20
  end
61
-
62
- exit(0)
data/code_snippet.gemspec CHANGED
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency 'hashie'
24
24
  spec.add_dependency 'highline', '~> 2.0'
25
25
  spec.add_dependency 'logger', '~> 1.4'
26
+ spec.add_dependency 'thor'
26
27
  spec.add_dependency 'tty-prompt'
27
28
  spec.add_dependency 'tty-table'
28
29
 
@@ -1,19 +1,78 @@
1
- require 'code_snippet/cli/commands'
2
- require 'code_snippet/cli/presenters'
3
-
1
+ require 'thor'
4
2
  require 'logger'
5
3
 
4
+ require 'code_snippet'
5
+ require 'code_snippet/commands'
6
+
6
7
  module CodeSnippet
7
8
  # Command line interface helpers and actions
8
- module CLI
9
- # CLI Helpers
10
- class <<self
9
+ class CLI < ::Thor
10
+ package_name 'CodeSnippet'
11
+
12
+ desc 'version', 'Print code snippet version to STDOUT'
13
+
14
+ ##
15
+ # Runs version command
16
+ #
17
+ def version
18
+ cmd = CodeSnippet::Commands::PrintVersion.new(options)
19
+ cmd.run
20
+ end
21
+
22
+ map %w[--version -v] => :version
23
+
24
+ desc 'path', 'Print snippet directory path to STDOUT'
25
+
26
+ ##
27
+ # Runs version command
28
+ #
29
+ def path
30
+ cmd = CodeSnippet::Commands::PrintPath.new(snip_dir, options)
31
+ cmd.run
32
+ end
33
+
34
+ desc 'show', 'Find and show snippet'
35
+
36
+ method_option(
37
+ :name,
38
+ type: :string,
39
+ desc: 'Name of snippet'
40
+ )
41
+
42
+ method_option(
43
+ :copy,
44
+ type: :boolean,
45
+ desc: 'Should copy snippet to clipboard',
46
+ default: false
47
+ )
48
+
49
+ ##
50
+ # Show snippet command
51
+ #
52
+ def show
53
+ cmd = CodeSnippet::Commands::ShowSnippet.new(manager, options)
54
+ cmd.run
55
+ end
56
+
57
+ desc 'list', 'List all known snippets'
58
+
59
+ ##
60
+ # Show snippet command
61
+ #
62
+ def list
63
+ cmd = CodeSnippet::Commands::ListSnippets.new(manager, options)
64
+ cmd.run
65
+ end
66
+
67
+ no_commands do
11
68
  ##
12
69
  # Retrieves snippet dir from environment
13
70
  #
14
71
  def snip_dir
15
72
  @snippet_dir = ENV['SNIPPET_DIR']
73
+
16
74
  raise 'SNIPPET_DIR environment variable not set' unless @snippet_dir
75
+
17
76
  unless File.exist?(@snippet_dir)
18
77
  raise "SNIPPET_DIR #{@snippet_dir} does not exist"
19
78
  end
@@ -22,30 +81,15 @@ module CodeSnippet
22
81
  end
23
82
 
24
83
  ##
25
- # Creates logger for printing messages
84
+ # Loads and creates snippet manager
26
85
  #
27
- def logger
28
- @logger ||= Logger.new(STDOUT)
29
- @logger.formatter = proc do |_sev, _time, _prog, msg|
30
- "#{msg}\n"
31
- end
32
-
33
- @logger
34
- end
35
-
36
- ##
37
- # Prints command line message to CLI
86
+ # @return [CodeSnippet::Manager]
38
87
  #
39
- def print_message(message)
40
- logger.info(message)
41
- end
88
+ def manager
89
+ @manager ||= CodeSnippet::Manager.new(snip_dir)
90
+ @manager.load
42
91
 
43
- ##
44
- # Prints a message and then exits with given status code
45
- #
46
- def print_message_and_exit(message, exit_code = 1)
47
- print_message(message)
48
- exit(exit_code)
92
+ @manager
49
93
  end
50
94
  end
51
95
  end
@@ -0,0 +1,41 @@
1
+ require 'tty-prompt'
2
+ require 'tty-table'
3
+
4
+ module CodeSnippet
5
+ module Commands
6
+ # Lists snippets in STDOUT
7
+ class ListSnippets
8
+ ##
9
+ # Output table header
10
+ #
11
+ RESULT_HEADER = %w[NAME LANG].freeze
12
+
13
+ def initialize(manager, options = Hashie::Mash.new)
14
+ @manager = manager
15
+ @options = options
16
+ @prompt = TTY::Prompt.new
17
+ end
18
+
19
+ ##
20
+ # Prints a list of snippets to STDOUT
21
+ #
22
+ def run
23
+ puts(snippet_list_table.render.strip)
24
+ end
25
+
26
+ private
27
+
28
+ def snippet_list_table
29
+ TTY::Table.new(
30
+ header: RESULT_HEADER,
31
+ rows: @manager.snippets.map do |snip|
32
+ [
33
+ snip.name.downcase,
34
+ snip.ext
35
+ ]
36
+ end
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ require 'tty-prompt'
2
+
3
+ module CodeSnippet
4
+ module Commands
5
+ # Prints gem version to STDOUT
6
+ class PrintPath
7
+ def initialize(snip_dir, options = Hashie::Mash.new)
8
+ @snip_dir = snip_dir
9
+ @options = options
10
+ end
11
+
12
+ ##
13
+ # Prints gem version
14
+ #
15
+ def run
16
+ puts(@snip_dir)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require 'tty-prompt'
2
+
3
+ module CodeSnippet
4
+ module Commands
5
+ # Prints snippet path to STDOUT
6
+ class PrintVersion
7
+ def initialize(options = Hashie::Mash.new)
8
+ @options = options
9
+ end
10
+
11
+ ##
12
+ # Prints snippet path
13
+ #
14
+ def run
15
+ puts(CodeSnippet::VERSION)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,57 @@
1
+ require 'clipboard'
2
+ require 'tty-prompt'
3
+
4
+ module CodeSnippet
5
+ module Commands
6
+ # Shows snippet in STDOUT
7
+ class ShowSnippet
8
+ def initialize(manager, options = Hashie::Mash.new)
9
+ @manager = manager
10
+ @options = options
11
+ @prompt = TTY::Prompt.new
12
+ end
13
+
14
+ ##
15
+ # Prints snippet path
16
+ #
17
+ def run
18
+ name = @options.name
19
+ name ||= @prompt.select(
20
+ 'Please choose snippet',
21
+ @manager.snippet_names
22
+ )
23
+
24
+ snips = find_snippets(name)
25
+ snip = pick_snippet(snips)
26
+
27
+ if @options.copy
28
+ Clipboard.copy(snip.content)
29
+ puts "COPIED: #{snip.path}"
30
+ end
31
+
32
+ puts(snip.content)
33
+ end
34
+
35
+ def find_snippets(name)
36
+ snips = @manager.find(name)
37
+ raise "unable to find #{name}" if snips.empty?
38
+
39
+ snips
40
+ end
41
+
42
+ def pick_snippet(snips)
43
+ snip = snips.first
44
+ if snips.length > 1
45
+ snip_name = @prompt.select(
46
+ 'Multiple snippets found, which one would you like to view?',
47
+ snips.map(&:name)
48
+ )
49
+
50
+ snip = @manager.find(snip_name).first
51
+ end
52
+
53
+ snip
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,13 @@
1
+ require 'hashie'
2
+
3
+ COMMANDS = File.join(__dir__, 'commands', '*.rb')
4
+
5
+ Dir[COMMANDS].sort.each do |file|
6
+ require file
7
+ end
8
+
9
+ module CodeSnippet
10
+ # Module for CLI command classes
11
+ module Commands
12
+ end
13
+ end
@@ -10,7 +10,7 @@ module CodeSnippet
10
10
  @snippets = []
11
11
  end
12
12
 
13
- def load_snippets
13
+ def load
14
14
  Dir.glob(File.join(@snippet_dir, '**', '*')).each do |file|
15
15
  next if File.directory?(file)
16
16
 
@@ -46,5 +46,9 @@ module CodeSnippet
46
46
  ext_query = ->(snip) { snip.ext == ext }
47
47
  filter(ext_query)
48
48
  end
49
+
50
+ def snippet_names
51
+ @snippets.map(&:name)
52
+ end
49
53
  end
50
54
  end
@@ -1,3 +1,3 @@
1
1
  module CodeSnippet
2
- VERSION = '0.2.7'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
@@ -0,0 +1,203 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CodeSnippet::CLI do
4
+ let(:options) { Hashie::Mash.new }
5
+ let(:snip_dir) { 'path/to/snippets' }
6
+ let(:snip_exist) { true }
7
+
8
+ subject { described_class.new }
9
+
10
+ before do
11
+ allow(subject).to receive(:options)
12
+ .and_return(options)
13
+ end
14
+
15
+ describe '#version' do
16
+ let(:command) { double }
17
+
18
+ before do
19
+ allow(command).to receive(:run)
20
+ allow(CodeSnippet::Commands::PrintVersion)
21
+ .to receive(:new)
22
+ .and_return(command)
23
+
24
+ subject.version
25
+ end
26
+
27
+ it 'passes options to command' do
28
+ expect(CodeSnippet::Commands::PrintVersion)
29
+ .to have_received(:new).with(options)
30
+ end
31
+
32
+ it 'runs command' do
33
+ expect(command).to have_received(:run)
34
+ end
35
+ end
36
+
37
+ describe '#path' do
38
+ let(:command) { double }
39
+
40
+ before do
41
+ allow(ENV)
42
+ .to receive(:[])
43
+ .with('SNIPPET_DIR')
44
+ .and_return(snip_dir)
45
+
46
+ allow(File)
47
+ .to receive(:exist?)
48
+ .with(snip_dir)
49
+ .and_return(snip_exist)
50
+
51
+ allow(command).to receive(:run)
52
+ allow(CodeSnippet::Commands::PrintPath)
53
+ .to receive(:new)
54
+ .and_return(command)
55
+
56
+ subject.path
57
+ end
58
+
59
+ it 'passes snip_dir and options to command' do
60
+ expect(CodeSnippet::Commands::PrintPath)
61
+ .to have_received(:new).with(snip_dir, options)
62
+ end
63
+
64
+ it 'runs command' do
65
+ expect(command).to have_received(:run)
66
+ end
67
+ end
68
+
69
+ describe '#show' do
70
+ let(:command) { double }
71
+ let(:manager) { CodeSnippet::Manager }
72
+
73
+ before do
74
+ allow(command).to receive(:run)
75
+ allow(subject)
76
+ .to receive(:manager)
77
+ .and_return(manager)
78
+
79
+ allow(CodeSnippet::Commands::ShowSnippet)
80
+ .to receive(:new)
81
+ .and_return(command)
82
+
83
+ subject.show
84
+ end
85
+
86
+ it 'passes manager and options to command' do
87
+ expect(CodeSnippet::Commands::ShowSnippet)
88
+ .to have_received(:new).with(manager, options)
89
+ end
90
+
91
+ it 'runs command' do
92
+ expect(command).to have_received(:run)
93
+ end
94
+ end
95
+
96
+ describe '#list' do
97
+ let(:command) { double }
98
+ let(:manager) { CodeSnippet::Manager }
99
+
100
+ before do
101
+ allow(command).to receive(:run)
102
+ allow(subject)
103
+ .to receive(:manager)
104
+ .and_return(manager)
105
+
106
+ allow(CodeSnippet::Commands::ListSnippets)
107
+ .to receive(:new)
108
+ .and_return(command)
109
+
110
+ subject.list
111
+ end
112
+
113
+ it 'passes manager and options to command' do
114
+ expect(CodeSnippet::Commands::ListSnippets)
115
+ .to have_received(:new).with(manager, options)
116
+ end
117
+
118
+ it 'runs command' do
119
+ expect(command).to have_received(:run)
120
+ end
121
+ end
122
+
123
+ describe '#snip_dir' do
124
+ let(:snip_dir) { 'path/to/snippet/dir' }
125
+ let(:snip_dir_exist) { false }
126
+
127
+ before do
128
+ allow(ENV)
129
+ .to receive(:[])
130
+ .with('SNIPPET_DIR')
131
+ .and_return(snip_dir)
132
+
133
+ allow(File)
134
+ .to receive(:exist?)
135
+ .and_return(snip_dir_exist)
136
+ end
137
+
138
+ context 'when environment variable not set' do
139
+ let(:snip_dir) { nil }
140
+
141
+ it 'raises env var not set runtime error' do
142
+ expect do
143
+ subject.snip_dir
144
+ end.to raise_error(
145
+ RuntimeError,
146
+ 'SNIPPET_DIR environment variable not set'
147
+ )
148
+ end
149
+ end
150
+
151
+ context 'when snippet directory does not exist' do
152
+ it 'raises env var not exist runtime error' do
153
+ expect do
154
+ subject.snip_dir
155
+ end.to raise_error(
156
+ RuntimeError,
157
+ "SNIPPET_DIR #{snip_dir} does not exist"
158
+ )
159
+ end
160
+ end
161
+
162
+ context 'when environment variable set and directory exists' do
163
+ let(:snip_dir_exist) { true }
164
+
165
+ it 'returns snippet dir' do
166
+ expect(subject.snip_dir).to eq snip_dir
167
+ end
168
+ end
169
+ end
170
+
171
+ describe '#manager' do
172
+ let(:snip_dir) { 'path/to/snip/dir' }
173
+ let(:manager) { double(CodeSnippet::Manager) }
174
+
175
+ before do
176
+ allow(subject)
177
+ .to receive(:snip_dir)
178
+ .and_return(snip_dir)
179
+
180
+ allow(CodeSnippet::Manager)
181
+ .to receive(:new)
182
+ .and_return(manager)
183
+
184
+ allow(manager).to receive(:load)
185
+
186
+ @manager = subject.manager
187
+ end
188
+
189
+ it 'creates a new snip with snip dir' do
190
+ expect(CodeSnippet::Manager)
191
+ .to have_received(:new)
192
+ .with(snip_dir)
193
+ end
194
+
195
+ it 'loads manager' do
196
+ expect(manager).to have_received(:load)
197
+ end
198
+
199
+ it 'returns manager' do
200
+ expect(@manager).to eq manager
201
+ end
202
+ end
203
+ end