snipper 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/bin/snipper CHANGED
@@ -1,32 +1,65 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
+ require 'rubygems'
3
4
  require 'snipper'
5
+ require 'thor'
4
6
 
5
- @options = {:command => "new"}
7
+ class MyCLI<Thor
8
+ desc "add [FILE1..5]", "add a snippet"
9
+ long_desc <<-D
10
+ There are various ways to add a new snippet:
6
11
 
7
- OptionParser.new do |opts|
8
- opts.on("--lang LANGUAGE", "-l", "Snippet Language") do |v|
9
- @options[:syntax] = v
12
+ To add a number of PHP files to a single snippet:
13
+
14
+ $ snipper add file1 file2 file3 file4 file5 --lang=php
15
+
16
+ To create a new snippet with a file from STDIN:
17
+
18
+ $ cat files1 | snipper add --lang=php
19
+
20
+ There is also a shortcut for supplying files from STDIN:
21
+
22
+ $ cat file1 | snipper --lang=php
23
+ D
24
+ option :syntax, :desc => "Language to use for the new snippet", :default => "ruby", :type => :string, :aliases => ["-l", "--lang", "-s"]
25
+ def add(file1=nil, file2=nil, file3=nil, file4=nil, file5=nil)
26
+ puts Snipper::Util.new([file1, file2, file3, file4, file5].compact, options[:syntax])
27
+ end
28
+
29
+ desc "append [SNIPPET] [FILE1..5]", "appends a file to an existing snippet"
30
+ long_desc <<-D
31
+ Appending works similar to adding except you must known an existing snippet number:
32
+
33
+ $ snipper append 123 file1 file2 file3 file4 file5 --lang=php
34
+
35
+ $ cat file1 | snipper append 123 --lang=php
36
+ D
37
+ option :syntax, :desc => "Language to use for the new snippet", :default => "ruby", :type => :string, :aliases => ["-l", "--lang", "-s"]
38
+ def append(snippet, file1=nil, file2=nil, file3=nil, file4=nil, file5=nil)
39
+ puts Snipper::Util.append(snippet, [file1, file2, file3, file4, file5].compact, options[:syntax])
10
40
  end
11
41
 
12
- opts.on("-L","List Languages") do
13
- @options[:list] = true
42
+ desc "delete [SNIPPET]", "deletes an existing snippet"
43
+ def delete(snippet)
44
+ Snipper::Util.delete(snippet)
14
45
  end
15
- end.parse!
16
-
17
- exec "pygmentize -L lexer" if @options[:list]
18
-
19
- @options[:command] = ARGV[0] if ARGV.size >= 1
20
-
21
- case @options[:command]
22
- when "search", "s"
23
- Snipper::Command::Search.new(@options)
24
- when "delete", "d", "rm"
25
- Snipper::Command::Delete.new(@options)
26
- when "view", "v"
27
- Snipper::Command::View.new(@options)
28
- when "edit", "e"
29
- puts Snipper::Command::Edit.new(@options).url
30
- else
31
- puts Snipper::Command::New.new(@options).url
46
+
47
+ desc "edit [SNIPPET]", "edits an existing snippet"
48
+ def edit(snippet)
49
+ puts Snipper::Util.edit(snippet)
50
+ end
51
+
52
+ desc "view [SNIPPET]", "views an existing snippet"
53
+ def view(snippet)
54
+ puts Snipper::Util.view(snippet)
55
+ end
56
+
57
+ desc "languages", "list known languages"
58
+ def languages
59
+ Snipper::Util.list_langs
60
+ end
61
+
62
+ default_task :add
32
63
  end
64
+
65
+ MyCLI.start(ARGV)
data/lib/snipper.rb CHANGED
@@ -3,12 +3,8 @@ require 'pygments.rb'
3
3
  require 'optparse'
4
4
  require 'etc'
5
5
  require 'snipper/config'
6
+ require 'snipper/util'
6
7
  require 'snipper/snippet'
7
- require 'snipper/command/new'
8
- require 'snipper/command/edit'
9
- require 'snipper/command/delete'
10
- require 'snipper/command/search'
11
- require 'snipper/command/view'
12
8
  require 'snipper/version'
13
9
  require 'snipper/output/pygments_output.rb'
14
10
  require 'yaml'
@@ -27,9 +27,9 @@ class Snipper
27
27
  html.puts ".linenos { text-align: right; color: white; width: 30px; }"
28
28
 
29
29
  if Config[:dark_theme]
30
- html.puts ".highlight { background-color: #0C1000 }"
30
+ html.puts ".highlight { background-color: #0C1000; color: white }"
31
31
  else
32
- html.puts ".highlight { background-color: white }"
32
+ html.puts ".highlight { background-color: white; color: black }"
33
33
  end
34
34
 
35
35
  html.puts "</style>"
@@ -0,0 +1,92 @@
1
+ class Snipper
2
+ class Util
3
+ def self.delete(snippet)
4
+ snipper = Snipper.new
5
+
6
+ raise "Please specify a snippet number" unless snippet =~ /^\d+$/
7
+
8
+ snip = Snippet.new(nil, :snippet => snippet)
9
+
10
+ snip.delete!
11
+ end
12
+
13
+ def self.new(snippet, syntax)
14
+ snipper = Snipper.new
15
+
16
+ if STDIN.tty?
17
+ if snippet.empty?
18
+ if ARGV.empty?
19
+ abort "When adding a snippet without providing any arguments a snippet has to be provided on STDIN, run 'snipper help add' for full details"
20
+ else
21
+ abort "Please specify a filename or provide a snippet on STDIN"
22
+ end
23
+ end
24
+
25
+ txt = snippet
26
+ else
27
+ txt = STDIN.read
28
+ end
29
+
30
+ snip = Snippet.new(txt, {:syntax => syntax})
31
+ snip.url_for_snippet
32
+ end
33
+
34
+ def self.append(snippet_id, snippet, syntax)
35
+ snipper = Snipper.new
36
+
37
+ if STDIN.tty?
38
+ abort "Please specify a filename or provide a snippet on STDIN" if snippet.empty?
39
+
40
+ txt = snippet
41
+ else
42
+ txt = STDIN.read
43
+ end
44
+
45
+ snip = Snippet.new(txt, {:syntax => syntax, :snippet => snippet_id})
46
+ snip.url_for_snippet
47
+ end
48
+
49
+ def self.edit(snippet)
50
+ snipper = Snipper.new
51
+
52
+ raise "Please specify a snippet number" unless snippet
53
+
54
+ snip = Snippet.new(nil, :snippet => snippet)
55
+
56
+ files = snip.files.map {|file| File.join(snip.snippet_path, file)}
57
+
58
+ editor = ENV["EDITOR"] || "vi"
59
+
60
+ system "%s %s" % [editor, files.join(" ")]
61
+
62
+ files.each do |file|
63
+ unless File.size?(file)
64
+ puts "Removing #{file} from the snippet"
65
+ File.unlink(file)
66
+ end
67
+ end
68
+
69
+ snip.update_html
70
+
71
+ snip.url_for_snippet
72
+ end
73
+
74
+ def self.view(snippet)
75
+ snipper = Snipper.new
76
+
77
+ snip = Snippet.new(nil, :snippet => snippet)
78
+
79
+ files = snip.files.map {|file| File.join(snip.snippet_path, file)}
80
+
81
+ pager = ENV["PAGER"] || "less"
82
+
83
+ system "cat %s|%s" % [files.join(" "), pager]
84
+
85
+ snip.url_for_snippet
86
+ end
87
+
88
+ def self.list_langs
89
+ exec "pygmentize -L lexer"
90
+ end
91
+ end
92
+ end
@@ -1,3 +1,3 @@
1
1
  class Snipper
2
- VERSION="0.0.2"
2
+ VERSION="0.0.3"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snipper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - R.I.Pienaar
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-28 00:00:00 +01:00
18
+ date: 2012-05-03 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -60,6 +60,20 @@ dependencies:
60
60
  version: "0"
61
61
  type: :runtime
62
62
  version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: thor
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :runtime
76
+ version_requirements: *id004
63
77
  description: A Unix CLI centric snippet manager that produces static files
64
78
  email: rip@devco.net
65
79
  executables:
@@ -74,11 +88,7 @@ files:
74
88
  - lib/snipper/config.rb
75
89
  - lib/snipper/output/pygments_output.rb
76
90
  - lib/snipper/version.rb
77
- - lib/snipper/command/delete.rb
78
- - lib/snipper/command/edit.rb
79
- - lib/snipper/command/new.rb
80
- - lib/snipper/command/search.rb
81
- - lib/snipper/command/view.rb
91
+ - lib/snipper/util.rb
82
92
  - lib/snipper.rb
83
93
  has_rdoc: true
84
94
  homepage: http://devco.net/
@@ -1,21 +0,0 @@
1
- class Snipper
2
- class Command
3
- class Delete
4
- attr_reader :url, :snippet
5
-
6
- def initialize(options)
7
- @options = options
8
-
9
- snipper = Snipper.new
10
-
11
- raise "Please specify a snippet number" unless ARGV[1] =~ /^\d+$/
12
-
13
- options[:snippet] = ARGV[1]
14
-
15
- snippet = Snippet.new(nil, @options)
16
-
17
- snippet.delete!
18
- end
19
- end
20
- end
21
- end
@@ -1,35 +0,0 @@
1
- class Snipper
2
- class Command
3
- class Edit
4
- attr_reader :url, :snippet
5
-
6
- def initialize(options)
7
- @options = options
8
-
9
- snipper = Snipper.new
10
-
11
- raise "Please specify a snippet number" unless ARGV[1] =~ /^\d+$/
12
-
13
- options[:snippet] = ARGV[1]
14
-
15
- snippet = Snippet.new(nil, @options)
16
- @url = snippet.url_for_snippet
17
-
18
- files = snippet.files.map {|file| File.join(snippet.snippet_path, file)}
19
-
20
- editor = ENV["EDITOR"] || "vi"
21
-
22
- system "%s %s" % [editor, files.join(" ")]
23
-
24
- files.each do |file|
25
- unless File.size?(file)
26
- puts "Removing #{file} from the snippet"
27
- File.unlink(file)
28
- end
29
- end
30
-
31
- snippet.update_html
32
- end
33
- end
34
- end
35
- end
@@ -1,30 +0,0 @@
1
- class Snipper
2
- class Command
3
- class New
4
- attr_reader :url, :snippet
5
-
6
- def initialize(options)
7
- @options = options
8
-
9
- @snipper = Snipper.new
10
-
11
- # if the first arg is a number and it isnt a file path
12
- # then assume this is a snippet id we want to append to
13
- if ARGV.first =~ /^\d+$/ && !File.exist?(ARGV.first)
14
- options[:snippet] = ARGV.shift
15
- end
16
-
17
- if STDIN.tty?
18
- abort "Please specify a filename or provide one on STDIN" if ARGV.empty?
19
-
20
- txt = ARGV
21
- else
22
- txt = STDIN.read
23
- end
24
-
25
- @snippet = Snippet.new(txt, @options)
26
- @url = @snippet.url_for_snippet
27
- end
28
- end
29
- end
30
- end
@@ -1,21 +0,0 @@
1
- class Snipper
2
- class Command
3
- class Search
4
- attr_reader :url, :snippet
5
-
6
- def initialize(options)
7
- @options = options
8
-
9
- snipper = Snipper.new
10
-
11
- raise "Please specify a search string" unless ARGV[1]
12
-
13
- Dir.chdir(Config[:snippets_dir]) do
14
- grep = Config[:grep].gsub("%Q%", ARGV[1].gsub("'", "\\'"))
15
-
16
- system grep
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,25 +0,0 @@
1
- class Snipper
2
- class Command
3
- class View
4
- attr_reader :url, :snippet
5
-
6
- def initialize(options)
7
- @options = options
8
-
9
- snipper = Snipper.new
10
-
11
- raise "Please specify a snippet number" unless ARGV[1] =~ /^\d+$/
12
-
13
- options[:snippet] = ARGV[1]
14
-
15
- snippet = Snippet.new(nil, @options)
16
-
17
- files = snippet.files.map {|file| File.join(snippet.snippet_path, file)}
18
-
19
- pager = ENV["PAGER"] || "less"
20
-
21
- system "cat %s|%s" % [files.join(" "), pager]
22
- end
23
- end
24
- end
25
- end