snipgem 0.0.8 → 0.0.9
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/Rakefile +5 -1
- data/lib/controllers/controller.rb +28 -0
- data/lib/languages/languages.rb +19 -0
- data/{app → lib}/models/snippet.rb +3 -11
- data/lib/models/utils/batchprocessing.rb +13 -0
- data/{app → lib}/models/utils/codescanner.rb +12 -2
- data/{app → lib}/models/utils/destinationfilewriter.rb +3 -9
- data/lib/models/utils/searchfile.rb +67 -0
- data/{app → lib}/models/utils/sourcefilereader.rb +0 -0
- data/lib/snip.rb +161 -134
- data/lib/snip/version.rb +1 -1
- data/lib/views/viewformatter.rb +222 -0
- data/my_snips.rb +0 -198
- data/rspec/rspec.rb +1 -1
- data/snip.gemspec +1 -1
- metadata +11 -11
- data/app/controllers/controller.rb +0 -30
- data/app/models/utils/batchprocessing.rb +0 -11
- data/app/models/utils/searchfile.rb +0 -42
- data/app/views/viewformatter.rb +0 -169
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f3ac79c11e098d74ad90e8122495c7673c9731c1
|
4
|
+
data.tar.gz: 119b58406d5ba4a91e2541784d731930a620a572
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 819aa8372e1ba4c2ce909d6886a9788db4d89d3fabe55250d46349b937f7949a00f3f49a0611d7c0d15d701f99a9eacf8dd9c92ccc8b338511aaa6c0c9b535fb
|
7
|
+
data.tar.gz: e17304fdfae515fbd8eaf992b21a12c5a5513f6a988677fdf2d0e053bec202efd59880d65decab4b87ad619f7e33ece06525d7da21f5b7fba8a87994bd7c29b9
|
data/Rakefile
CHANGED
@@ -11,7 +11,8 @@ task :seed do
|
|
11
11
|
test_str2 = "#<$> Title ggg\ndef method\n \"code\"\nend\n#</$>\n\n#<$> Title 2323\ndef method\n \"code\"\nend\n#</$>\n\n#<$> Title 4\ndef method\n \"code\"\nend\n#</$>"
|
12
12
|
test_str3 = "# <snip> Title of first snip\n desc \"create the database\"\n task :create do\n puts \"Creating file \#{DB_PATH} if it doesn't exist...\"\n touch DB_PATH\n end\n# </snip>\n\n# <snip> Title of this snip\n desc \"drop the database\"\n task :drop do\n puts \"Deleting \#{DB_PATH}...\"\n rm_f DB_PATH\n end\n# </snip>\n\n# <snip> Title of first snip\n desc \"create the database\"\n task :create do\n puts \"Creating file \#{DB_PATH} if it doesn't exist...\"\n touch DB_PATH\n end\n# </snip>\n\n# <snip> Title of this snip\n desc \"drop the database\"\n task :drop do\n puts \"Deleting \#{DB_PATH}...\"\n rm_f DB_PATH\n end\n# </snip>"
|
13
13
|
test_str_js = "//<$> Title ggg\nvar test1 = 44;\nvar test2 = 22;\n//</$>\n\n//<$>\nvar test4 = \"Hola\";\n//</$>\n\n"
|
14
|
-
|
14
|
+
test_str_erb = "<!-- <$> Title ggg -->\n <% @snips.each do |snip| %>\n <p><%= snip.title %></p> \n<% end %>\n<!-- </$> --> \n\n<!-- <$> Title ggg -->\n <% @snips.each do |snip| %>\n <p><%= snip.title %></p> \n<% end %>\n<!-- </$> --> \n\n"
|
15
|
+
test_str_html = "<!-- <$> Title ggg -->\n <p>Hello world</p>\n <h1>title</h1> \n<!-- </$> --> \n\n<!-- <$> Title ggg -->\n <p>Hello world</p>\n <h1>title</h1> \n<!-- </$> --> \n\n"
|
15
16
|
|
16
17
|
|
17
18
|
FileUtils.rm_rf('rspec/test_snip_files')
|
@@ -33,6 +34,9 @@ task :seed do
|
|
33
34
|
File.open("rspec/test_snip_files/subfolder/test_erb.erb", "w+") do |file|
|
34
35
|
file << test_str_erb
|
35
36
|
end
|
37
|
+
File.open("rspec/test_snip_files/subfolder/test_html.html", "w+") do |file|
|
38
|
+
file << test_str_erb
|
39
|
+
end
|
36
40
|
end
|
37
41
|
|
38
42
|
desc "Run the specs"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative '../models/utils/sourcefilereader'
|
2
|
+
require_relative '../models/utils/codescanner'
|
3
|
+
require_relative '../models/utils/destinationfilewriter'
|
4
|
+
require_relative '../models/utils/searchfile'
|
5
|
+
require_relative '../languages/languages'
|
6
|
+
|
7
|
+
module CommandLineController
|
8
|
+
|
9
|
+
extend self
|
10
|
+
|
11
|
+
def run(file)
|
12
|
+
file_read = SourceFileReaderWriter.new(file)
|
13
|
+
to_run = file_read.convert_to_array_of_lines
|
14
|
+
mismatch_status = CodeScanner.run(to_run, SourceFileReaderWriter.file_to_open)
|
15
|
+
abort(ViewFormatter.mismatched_tags(mismatch_status)) if mismatch_status
|
16
|
+
file_writing
|
17
|
+
file_read.overwrite_existing_snips
|
18
|
+
end
|
19
|
+
|
20
|
+
def file_writing
|
21
|
+
DestinationFileWriter.run(Snippet.snippet_array)
|
22
|
+
Language.languages.keys.each do |lang|
|
23
|
+
DestinationFileWriter.run(Snippet.select_lang_snippets(lang), lang) if Snippet.select_lang_snippets(lang).any?
|
24
|
+
end
|
25
|
+
Snippet.snippet_array = []
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Language
|
2
|
+
|
3
|
+
def self.languages
|
4
|
+
{
|
5
|
+
"js" => ["//"],
|
6
|
+
"erb" => ["<!--", "-->"],
|
7
|
+
"rb" => ["#"],
|
8
|
+
"html" => ["<!--", "-->"],
|
9
|
+
"py" => ["#"],
|
10
|
+
"php" => ["<?php /*", "*/ ?>"],
|
11
|
+
"css" => ["/*", "*/"]
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.supports?(lang)
|
16
|
+
self.languages.key?(lang)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -17,18 +17,10 @@ attr_accessor :snippet_array
|
|
17
17
|
@@snippet_array = arg
|
18
18
|
end
|
19
19
|
|
20
|
-
def self.
|
21
|
-
@@snippet_array.select {|snippet| snippet.filename.end_with?("
|
20
|
+
def self.select_lang_snippets(lang)
|
21
|
+
@@snippet_array.select {|snippet| snippet.filename.end_with?(".#{lang}") || snippet.filename.end_with?("(.#{lang})")}
|
22
22
|
end
|
23
|
-
|
24
|
-
def self.erb_snippets
|
25
|
-
@@snippet_array.select {|snippet| snippet.filename.end_with?(".erb") || snippet.filename.end_with?("(.erb)") }
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.js_snippets
|
29
|
-
@@snippet_array.select {|snippet| snippet.filename.end_with?(".js") || snippet.filename.end_with?("(.js)") }
|
30
|
-
end
|
31
|
-
|
23
|
+
|
32
24
|
def initialize(args = {})
|
33
25
|
@code = args[:code]
|
34
26
|
@title = args[:title]
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class BatchProcessing
|
2
|
+
|
3
|
+
require_relative '../../languages/languages'
|
4
|
+
|
5
|
+
def self.process(directory)
|
6
|
+
directory = directory.chomp('/')
|
7
|
+
Dir.glob(directory + "/**/*.{#{Language.languages.keys.join(',')}}") do |file|
|
8
|
+
next if file == '.' or file == '..' or Dir.exist?(file)
|
9
|
+
CommandLineController.run(file)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -9,8 +9,14 @@ class CodeScanner
|
|
9
9
|
def self.run(scan_array, filename)
|
10
10
|
@scan_array = scan_array
|
11
11
|
while @scan_array.join.include?('<snip>') || @scan_array.join.include?('<$>')
|
12
|
-
|
12
|
+
code = array_range
|
13
|
+
if code
|
14
|
+
Snippet.new(code: code, title: @title, line: @line, filename: filename)
|
15
|
+
else
|
16
|
+
return {filename: filename, line: @line} #in case of mismatch
|
17
|
+
end
|
13
18
|
end
|
19
|
+
return nil
|
14
20
|
end
|
15
21
|
|
16
22
|
def self.find_begin_range
|
@@ -27,8 +33,12 @@ class CodeScanner
|
|
27
33
|
|
28
34
|
def self.find_end_range
|
29
35
|
@scan_array.each_with_index do |line, index|
|
36
|
+
if line.include?('<snip>') || line.include?('<$>') #mismatch before closing tag
|
37
|
+
return false
|
38
|
+
end
|
30
39
|
if line.include?('</snip>') || line.include?('</$>')
|
31
40
|
strip_snip_tag(index)
|
41
|
+
index
|
32
42
|
return @end_scan = index
|
33
43
|
end
|
34
44
|
end
|
@@ -37,7 +47,7 @@ class CodeScanner
|
|
37
47
|
|
38
48
|
def self.array_range
|
39
49
|
find_begin_range
|
40
|
-
find_end_range
|
50
|
+
return false unless find_end_range
|
41
51
|
@scan_array[@begin_scan+1..@end_scan-1].join
|
42
52
|
end
|
43
53
|
|
@@ -3,6 +3,7 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
require_relative '../../views/viewformatter'
|
6
|
+
require_relative '../../languages/languages'
|
6
7
|
CONFIG_PATH = File.expand_path("../../../../config/filepath.config", __FILE__)
|
7
8
|
LOG_PATH = File.expand_path("../../../../log/snip.log", __FILE__)
|
8
9
|
|
@@ -47,15 +48,8 @@ module DestinationFileWriter
|
|
47
48
|
end
|
48
49
|
|
49
50
|
def run(snippet_array, type=nil)
|
50
|
-
|
51
|
-
|
52
|
-
filename = directory + "/my_snips.rb"
|
53
|
-
File.new(filename, 'w') unless File.exist?(filename)
|
54
|
-
when "js"
|
55
|
-
filename = directory + "/my_snips.js"
|
56
|
-
File.new(filename, 'w') unless File.exist?(filename)
|
57
|
-
when "erb"
|
58
|
-
filename = directory + "/my_snips.erb"
|
51
|
+
if Language.supports?(type)
|
52
|
+
filename = directory + "/my_snips.#{type}"
|
59
53
|
File.new(filename, 'w') unless File.exist?(filename)
|
60
54
|
else
|
61
55
|
filename = @snip_file_name
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module SearchDisplay
|
2
|
+
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def check_for_title(line)
|
6
|
+
line.match(/\*\*\*\* Snippet \d+:/)
|
7
|
+
end
|
8
|
+
|
9
|
+
def return_search_results(file,text=nil,ext=nil)
|
10
|
+
search_results = search_snips(divide_snips(file), text, ext)
|
11
|
+
if search_results.any?
|
12
|
+
search_results
|
13
|
+
else
|
14
|
+
ViewFormatter.no_results
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def divide_snips(file)
|
19
|
+
snip_array = []
|
20
|
+
snip_str = ""
|
21
|
+
File.open(file, "r").each do |line|
|
22
|
+
if check_for_title(line)
|
23
|
+
snip_array << snip_str
|
24
|
+
snip_str = line
|
25
|
+
else
|
26
|
+
snip_str << line
|
27
|
+
end
|
28
|
+
end
|
29
|
+
snip_array << snip_str
|
30
|
+
snip_array.shift # todo: check on this
|
31
|
+
snip_array
|
32
|
+
end
|
33
|
+
|
34
|
+
def search_snips(array,text,ext)
|
35
|
+
array.select do |snip|
|
36
|
+
if ext
|
37
|
+
includes_ext = (snip.include?("(.#{ext})") || snip.include?(".#{ext}:"))
|
38
|
+
else
|
39
|
+
includes_ext = true
|
40
|
+
end
|
41
|
+
# matches either clipboard or normal formatting
|
42
|
+
if text
|
43
|
+
includes_text = smart_text_search_results?(snip, text)
|
44
|
+
else
|
45
|
+
includes_text = true
|
46
|
+
end
|
47
|
+
includes_ext && includes_text
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def smart_text_search_results?(snip, text)
|
52
|
+
snip = snip.downcase
|
53
|
+
words_arr = text.split(" ")
|
54
|
+
words_arr.each do |word|
|
55
|
+
unless snip.include?(word.downcase)
|
56
|
+
return false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
true
|
60
|
+
end
|
61
|
+
|
62
|
+
def delete(file, ids)
|
63
|
+
return_search_results(file).reject do |snip|
|
64
|
+
ids.include?(snip.match(/\*\*\*\* Snippet (\d+):/).captures[0])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
File without changes
|
data/lib/snip.rb
CHANGED
@@ -1,139 +1,166 @@
|
|
1
1
|
require "snip/version"
|
2
2
|
require "clipboard"
|
3
|
-
require_relative '
|
4
|
-
require_relative '
|
3
|
+
require_relative 'controllers/controller'
|
4
|
+
require_relative 'models/utils/batchprocessing'
|
5
|
+
require_relative 'languages/languages'
|
5
6
|
|
7
|
+
|
6
8
|
module Snip
|
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
|
-
|
9
|
+
class Run
|
10
|
+
|
11
|
+
### execution flow for ARGV
|
12
|
+
def execute
|
13
|
+
unless ARGV[0]
|
14
|
+
abort(ViewFormatter.no_args_message)
|
15
|
+
end
|
16
|
+
|
17
|
+
if ARGV[0] == "--help" || ARGV[0] == "-h"
|
18
|
+
abort(ViewFormatter.terminal_help_message)
|
19
|
+
end
|
20
|
+
|
21
|
+
# set file location
|
22
|
+
if ARGV[0] == "-f"
|
23
|
+
set_output_file_location
|
24
|
+
end
|
25
|
+
|
26
|
+
# show log
|
27
|
+
if ARGV[0] == "-l"
|
28
|
+
puts ViewFormatter.show_log(DestinationFileWriter.log_filepath)
|
29
|
+
abort
|
30
|
+
end
|
31
|
+
|
32
|
+
# reindex
|
33
|
+
if ARGV[0] == "-i"
|
34
|
+
DestinationFileWriter.reindex_all
|
35
|
+
abort(ViewFormatter.reindexed)
|
36
|
+
end
|
37
|
+
|
38
|
+
# restore whitespace
|
39
|
+
if ARGV[0] == "-w"
|
40
|
+
display_file = DestinationFileWriter.return_display_file
|
41
|
+
DestinationFileWriter.restore_whitespace(display_file, SearchDisplay.return_search_results(display_file))
|
42
|
+
abort(ViewFormatter.whitespace_success)
|
43
|
+
end
|
44
|
+
|
45
|
+
# delete
|
46
|
+
if ARGV[0] == "--delete"
|
47
|
+
unless ARGV[1]
|
48
|
+
abort(ViewFormatter.delete_error)
|
49
|
+
end
|
50
|
+
delete_specified(ARGV[1])
|
51
|
+
abort(ViewFormatter.delete_success)
|
52
|
+
end
|
53
|
+
|
54
|
+
# display snips function
|
55
|
+
if ARGV[0] == "-a"
|
56
|
+
puts ViewFormatter.show_snips(DestinationFileWriter.snip_filepath)
|
57
|
+
abort
|
58
|
+
end
|
59
|
+
|
60
|
+
if ARGV[0] == "-s" || ARGV[0] == "-d"
|
61
|
+
search_snips
|
62
|
+
abort
|
63
|
+
end
|
64
|
+
|
65
|
+
# at this point we need to make sure config file exists - may want to refactor this check to occur before -d (handled in show_snips currently)
|
66
|
+
DestinationFileWriter.check_config_file_for_snip_file
|
67
|
+
|
68
|
+
# clipboard function
|
69
|
+
if ARGV[0] == "-c"
|
70
|
+
code = Clipboard.paste + "\n"
|
71
|
+
snip_from_clipboard(code)
|
72
|
+
abort(ViewFormatter.clipboard_success(code))
|
73
|
+
end
|
74
|
+
|
75
|
+
# no CL arguments (e.g. '-c') - straight snip
|
76
|
+
perform_file_or_batch_processing
|
77
|
+
end
|
78
|
+
|
79
|
+
# -f
|
80
|
+
def set_output_file_location
|
81
|
+
if ARGV[1]
|
82
|
+
check_if_valid_directory(ARGV[1])
|
83
|
+
else
|
84
|
+
abort(ViewFormatter.check_for_file((DestinationFileWriter.snip_filepath)))
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def check_if_valid_directory(directory)
|
89
|
+
if File.directory?(directory)
|
90
|
+
DestinationFileWriter.save_file_path_to_config_file(directory)
|
91
|
+
abort(ViewFormatter.new_file_path)
|
92
|
+
else
|
93
|
+
abort(ViewFormatter.need_to_specify_a_directory)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# --delete
|
98
|
+
def delete_specified(items)
|
99
|
+
if items.include?(",")
|
100
|
+
items = items.split(",")
|
101
|
+
else
|
102
|
+
items = [items]
|
103
|
+
end
|
104
|
+
display_file = DestinationFileWriter.return_display_file
|
105
|
+
DestinationFileWriter.rewrite_file(display_file, SearchDisplay.delete(display_file, items))
|
106
|
+
DestinationFileWriter.reindex_all
|
107
|
+
end
|
108
|
+
|
109
|
+
# -d
|
110
|
+
def search_snips
|
111
|
+
if incorrect_num_of_args?(:d)
|
112
|
+
abort(ViewFormatter.display_error)
|
113
|
+
end
|
114
|
+
|
115
|
+
if Language.supports?(ARGV[1]) # ext search
|
116
|
+
ext_to_search = ARGV[1]
|
117
|
+
string_to_search = ARGV[2]
|
118
|
+
else # string search only
|
119
|
+
string_to_search = ARGV[1]
|
120
|
+
end
|
121
|
+
|
122
|
+
puts SearchDisplay.return_search_results(DestinationFileWriter.return_display_file, string_to_search, ext_to_search)
|
123
|
+
end
|
124
|
+
|
125
|
+
def incorrect_num_of_args?(method)
|
126
|
+
!ARGV[1] || ARGV[3] || (!Language.supports?(ARGV[1]) && ARGV[2]) if method == :d
|
127
|
+
end
|
128
|
+
|
129
|
+
# -c
|
130
|
+
def snip_from_clipboard(code)
|
131
|
+
if ARGV[2] && (Language.supports?(ARGV[1]) || ARGV[1] == "misc") #ARGV[2] is title, ARGV[1] is ext
|
132
|
+
type = ARGV[1]
|
133
|
+
title = ARGV[2]
|
134
|
+
elsif ARGV[1].nil?
|
135
|
+
puts ViewFormatter.clipboard_prompts[0]
|
136
|
+
type = $stdin.gets.chomp
|
137
|
+
puts ViewFormatter.clipboard_prompts[1]
|
138
|
+
title = $stdin.gets.chomp
|
139
|
+
else
|
140
|
+
abort(ViewFormatter.clipboard_instructions)
|
141
|
+
end
|
142
|
+
|
143
|
+
origin = ViewFormatter.clipboard_origin(type)
|
144
|
+
Snippet.new(code: code, title: title, line: nil, filename: origin)
|
145
|
+
CommandLineController.file_writing
|
146
|
+
end
|
147
|
+
|
148
|
+
# no flags
|
149
|
+
def perform_file_or_batch_processing
|
150
|
+
if File.directory?(ARGV[0])
|
151
|
+
BatchProcessing.process(ARGV[0])
|
152
|
+
elsif File.file?(ARGV[0])
|
153
|
+
file_arr = ARGV[0].split('.')
|
154
|
+
if file_arr.length > 1 && Language.supports?(file_arr[-1])
|
155
|
+
CommandLineController.run(ARGV[0])
|
156
|
+
else
|
157
|
+
abort(ViewFormatter.invalid_input_file)
|
158
|
+
end
|
159
|
+
else
|
160
|
+
abort(ViewFormatter.input_file_not_found)
|
161
|
+
end
|
162
|
+
|
163
|
+
puts ViewFormatter.success_message(DestinationFileWriter.full_file_directory)
|
164
|
+
end
|
165
|
+
end
|
139
166
|
end
|
data/lib/snip/version.rb
CHANGED
@@ -0,0 +1,222 @@
|
|
1
|
+
# View component for file writing operations
|
2
|
+
require 'date'
|
3
|
+
require_relative '../languages/languages'
|
4
|
+
|
5
|
+
module ViewFormatter
|
6
|
+
|
7
|
+
extend self
|
8
|
+
|
9
|
+
### utility
|
10
|
+
def conjunctionator(list = Language.languages, conj="or")
|
11
|
+
if list.class.name == "Hash"
|
12
|
+
list = list.keys
|
13
|
+
end
|
14
|
+
str = "#{list[0..-2].join(", ")} #{conj} #{list[-1]}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def line_check(line)
|
18
|
+
line ? ':' + line.to_s : nil
|
19
|
+
end
|
20
|
+
|
21
|
+
### file output formatting
|
22
|
+
def snippet_indexer(index, title, type)
|
23
|
+
languages = Language.languages
|
24
|
+
if languages.key?(type)
|
25
|
+
"#{languages[type][0]} **** Snippet " + (index).to_s + ": #{title} **** #{languages[type][1]} \n"
|
26
|
+
else
|
27
|
+
"# **** Snippet " + (index).to_s + ": #{title} **** \n"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def status_line(line, type, file)
|
32
|
+
languages = Language.languages
|
33
|
+
if languages.key?(type)
|
34
|
+
"#{languages[type][0]} Snipped from #{file}#{line_check(line)} on #{Time.now.strftime("%m-%d-%Y")} #{languages[type][1]}"
|
35
|
+
else
|
36
|
+
"# Snipped from #{file}#{line_check(line)} on #{Time.now.strftime("%m-%d-%Y")}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
### file management (based on check)config_file function in DestinationFileWriter)
|
41
|
+
def specify_path
|
42
|
+
"Please specify where you would like to save your snippets by running 'snip -f <filepath>'"
|
43
|
+
end
|
44
|
+
|
45
|
+
def output_file_not_found(file)
|
46
|
+
"Output file '#{file}' not found, please specify existing file location or desired location for new file by running 'snip -f <filepath>'"
|
47
|
+
end
|
48
|
+
|
49
|
+
def new_file(filename)
|
50
|
+
"New snippet file created at #{File.absolute_path(filename)}"
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
### default snip functionality reporting messages (based on files/directories)
|
55
|
+
def input_file_not_found
|
56
|
+
"File not found, please try again."
|
57
|
+
end
|
58
|
+
|
59
|
+
def invalid_input_file
|
60
|
+
"File must be a #{conjunctionator} file."
|
61
|
+
end
|
62
|
+
|
63
|
+
def mismatched_tags(status)
|
64
|
+
"Missing closing tag in #{status[:filename]} after line #{status[:line]}"
|
65
|
+
end
|
66
|
+
|
67
|
+
def success_message(filedir)
|
68
|
+
if Snippet.snippet_counter > 0
|
69
|
+
"Your snippet file has been successfully updated with #{Snippet.snippet_counter} new snips at: '#{filedir}'"
|
70
|
+
else
|
71
|
+
"No snips found in the specified files or directories."
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def snip_terminal_status(filename, line)
|
76
|
+
if line
|
77
|
+
"#{filename}:#{line} snipped"
|
78
|
+
else
|
79
|
+
nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
### ARGV command line argument-specific
|
84
|
+
|
85
|
+
# no args
|
86
|
+
def no_args_message
|
87
|
+
"Welcome to snip. Type 'snip --help' for help"
|
88
|
+
end
|
89
|
+
|
90
|
+
# filename set (-f)
|
91
|
+
def new_file_path
|
92
|
+
"Snippet file location saved as '#{File.absolute_path(ARGV[1])}/my_snips.txt'"
|
93
|
+
end
|
94
|
+
|
95
|
+
def check_for_file(filename)
|
96
|
+
if filename
|
97
|
+
"Your file is located at #{filename}"
|
98
|
+
else
|
99
|
+
"No output file found, please specify a filename after '-f'"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def need_to_specify_a_directory
|
104
|
+
"Please specify an existing directory to save your snippets to"
|
105
|
+
end
|
106
|
+
|
107
|
+
# Search (-d)
|
108
|
+
def show_snips(file)
|
109
|
+
if file
|
110
|
+
if File.readlines(file)[0]
|
111
|
+
File.readlines(file)
|
112
|
+
else
|
113
|
+
"Snippet file is empty"
|
114
|
+
end
|
115
|
+
else
|
116
|
+
"Snippet file doesn't exist. Specify where you would like to save your snippets by running 'snip -f <filepath>"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def display_error
|
121
|
+
"Specify code type (#{conjunctionator}) and/or a search string, e.g. 'snip -d js \"event delegation\"', or snip -d .' for all snips"
|
122
|
+
end
|
123
|
+
|
124
|
+
def no_results
|
125
|
+
"No matching snippet files found"
|
126
|
+
end
|
127
|
+
|
128
|
+
# reindex (-i)
|
129
|
+
def reindexed
|
130
|
+
"Your snippet files have been reindexed"
|
131
|
+
end
|
132
|
+
|
133
|
+
# log (-l)
|
134
|
+
def show_log(log_file)
|
135
|
+
if File.readlines(log_file)[0]
|
136
|
+
File.readlines(log_file)
|
137
|
+
else
|
138
|
+
"Log file is empty"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
# whitespace (-w)
|
143
|
+
def whitespace_success
|
144
|
+
"Whitespace restored"
|
145
|
+
end
|
146
|
+
|
147
|
+
# delete (--delete)
|
148
|
+
def delete_error
|
149
|
+
"Please specify which snippets you wish to delete"
|
150
|
+
end
|
151
|
+
|
152
|
+
def delete_success
|
153
|
+
"Snips #{ARGV[1]} deleted from my_snips.txt (other files must be modified manually)"
|
154
|
+
end
|
155
|
+
|
156
|
+
# clipboard (-c)
|
157
|
+
def clipboard_instructions
|
158
|
+
"Run 'snip -c' or specify code type (#{Language.languages.keys.join(", ") + " or misc"}) and a title string with:
|
159
|
+
'snip -c rb \"Using string interpolation\"' "
|
160
|
+
end
|
161
|
+
|
162
|
+
def clipboard_prompts
|
163
|
+
["Type of code (e.g. #{conjunctionator}) or press enter for misc:", "Title:"]
|
164
|
+
end
|
165
|
+
|
166
|
+
def clipboard_origin(type)
|
167
|
+
type != "misc" ? type_str = " (.#{type})" : type_str = ""
|
168
|
+
"clipboard#{type_str}"
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
def clipboard_success(code)
|
173
|
+
"Your code has been snipped:\n\n#{code}"
|
174
|
+
end
|
175
|
+
|
176
|
+
# help (--help)
|
177
|
+
def terminal_help_message
|
178
|
+
"
|
179
|
+
|
180
|
+
*** First time setup: ***
|
181
|
+
|
182
|
+
snip -f <directory>
|
183
|
+
|
184
|
+
This specifies the directory where you would like to save your snippets. 'my_snips.txt' will be automatically created if it doesn't already exist. If you move your my_snips.txt file, you will need to run 'snip -f' again.
|
185
|
+
|
186
|
+
Language-specific (e.g. .js, .rb) snippet files will be automatically created in the same directory as the .txt file.
|
187
|
+
|
188
|
+
|
189
|
+
*** To add snip tags into your code: ***
|
190
|
+
|
191
|
+
Ruby:
|
192
|
+
|
193
|
+
# <snip> Title goes here
|
194
|
+
5.times do {|x| puts x}
|
195
|
+
# </snip>
|
196
|
+
|
197
|
+
JS:
|
198
|
+
|
199
|
+
// <snip> Title goes here
|
200
|
+
var test = 1234;
|
201
|
+
// </snip>
|
202
|
+
|
203
|
+
You can alternatively use <$> and </$>. Your tags will be replaced with <*snip*> and/or <*$*> after running 'snip'.
|
204
|
+
|
205
|
+
|
206
|
+
*** Ongoing usage: ***
|
207
|
+
|
208
|
+
snip <filename> - process a single file
|
209
|
+
snip <directory> - process a directory recursively
|
210
|
+
snip -a - display all your snips in terminal
|
211
|
+
snip -c - creates new snippet from clipboard contents
|
212
|
+
snip -s <type> '<string>' - searches your snips with type #{conjunctionator} and/or search string
|
213
|
+
snip -i - reindexes your snippet files (after deleting old snips, etc)
|
214
|
+
snip -l - view log history if you need to debug previous snips
|
215
|
+
snip --delete <num> - deletes the specified snippet number(s) (comma-delimited) and reindexes
|
216
|
+
snip --help - displays this help menu
|
217
|
+
|
218
|
+
Visit https://github.com/jgerminario/snip for more information or to submit bug reports/feature requests.
|
219
|
+
|
220
|
+
"
|
221
|
+
end
|
222
|
+
end
|
data/my_snips.rb
CHANGED
@@ -1,198 +0,0 @@
|
|
1
|
-
# **** Snippet 1: Title of first snip ****
|
2
|
-
# Snipped from rspec/test_snip_files/subfolder/test3.rb:1 on 12-13-2014
|
3
|
-
desc "create the database"
|
4
|
-
task :create do
|
5
|
-
puts "Creating file #{DB_PATH} if it doesn't exist..."
|
6
|
-
touch DB_PATH
|
7
|
-
end
|
8
|
-
|
9
|
-
|
10
|
-
# **** Snippet 2: Title of this snip ****
|
11
|
-
# Snipped from rspec/test_snip_files/subfolder/test3.rb:9 on 12-13-2014
|
12
|
-
desc "drop the database"
|
13
|
-
task :drop do
|
14
|
-
puts "Deleting #{DB_PATH}..."
|
15
|
-
rm_f DB_PATH
|
16
|
-
end
|
17
|
-
|
18
|
-
|
19
|
-
# **** Snippet 3: Title of first snip ****
|
20
|
-
# Snipped from rspec/test_snip_files/subfolder/test3.rb:17 on 12-13-2014
|
21
|
-
desc "create the database"
|
22
|
-
task :create do
|
23
|
-
puts "Creating file #{DB_PATH} if it doesn't exist..."
|
24
|
-
touch DB_PATH
|
25
|
-
end
|
26
|
-
|
27
|
-
|
28
|
-
# **** Snippet 4: Title of this snip ****
|
29
|
-
# Snipped from rspec/test_snip_files/subfolder/test3.rb:25 on 12-13-2014
|
30
|
-
desc "drop the database"
|
31
|
-
task :drop do
|
32
|
-
puts "Deleting #{DB_PATH}..."
|
33
|
-
rm_f DB_PATH
|
34
|
-
end
|
35
|
-
|
36
|
-
|
37
|
-
# **** Snippet 5: Title of this snip ****
|
38
|
-
# Snipped from rspec/test_snip_files/test.rb:1 on 12-13-2014
|
39
|
-
class OrangeTree
|
40
|
-
attr_reader :age, :height
|
41
|
-
def initialize
|
42
|
-
@age = 0
|
43
|
-
@oranges = []
|
44
|
-
end
|
45
|
-
|
46
|
-
|
47
|
-
# **** Snippet 6: TITLE ****
|
48
|
-
# Snipped from rspec/test_snip_files/test.rb:10 on 12-13-2014
|
49
|
-
def age!
|
50
|
-
@oranges += Array.new(rand(1..10)) { Orange.new } if @age > 5
|
51
|
-
end
|
52
|
-
|
53
|
-
|
54
|
-
# **** Snippet 7: test ****
|
55
|
-
# Snipped from rspec/test_snip_files/test.rb:16 on 12-13-2014
|
56
|
-
def any_oranges?
|
57
|
-
!@oranges.empty?
|
58
|
-
end
|
59
|
-
|
60
|
-
|
61
|
-
# **** Snippet 8: Title ggg ****
|
62
|
-
# Snipped from rspec/test_snip_files/test2.rb:1 on 12-13-2014
|
63
|
-
def method
|
64
|
-
"code"
|
65
|
-
end
|
66
|
-
|
67
|
-
|
68
|
-
# **** Snippet 9: Title 2323 ****
|
69
|
-
# Snipped from rspec/test_snip_files/test2.rb:7 on 12-13-2014
|
70
|
-
def method
|
71
|
-
"code"
|
72
|
-
end
|
73
|
-
|
74
|
-
|
75
|
-
# **** Snippet 10: Title 4 ****
|
76
|
-
# Snipped from rspec/test_snip_files/test2.rb:13 on 12-13-2014
|
77
|
-
def method
|
78
|
-
"code"
|
79
|
-
end
|
80
|
-
|
81
|
-
|
82
|
-
# **** Snippet 11: sweet jesus ****
|
83
|
-
# Snipped from clipboard (.rb): on 12-13-2014
|
84
|
-
["line_check(line)"]
|
85
|
-
|
86
|
-
# **** Snippet 12: test ****
|
87
|
-
# Snipped from clipboard (.rb): on 12-13-2014
|
88
|
-
if File.directory?(ARGV[0])
|
89
|
-
BatchProcessing.process(ARGV[0])
|
90
|
-
elsif File.file?(ARGV[0])
|
91
|
-
if ARGV[0].end_with?(".rb") || ARGV[0].end_with?(".js") ||ARGV[0].end_with?(".erb")
|
92
|
-
CommandLineController.run(ARGV[0])
|
93
|
-
else
|
94
|
-
abort(ViewFormatter.invalid_file)
|
95
|
-
end
|
96
|
-
else
|
97
|
-
abort(ViewFormatter.file_not_found)
|
98
|
-
end
|
99
|
-
|
100
|
-
puts ViewFormatter.success_message(DestinationFileWriter.full_file_directory)
|
101
|
-
|
102
|
-
# **** Snippet 13: cool catz ****
|
103
|
-
# Snipped from clipboard (.rb): on 12-14-2014
|
104
|
-
if File.directory?(ARGV[0])
|
105
|
-
BatchProcessing.process(ARGV[0])
|
106
|
-
elsif File.file?(ARGV[0])
|
107
|
-
if ARGV[0].end_with?(".rb") || ARGV[0].end_with?(".js") ||ARGV[0].end_with?(".erb")
|
108
|
-
CommandLineController.run(ARGV[0])
|
109
|
-
else
|
110
|
-
abort(ViewFormatter.invalid_file)
|
111
|
-
end
|
112
|
-
else
|
113
|
-
abort(ViewFormatter.file_not_found)
|
114
|
-
end
|
115
|
-
|
116
|
-
puts ViewFormatter.success_message(DestinationFileWriter.full_file_directory)
|
117
|
-
|
118
|
-
# **** Snippet 14: Title of first snip ****
|
119
|
-
# Snipped from rspec/test_snip_files/subfolder/test3.rb::1 on 12-16-2014
|
120
|
-
desc "create the database"
|
121
|
-
task :create do
|
122
|
-
puts "Creating file #{DB_PATH} if it doesn't exist..."
|
123
|
-
touch DB_PATH
|
124
|
-
end
|
125
|
-
|
126
|
-
|
127
|
-
# **** Snippet 15: Title of this snip ****
|
128
|
-
# Snipped from rspec/test_snip_files/subfolder/test3.rb::9 on 12-16-2014
|
129
|
-
desc "drop the database"
|
130
|
-
task :drop do
|
131
|
-
puts "Deleting #{DB_PATH}..."
|
132
|
-
rm_f DB_PATH
|
133
|
-
end
|
134
|
-
|
135
|
-
|
136
|
-
# **** Snippet 16: Title of first snip ****
|
137
|
-
# Snipped from rspec/test_snip_files/subfolder/test3.rb::17 on 12-16-2014
|
138
|
-
desc "create the database"
|
139
|
-
task :create do
|
140
|
-
puts "Creating file #{DB_PATH} if it doesn't exist..."
|
141
|
-
touch DB_PATH
|
142
|
-
end
|
143
|
-
|
144
|
-
|
145
|
-
# **** Snippet 17: Title of this snip ****
|
146
|
-
# Snipped from rspec/test_snip_files/subfolder/test3.rb::25 on 12-16-2014
|
147
|
-
desc "drop the database"
|
148
|
-
task :drop do
|
149
|
-
puts "Deleting #{DB_PATH}..."
|
150
|
-
rm_f DB_PATH
|
151
|
-
end
|
152
|
-
|
153
|
-
|
154
|
-
# **** Snippet 18: Title of this snip ****
|
155
|
-
# Snipped from rspec/test_snip_files/test.rb::1 on 12-16-2014
|
156
|
-
class OrangeTree
|
157
|
-
attr_reader :age, :height
|
158
|
-
def initialize
|
159
|
-
@age = 0
|
160
|
-
@oranges = []
|
161
|
-
end
|
162
|
-
|
163
|
-
|
164
|
-
# **** Snippet 19: TITLE ****
|
165
|
-
# Snipped from rspec/test_snip_files/test.rb::10 on 12-16-2014
|
166
|
-
def age!
|
167
|
-
@oranges += Array.new(rand(1..10)) { Orange.new } if @age > 5
|
168
|
-
end
|
169
|
-
|
170
|
-
|
171
|
-
# **** Snippet 20: test ****
|
172
|
-
# Snipped from rspec/test_snip_files/test.rb::16 on 12-16-2014
|
173
|
-
def any_oranges?
|
174
|
-
!@oranges.empty?
|
175
|
-
end
|
176
|
-
|
177
|
-
|
178
|
-
# **** Snippet 21: Title ggg ****
|
179
|
-
# Snipped from rspec/test_snip_files/test2.rb::1 on 12-16-2014
|
180
|
-
def method
|
181
|
-
"code"
|
182
|
-
end
|
183
|
-
|
184
|
-
|
185
|
-
# **** Snippet 22: Title 2323 ****
|
186
|
-
# Snipped from rspec/test_snip_files/test2.rb::7 on 12-16-2014
|
187
|
-
def method
|
188
|
-
"code"
|
189
|
-
end
|
190
|
-
|
191
|
-
|
192
|
-
# **** Snippet 23: Title 4 ****
|
193
|
-
# Snipped from rspec/test_snip_files/test2.rb::13 on 12-16-2014
|
194
|
-
def method
|
195
|
-
"code"
|
196
|
-
end
|
197
|
-
|
198
|
-
|
data/rspec/rspec.rb
CHANGED
data/snip.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
# spec.executables = ["snip"]
|
18
18
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
-
spec.require_paths = ["lib", "db", "config", "
|
20
|
+
spec.require_paths = ["lib", "db", "config", "log"]
|
21
21
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.7"
|
23
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snipgem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jgerminario
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2015-
|
13
|
+
date: 2015-03-05 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -83,18 +83,19 @@ files:
|
|
83
83
|
- Gemfile.lock
|
84
84
|
- LICENSE.txt
|
85
85
|
- Rakefile
|
86
|
-
- app/controllers/controller.rb
|
87
|
-
- app/models/snippet.rb
|
88
|
-
- app/models/utils/batchprocessing.rb
|
89
|
-
- app/models/utils/codescanner.rb
|
90
|
-
- app/models/utils/destinationfilewriter.rb
|
91
|
-
- app/models/utils/searchfile.rb
|
92
|
-
- app/models/utils/sourcefilereader.rb
|
93
|
-
- app/views/viewformatter.rb
|
94
86
|
- bin/snip
|
95
87
|
- config/filepath.config
|
88
|
+
- lib/controllers/controller.rb
|
89
|
+
- lib/languages/languages.rb
|
90
|
+
- lib/models/snippet.rb
|
91
|
+
- lib/models/utils/batchprocessing.rb
|
92
|
+
- lib/models/utils/codescanner.rb
|
93
|
+
- lib/models/utils/destinationfilewriter.rb
|
94
|
+
- lib/models/utils/searchfile.rb
|
95
|
+
- lib/models/utils/sourcefilereader.rb
|
96
96
|
- lib/snip.rb
|
97
97
|
- lib/snip/version.rb
|
98
|
+
- lib/views/viewformatter.rb
|
98
99
|
- log/snip.log
|
99
100
|
- my_snips.rb
|
100
101
|
- rspec/rspec.rb
|
@@ -116,7 +117,6 @@ require_paths:
|
|
116
117
|
- lib
|
117
118
|
- db
|
118
119
|
- config
|
119
|
-
- app
|
120
120
|
- log
|
121
121
|
required_ruby_version: !ruby/object:Gem::Requirement
|
122
122
|
requirements:
|
@@ -1,30 +0,0 @@
|
|
1
|
-
require_relative '../models/utils/sourcefilereader'
|
2
|
-
require_relative '../models/utils/codescanner'
|
3
|
-
require_relative '../models/utils/destinationfilewriter'
|
4
|
-
require_relative '../models/utils/searchfile'
|
5
|
-
|
6
|
-
module CommandLineController
|
7
|
-
|
8
|
-
extend self
|
9
|
-
|
10
|
-
def run(file)
|
11
|
-
file_read = SourceFileReaderWriter.new(file)
|
12
|
-
to_run = file_read.convert_to_array_of_lines
|
13
|
-
CodeScanner.run(to_run, SourceFileReaderWriter.file_to_open)
|
14
|
-
file_writing
|
15
|
-
file_read.overwrite_existing_snips
|
16
|
-
end
|
17
|
-
|
18
|
-
def file_writing
|
19
|
-
DestinationFileWriter.run(Snippet.snippet_array)
|
20
|
-
DestinationFileWriter.run(Snippet.rb_snippets, "rb") if Snippet.rb_snippets.any?
|
21
|
-
DestinationFileWriter.run(Snippet.js_snippets, "js") if Snippet.js_snippets.any?
|
22
|
-
DestinationFileWriter.run(Snippet.erb_snippets, "erb") if Snippet. erb_snippets.any?
|
23
|
-
Snippet.snippet_array = []
|
24
|
-
end
|
25
|
-
|
26
|
-
def display_search(text, ext)
|
27
|
-
SearchDisplay.run(DestinationFileWriter.return_display_file, text, ext)
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
module SearchDisplay
|
2
|
-
|
3
|
-
extend self
|
4
|
-
|
5
|
-
def check_for_title(line)
|
6
|
-
line.match(/\*\*\*\* Snippet \d+:/)
|
7
|
-
end
|
8
|
-
|
9
|
-
def run(file,text="",ext="")
|
10
|
-
if !search_snips(divide_snips(file), text, ext).empty?
|
11
|
-
search_snips(divide_snips(file), text, ext)
|
12
|
-
else
|
13
|
-
ViewFormatter.no_results
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def divide_snips(file)
|
18
|
-
snip_array = []
|
19
|
-
snip_str = ""
|
20
|
-
File.open(file, "r").each do |line|
|
21
|
-
if check_for_title(line)
|
22
|
-
snip_array << snip_str
|
23
|
-
snip_str = line
|
24
|
-
else
|
25
|
-
snip_str << line
|
26
|
-
end
|
27
|
-
end
|
28
|
-
snip_array << snip_str
|
29
|
-
snip_array.shift
|
30
|
-
snip_array
|
31
|
-
end
|
32
|
-
|
33
|
-
def search_snips(array,text,ext)
|
34
|
-
array.select{ |snip| snip.include?(text) && snip.include?(ext)}
|
35
|
-
end
|
36
|
-
|
37
|
-
def delete(file, ids)
|
38
|
-
run(file).reject do |snip|
|
39
|
-
ids.include?(snip.match(/\*\*\*\* Snippet (\d+):/).captures[0])
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
data/app/views/viewformatter.rb
DELETED
@@ -1,169 +0,0 @@
|
|
1
|
-
# View component for file writing operations and possibly terminal output
|
2
|
-
require 'date'
|
3
|
-
|
4
|
-
module ViewFormatter
|
5
|
-
|
6
|
-
extend self
|
7
|
-
|
8
|
-
def snippet_indexer(index, title, type)
|
9
|
-
if type == "js"
|
10
|
-
"// **** Snippet " + (index).to_s + ": #{title} **** \n"
|
11
|
-
elsif type == "erb"
|
12
|
-
"<!-- **** Snippet " + (index).to_s + ": #{title} **** -->\n"
|
13
|
-
else
|
14
|
-
"# **** Snippet " + (index).to_s + ": #{title} **** \n"
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def file_not_found
|
19
|
-
"File not found, please try again."
|
20
|
-
end
|
21
|
-
|
22
|
-
def invalid_file
|
23
|
-
"File must be a .js, .rb or .erb file."
|
24
|
-
end
|
25
|
-
|
26
|
-
def success_message(filedir)
|
27
|
-
if Snippet.snippet_counter > 0
|
28
|
-
"Your snippet file has been successfully updated with #{Snippet.snippet_counter} new snips at: '#{filedir}'"
|
29
|
-
else
|
30
|
-
"No snips found in the specified files or directories."
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def status_line(line, type, file)
|
35
|
-
if type == "js"
|
36
|
-
"// Snipped from #{file}#{line_check(line)} on #{Time.now.strftime("%m-%d-%Y")}"
|
37
|
-
elsif type == "erb"
|
38
|
-
"<!-- Snipped from #{file}#{line_check(line)} on #{Time.now.strftime("%m-%d-%Y")}-->"
|
39
|
-
else
|
40
|
-
"# Snipped from #{file}#{line_check(line)} on #{Time.now.strftime("%m-%d-%Y")}"
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def line_check(line)
|
45
|
-
line ? ':' + line.to_s : nil
|
46
|
-
end
|
47
|
-
|
48
|
-
def snip_terminal_status(filename, line)
|
49
|
-
if line
|
50
|
-
"#{filename}:#{line} snipped"
|
51
|
-
else
|
52
|
-
nil
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def specify_path
|
57
|
-
"Please specify where you would like to save your snippets by running 'snip -f <filepath>'"
|
58
|
-
end
|
59
|
-
|
60
|
-
def output_file_not_found(file)
|
61
|
-
"Output file '#{file}' not found, please specify existing file location or desired location for new file by running 'snip -f <filepath>'"
|
62
|
-
end
|
63
|
-
|
64
|
-
def new_file(filename)
|
65
|
-
"New snippet file created at #{File.absolute_path(filename)}"
|
66
|
-
end
|
67
|
-
|
68
|
-
def terminal_help_message
|
69
|
-
"
|
70
|
-
|
71
|
-
*** First time setup: ***
|
72
|
-
|
73
|
-
snip -f <directory>
|
74
|
-
|
75
|
-
This specifies the directory where you would like to save your snippets. 'my_snips.txt' will be automatically created if it doesn't already exist. If you move your my_snips.txt file, you will need to run 'snip -f' again.
|
76
|
-
|
77
|
-
Ruby and JavaScript snippet files will be automatically created in the same directory as the .txt file.
|
78
|
-
|
79
|
-
|
80
|
-
*** To add snip tags into your code: ***
|
81
|
-
|
82
|
-
Ruby:
|
83
|
-
|
84
|
-
# <snip> Title goes here
|
85
|
-
5.times do {|x| puts x}
|
86
|
-
# </snip>
|
87
|
-
|
88
|
-
JS:
|
89
|
-
|
90
|
-
// <snip> Title goes here
|
91
|
-
var test = 1234;
|
92
|
-
// </snip>
|
93
|
-
|
94
|
-
You can alternatively use <$> and </$>. Your tags will be replaced with <*snip*> and/or <*$*> after running 'snip'.
|
95
|
-
|
96
|
-
|
97
|
-
*** Ongoing usage: ***
|
98
|
-
|
99
|
-
snip <filename> - process a single file
|
100
|
-
snip <directory> - process a directory recursively
|
101
|
-
snip -c - creates new snippet from clipboard contents
|
102
|
-
snip -d . - display all your snips in terminal
|
103
|
-
snip -d <type> '<string>' - searches your snips by type (js, rb, erb) or search string
|
104
|
-
snip -i - reindexes your snippet files (after deleting old snips, etc)
|
105
|
-
snip -l - view log history if you need to debug previous snips
|
106
|
-
snip --delete <num> - deletes the specified snippet number(s) (comma-delimited) and reindexes
|
107
|
-
snip --help - displays this help menu
|
108
|
-
|
109
|
-
Visit https://github.com/jgerminario/snip for more information or to submit bug reports/feature requests.
|
110
|
-
|
111
|
-
"
|
112
|
-
end
|
113
|
-
|
114
|
-
def new_file_path
|
115
|
-
"Snippet file location saved as '#{File.absolute_path(ARGV[1])}/my_snips.txt'"
|
116
|
-
end
|
117
|
-
|
118
|
-
def no_args_message
|
119
|
-
"Welcome to snip. Type 'snip --help' for help"
|
120
|
-
end
|
121
|
-
|
122
|
-
def specify_filename(filename)
|
123
|
-
if filename
|
124
|
-
"Your file is located at #{filename}"
|
125
|
-
else
|
126
|
-
"No output file found, please specify a filename after '-f'"
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def need_to_specify_directory
|
131
|
-
"Please specify an existing directory to save your snippets to"
|
132
|
-
end
|
133
|
-
|
134
|
-
def show_log(log_file)
|
135
|
-
if File.readlines(log_file)[0]
|
136
|
-
File.readlines(log_file)
|
137
|
-
else
|
138
|
-
"Log file is empty"
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def show_snips(file)
|
143
|
-
if file
|
144
|
-
if File.readlines(file)[0]
|
145
|
-
File.readlines(file)
|
146
|
-
else
|
147
|
-
"Snippet file is empty"
|
148
|
-
end
|
149
|
-
else
|
150
|
-
"Snippet file doesn't exist. Specify where you would like to save your snippets by running 'snip -f <filepath>"
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
def reindexed
|
155
|
-
"Your snippet files have been reindexed"
|
156
|
-
end
|
157
|
-
|
158
|
-
def clipboard_command
|
159
|
-
"Run 'snip -c' or specify code type (js, rb, erb, misc) and a title string with:
|
160
|
-
'snip -c rb \"Using string interpolation\"' "
|
161
|
-
end
|
162
|
-
|
163
|
-
def display_error
|
164
|
-
"Specify code type (js, rb, erb) and/or a search string, e.g. 'snip -d js \"event delegation\"', or snip -d .' for all snips"
|
165
|
-
end
|
166
|
-
def no_results
|
167
|
-
"No matching snippet files found"
|
168
|
-
end
|
169
|
-
end
|