cheatorious 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,20 @@
1
+ #v0.3.0 (Jan/18 2012 22:28 -0200 by Luis Cipriani)
2
+
3
+ Changes:
4
+
5
+ - Removed bundler runtime dependency
6
+ - Simplified gemspec
7
+
8
+ Bugfixes:
9
+
10
+ - Added yaml require to cli (duh)
11
+ - Fixed lots of tiny bugs
12
+
13
+ Features:
14
+
15
+ - Colorful and better output for Text writer
16
+ - New commands to CLI: edit, reload and remove cheatsheets
17
+ - Added new Writer: Tree (using hirb)
1
18
 
2
19
  #v0.2.1 (Dec/29 2011 10:40 -0200 by Luis Cipriani)
3
20
 
@@ -51,4 +68,4 @@
51
68
  - simple text ouptut
52
69
 
53
70
  ----
54
- _this cheatsheet is generated by [step-up](https://github.com/kawamanza/step-up)_
71
+ _this cheatsheet is generated by [step-up](https://github.com/kawamanza/step-up)_
data/LICENSE CHANGED
@@ -1,8 +1,8 @@
1
- Copyright (c) 2011 Luis Cipriani
1
+ Copyright (c) 2011-2012 Luis Cipriani
2
2
 
3
3
  The MIT License (MIT)
4
4
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
5
 
6
6
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
7
 
8
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # cheatorious
2
2
 
3
- _"Being victorious through a means so amazing it cannot possibly be achieved without cheating."_
3
+ _"Being victorious through a means so amazing it cannot possibly be achieved without cheating."_
4
4
  (Urban Dictionary)
5
5
 
6
6
  [![Build Status](https://secure.travis-ci.org/lfcipriani/cheatorious.png)](http://travis-ci.org/lfcipriani/cheatorious)
@@ -12,7 +12,7 @@ Cheatsheets are very good to start learning or keep knowledge of some language o
12
12
  ## Install ##
13
13
 
14
14
  gem install cheatorious
15
-
15
+
16
16
  Cheatorious is compatible with ruby 1.8.x, 1.9.x, rubinius, jruby, ree and more.
17
17
 
18
18
  ## Creating your Cheatsheet ##
@@ -29,7 +29,7 @@ Create a file and use the following syntax, shown in the example below:
29
29
  key :control, "^"
30
30
  key :esc , "<esc>"
31
31
  key :leader , ","
32
-
32
+
33
33
  # your prefered keyboard keys separators (default is empty string)
34
34
  key_separator " "
35
35
 
@@ -62,16 +62,18 @@ Create a file and use the following syntax, shown in the example below:
62
62
 
63
63
  ## Using cheatorious ##
64
64
 
65
- The CLI usage goes as follows:
65
+ The basic CLI usage goes as follows:
66
66
 
67
67
  $ cheatorious # get help
68
68
  $ cheatorious help [TASK] # get help for a specific task
69
69
  $ cheatorious import FILE # import a cheatsheet description FILE (example above)
70
70
  $ cheatorious list # lists the available cheatsheets
71
71
  $ cheatorious view CHEATSHEET [OPTIONS] # view a CHEATSHEET
72
-
72
+
73
+ ### Searching ###
74
+
73
75
  To search on your cheatsheets:
74
-
76
+
75
77
  $ cheatorious help search
76
78
  Usage:
77
79
  cheatorious search CHEATSHEET [KEYWORD] [OPTIONS]
@@ -86,6 +88,8 @@ To search on your cheatsheets:
86
88
  The CHEATSHEET variable could be a name (for imported cheatsheets) or a file that describes a cheatsheet.
87
89
  Omit KEYWORD to view the full cheatsheet.
88
90
 
91
+ ### Alias ###
92
+
89
93
  If you are tired to type everything above to do a simple search, use the alias command:
90
94
 
91
95
  $ bin/cheatorious help alias
@@ -99,6 +103,12 @@ If you are tired to type everything above to do a simple search, use the alias c
99
103
 
100
104
  It will just **show** the alias command, it's up to you to decide where to put it.
101
105
 
106
+ ### Editing, reloading, removing ###
107
+
108
+ $ cheatorious edit CHEATSHEET # edit an existent CHEATSHEET. Will open it in the default terminal editor, use CHEATORIOUS_EDITOR environment variable to use another.
109
+ $ cheatorious reload CHEATSHEET # reload a CHEATSHEET after editing it with 'edit' command
110
+ $ cheatorious remove CHEATSHEET # remove a CHEATSHEET. The original file is kept for later recovering
111
+
102
112
  ## Writers ##
103
113
 
104
114
  Cheatorious has a default Text writer to give a fairly nice output for your cheatsheet. Since we can have multiple writers avaiable, you can list and set a default writer through the command *writers*:
@@ -116,7 +126,6 @@ I'll work to provide more options of output as soon as possible (markdown, color
116
126
 
117
127
  ## Tips to improve the experience! ##
118
128
 
119
- * In Macs, cheatorious is very powerful if used with [DTerm](http://decimus.net/DTerm)
120
129
  * Create aliases to save typings in the cheatsheets you use more
121
130
  * Sync your cheatsheets with Dropbox by creating a symlink for the `~/.cheatorious` folder
122
131
 
@@ -139,4 +148,4 @@ You can also contribute in the Core or Writers, feel free to:
139
148
  * Pull request
140
149
 
141
150
  ----
142
- _Copyright (c) 2011 Luis Cipriani (under MIT license)_
151
+ _Copyright (c) 2011-2012 Luis Cipriani (under MIT license)_
data/bin/cheatorious CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # encoding: UTF-8
2
3
  require File.expand_path(File.dirname(__FILE__) + '/../lib/cheatorious')
3
4
 
4
5
  Cheatorious::CLI.start
data/cheatorious.gemspec CHANGED
@@ -1,12 +1,8 @@
1
1
  # encoding: UTF-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $:.unshift lib unless $:.include?(lib)
2
+ lib = File.expand_path('../lib' , __FILE__)
3
+ $:.unshift(lib) unless $:.include?(lib)
4
4
 
5
- version_file = File.expand_path "../GEM_VERSION", __FILE__
6
- File.delete version_file if File.exists? version_file
7
-
8
- require 'step-up'
9
- require 'cheatorious'
5
+ require 'cheatorious/version'
10
6
 
11
7
  Gem::Specification.new do |s|
12
8
  s.name = "cheatorious"
@@ -24,15 +20,17 @@ Gem::Specification.new do |s|
24
20
  Rakefile
25
21
  ]
26
22
  tests = `git ls-files -- {script,test}/*`.split("\n")
27
- s.files = `git ls-files`.split("\n") - excepts - tests + %w[GEM_VERSION]
23
+ s.files = `git ls-files`.split("\n") - excepts - tests
28
24
 
29
25
  s.author = "Luis Cipriani"
30
26
  s.email = "lfcipriani@gmail.com"
31
27
  s.homepage = "https://github.com/abril/cheatorious"
32
28
 
33
29
  s.add_runtime_dependency('thor', '>= 0.14.6')
30
+ s.add_runtime_dependency('rainbow', '>= 1.1.3')
31
+ s.add_runtime_dependency('hirb', '>= 0.6.0')
32
+ s.add_runtime_dependency('hirb-unicode', '>= 0.0.5')
34
33
 
35
- # s.add_development_dependency('cover_me')
36
- # s.add_development_dependency('ruby-debug19')
37
34
  s.add_development_dependency('step-up')
35
+ s.add_development_dependency('rake')
38
36
  end
@@ -1,18 +1,21 @@
1
1
  module Cheatorious
2
- module Writer
2
+ module Writer
3
3
  # This is an example of a Writer
4
4
  #
5
- # To implement your own Writer, just create a class inside
6
- # Cheatorious::Writer module and implement the interface
5
+ # To implement your own Writer, just create a class inside
6
+ # Cheatorious::Writer module and implement the interface
7
7
  # represented by the public methods below.
8
8
  class WriterSample
9
9
  def initialize
10
10
  @section_stack = []
11
11
  @result = ""
12
+ @query = nil
13
+ @options = {}
14
+ @color = :yellow
15
+ @add_empty_line = true
12
16
  end
13
-
17
+
14
18
  def header(name, author = "", version = "", description = "")
15
- line
16
19
  line "-" * 80
17
20
  line "#{name} (#{version})"
18
21
  line
@@ -21,49 +24,75 @@ module Cheatorious
21
24
  line "-" * 80
22
25
  line
23
26
  end
24
-
27
+
25
28
  def search_header(query, results_count, options)
26
- search_type = options.keys.join(", ")
29
+ @query = query
30
+ @options = options
31
+ search_type = (options.keys - ["writer"]).join(", ")
27
32
  search_type += " " if search_type.size > 0
28
- line
29
- line "Your #{search_type}search for '#{query}' returned #{results_count} #{results_count > 1 ? "results" : "result"}:" if results_count != 0
30
- line "Your #{search_type}search for '#{query}' doesn't returned any result. Try with another keyword." if results_count == 0
33
+ line "Your #{search_type}search for '#{query.dup.foreground(@color)}' returned #{results_count} #{results_count > 1 ? "results" : "result"}:" if results_count != 0
34
+ line "Your #{search_type}search for '#{query.dup.foreground(@color)}' doesn't returned any result. Try with another keyword." if results_count == 0
31
35
  line
32
36
  end
33
-
37
+
34
38
  def footer
35
39
  line
36
40
  line "-" * 80
37
41
  line "generated by Cheatorious (https://github.com/lfcipriani/cheatorious)"
38
42
  end
39
-
43
+
40
44
  def section_start(section)
41
45
  @section_stack.push(section)
42
- line indentation("-") + " #{section}"
46
+ section = paint(section,@query) if @query && @options['section']
47
+ line
48
+ line indentation(" ") + "#".foreground(:red) + " #{section} "+ "#".foreground(:red)
43
49
  end
44
-
50
+
45
51
  def section_end
46
52
  @section_stack.pop
53
+ line
47
54
  end
48
-
55
+
49
56
  def entry(name, *values)
50
- e = "#{indentation(" ")} - #{name}: "
51
- e << values.join(", ")
57
+ value_text = values.join(", ")
58
+ if @options['reverse']
59
+ value_text = paint(value_text,@query) if @query
60
+ elsif !@options['section']
61
+ name = paint(name,@query) if @query
62
+ end
63
+ e = "#{indentation(" ")} #{name} " + "=> ".foreground(:blue)
64
+ e << value_text
52
65
  line e
53
66
  end
54
-
67
+
55
68
  def result
56
69
  @result
57
70
  end
58
-
71
+
59
72
  private
60
-
73
+
61
74
  def line(str = "")
62
- @result += str + "\n"
75
+ if str.empty?
76
+ if @add_empty_line
77
+ @result += str + "\n"
78
+ @add_empty_line = false
79
+ end
80
+ else
81
+ @result += str + "\n"
82
+ @add_empty_line = true
83
+ end
63
84
  end
64
-
85
+
65
86
  def indentation(char)
66
- char * 2 * @section_stack.size
87
+ char * 3 * @section_stack.size
88
+ end
89
+
90
+ def paint(string, query)
91
+ string.gsub(regex_for(query)) {|q| q.foreground(@color)}
92
+ end
93
+
94
+ def regex_for(query)
95
+ Regexp.new(Regexp.escape(query), (@options['sensitive'] ? 0 : Regexp::IGNORECASE))
67
96
  end
68
97
  end
69
98
  end
data/lib/cheatorious.rb CHANGED
@@ -3,13 +3,18 @@ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__))
3
3
 
4
4
  # Dependencies
5
5
  require "rubygems"
6
- require "bundler/setup"
6
+ require "rainbow"
7
7
 
8
8
  # Adding Dir.home method if it's not available
9
+ # This method is the adapted from hirb rubygem
9
10
  unless Dir.respond_to?(:home)
10
11
  class Dir
11
12
  def self.home
12
- File.expand_path(File.join("~"))
13
+ ['HOME', 'USERPROFILE'].each {|e| return ENV[e] if ENV[e] }
14
+ return "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}" if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
15
+ File.expand_path("~")
16
+ rescue
17
+ File::ALT_SEPARATOR ? "C:/" : "/"
13
18
  end
14
19
  end
15
20
  end
@@ -1,4 +1,6 @@
1
+ # encoding: UTF-8
1
2
  require 'thor'
3
+ require 'yaml'
2
4
 
3
5
  module Cheatorious
4
6
  module DslExecutor
@@ -6,22 +8,22 @@ module Cheatorious
6
8
  Cheatorious::CheatSheet.compile(name, @output, &block)
7
9
  end
8
10
  end
9
-
11
+
10
12
  class CLI < Thor
11
13
  include Thor::Actions
12
-
14
+
13
15
  desc "list", "lists the available cheatsheets. See 'import' command."
14
16
  def list
15
17
  ensure_workspace_exist
16
18
  puts (cheatsheet_list.empty? ? "You don't have imported cheatsheets. See 'import' command." : "You have #{cheatsheet_list.size} cheatsheet(s):")
17
19
  puts cheatsheet_list.join("\n")
18
20
  end
19
-
20
- desc "import FILE", "import a cheatsheet description FILE.\nCheck https://github.com/lfcipriani/cheatorious to learn how to create your own cheatsheets."
21
+
22
+ desc "import FILE", "import a cheatsheet description FILE.\nCheck https://github.com/lfcipriani/cheatorious to learn how to create your own cheatsheets.\nCheck https://github.com/lfcipriani/cheatorious-cheatsheets for cheatsheets created by the community."
21
23
  def import(file)
22
24
  ensure_workspace_exist
23
25
  name = File.basename(file, ".rb")
24
- return if cheatsheet_list.include?(name) &&
26
+ return if cheatsheet_list.include?(name) &&
25
27
  !yes?("Do you want to override the existent #{name} cheatsheet? (y/n)")
26
28
  if File.exist?(file)
27
29
  source = File.read(file)
@@ -33,13 +35,13 @@ module Cheatorious
33
35
  puts "The specified file doesn't exist: #{file}"
34
36
  end
35
37
  end
36
-
38
+
37
39
  desc "view CHEATSHEET [OPTIONS]", "view a CHEATSHEET.\nThe CHEATSHEET variable could be a name (for imported cheatsheets) or a file that describes a cheatsheet."
38
40
  method_option :writer, :aliases => "-w", :type => :string, :desc => "writer to use for the output. If not set, uses the default."
39
41
  def view(cheatsheet)
40
42
  invoke :search
41
43
  end
42
-
44
+
43
45
  desc "search CHEATSHEET [KEYWORD] [OPTIONS]", "search for KEYWORD in CHEATSHEET entries only.\nThe CHEATSHEET variable could be a name (for imported cheatsheets) or a file that describes a cheatsheet.\nOmit KEYWORD to view the full cheatsheet."
44
46
  method_option :section, :aliases => "-s", :type => :boolean, :desc => "matches KEYWORD only on section names, returning all entries and sections inside it."
45
47
  method_option :reverse, :aliases => "-r", :type => :boolean, :desc => "reverse means to search only the values of a cheatsheet (and not entries, as usual). For example, search by shortcuts."
@@ -81,7 +83,7 @@ module Cheatorious
81
83
  puts "\nUse -d option to set a default writer."
82
84
  end
83
85
  end
84
-
86
+
85
87
  desc "alias NAME CHEATSHEET", "return a shell alias command with NAME for easy access to searching a CHEATSHEET.\nThe CHEATSHEET variable must be an imported cheatsheet.\nExample: cheatorious alias svim simple_vim >> ~/.bashrc\n next time just use: svim KEYWORD [OPTIONS]"
86
88
  def alias(name, cheatsheet)
87
89
  ensure_workspace_exist
@@ -91,7 +93,42 @@ module Cheatorious
91
93
  puts "Invalid cheatsheet name: #{cheatsheet}"
92
94
  end
93
95
  end
94
-
96
+
97
+ desc "edit CHEATSHEET", "edit an existent CHEATSHEET. Will open it in the default terminal editor, use CHEATORIOUS_EDITOR environment variable to use another."
98
+ def edit(cheatsheet)
99
+ ensure_workspace_exist
100
+ if cheatsheet_list.include?(cheatsheet)
101
+ editor = %w( CHEATORIOUS_EDITOR BUNDLER_EDITOR VISUAL EDITOR ).map { |var| ENV[var] }.compact.first || 'vi'
102
+ exec editor + " " + File.join(workspace_path, "originals", cheatsheet + ".rb")
103
+ else
104
+ puts "Invalid cheatsheet name: #{cheatsheet}. Use 'list' command to check the name."
105
+ end
106
+ end
107
+
108
+ desc "reload CHEATSHEET", "reload a CHEATSHEET after editing it with 'edit' command"
109
+ def reload(cheatsheet)
110
+ ensure_workspace_exist
111
+ if cheatsheet_list.include?(cheatsheet)
112
+ source = File.read(File.join(workspace_path, "originals", cheatsheet + ".rb"))
113
+ bytes = DslExecutor.module_eval("@output = :bytes\n"+source)
114
+ File.open(File.join(workspace_path, "compiled", cheatsheet), 'w') {|f| f.write(bytes) }
115
+ puts "Cheatsheet reloaded successfuly! Try 'cheatorious view #{cheatsheet}'"
116
+ else
117
+ puts "Invalid cheatsheet name: #{cheatsheet}. Use 'list' command to check the name."
118
+ end
119
+ end
120
+
121
+ desc "remove CHEATSHEET", "remove a CHEATSHEET. The original file is kept for later recovering"
122
+ def remove(cheatsheet)
123
+ ensure_workspace_exist
124
+ if cheatsheet_list.include?(cheatsheet)
125
+ FileUtils.rm(File.join(workspace_path, "compiled", cheatsheet))
126
+ puts "Cheatsheet removed successfuly! To recover it execute 'import' command for " + File.join(workspace_path, "originals", cheatsheet + ".rb")
127
+ else
128
+ puts "Invalid cheatsheet name: #{cheatsheet}. Use 'list' command to check the name."
129
+ end
130
+ end
131
+
95
132
  private
96
133
 
97
134
  def cheatsheet_list
@@ -105,7 +142,7 @@ module Cheatorious
105
142
  def default_writer
106
143
  writer_for(YAML.load(File.open(File.join(workspace_path, "config")))["default_writer"])
107
144
  end
108
-
145
+
109
146
  def ensure_workspace_exist
110
147
  unless File.directory?(workspace_path)
111
148
  Dir.mkdir(workspace_path)
@@ -122,9 +159,9 @@ module Cheatorious
122
159
  FileUtils.cp(File.expand_path(File.join(__FILE__, "..", "..", "..", "examples", "simple_vim.rb")), File.join(workspace_path, "originals"))
123
160
  end
124
161
  end
125
-
162
+
126
163
  def workspace_path
127
164
  @workspace ||= File.join(Dir.home, ".cheatorious")
128
165
  end
129
166
  end
130
- end
167
+ end
@@ -1,19 +1,19 @@
1
1
  module Cheatorious
2
2
  class Utils
3
3
  require "base64"
4
-
4
+
5
5
  def self.serialize(object, to = nil)
6
6
  data = Marshal.dump(object)
7
7
  data = Base64.encode64(data) if to == :base64
8
8
  return data
9
9
  end
10
-
10
+
11
11
  def self.deserialize(data, from = nil)
12
12
  data = Base64.decode64(data) if from == :base64
13
13
  object = Marshal.load(data)
14
14
  return object
15
15
  end
16
-
16
+
17
17
  def self.serialization_type(data)
18
18
  /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$/ =~ data ? :base64 : :bytes
19
19
  end
@@ -1,19 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module Cheatorious
3
- module VERSION #:nodoc:
4
- version = nil
5
- version_file = ::File.expand_path('../../../GEM_VERSION', __FILE__)
6
- version = File.read(version_file) if ::File.exists?(version_file)
7
- if version.nil? && ::File.exists?(::File.expand_path('../../../.git', __FILE__))
8
- version = ::StepUp::Driver::Git.new.last_version_tag("HEAD", true) rescue "v0.0.0+0"
9
- ::File.open(version_file, "w"){ |f| f.write version }
10
- end
11
-
12
- STRING = version.gsub(/^v([^\+]+)\+?\d*$/, '\1')
13
- MAJOR, MINOR, PATCH, TINY = STRING.scan(/\d+/)
14
-
15
- def self.to_s
16
- STRING
17
- end
18
- end
3
+ VERSION = "0.3.0"
19
4
  end
@@ -1,5 +1,6 @@
1
1
  module Cheatorious
2
- module Writer
2
+ module Writer
3
3
  autoload :Text, "cheatorious/writer/text"
4
+ autoload :Tree, "cheatorious/writer/tree"
4
5
  end
5
6
  end
@@ -1,13 +1,18 @@
1
+ # encoding: UTF-8
2
+
1
3
  module Cheatorious
2
- module Writer
4
+ module Writer
3
5
  class Text
4
6
  def initialize
5
7
  @section_stack = []
6
8
  @result = ""
9
+ @query = nil
10
+ @options = {}
11
+ @color = :yellow
12
+ @add_empty_line = true
7
13
  end
8
-
14
+
9
15
  def header(name, author = "", version = "", description = "")
10
- line
11
16
  line "-" * 80
12
17
  line "#{name} (#{version})"
13
18
  line
@@ -16,49 +21,75 @@ module Cheatorious
16
21
  line "-" * 80
17
22
  line
18
23
  end
19
-
24
+
20
25
  def search_header(query, results_count, options)
21
- search_type = options.keys.join(", ")
26
+ @query = query
27
+ @options = options
28
+ search_type = (options.keys - ["writer"]).join(", ")
22
29
  search_type += " " if search_type.size > 0
23
- line
24
- line "Your #{search_type}search for '#{query}' returned #{results_count} #{results_count > 1 ? "results" : "result"}:" if results_count != 0
25
- line "Your #{search_type}search for '#{query}' doesn't returned any result. Try with another keyword." if results_count == 0
30
+ line "Your #{search_type}search for '#{query.dup.foreground(@color)}' returned #{results_count} #{results_count > 1 ? "results" : "result"}:" if results_count != 0
31
+ line "Your #{search_type}search for '#{query.dup.foreground(@color)}' doesn't returned any result. Try with another keyword." if results_count == 0
26
32
  line
27
33
  end
28
-
34
+
29
35
  def footer
30
36
  line
31
37
  line "-" * 80
32
38
  line "generated by Cheatorious (https://github.com/lfcipriani/cheatorious)"
33
39
  end
34
-
40
+
35
41
  def section_start(section)
36
42
  @section_stack.push(section)
37
- line indentation("-") + " #{section}"
43
+ section = paint(section,@query) if @query && @options['section']
44
+ line
45
+ line indentation(" ") + "#".foreground(:red) + " #{section} "+ "#".foreground(:red)
38
46
  end
39
-
47
+
40
48
  def section_end
41
49
  @section_stack.pop
50
+ line
42
51
  end
43
-
52
+
44
53
  def entry(name, *values)
45
- e = "#{indentation(" ")} - #{name}: "
46
- e << values.join(", ")
54
+ value_text = values.join(", ")
55
+ if @options['reverse']
56
+ value_text = paint(value_text,@query) if @query
57
+ elsif !@options['section']
58
+ name = paint(name,@query) if @query
59
+ end
60
+ e = "#{indentation(" ")} #{name} " + "=> ".foreground(:blue)
61
+ e << value_text
47
62
  line e
48
63
  end
49
-
64
+
50
65
  def result
51
66
  @result
52
67
  end
53
-
68
+
54
69
  private
55
-
70
+
56
71
  def line(str = "")
57
- @result += str + "\n"
72
+ if str.empty?
73
+ if @add_empty_line
74
+ @result += str + "\n"
75
+ @add_empty_line = false
76
+ end
77
+ else
78
+ @result += str + "\n"
79
+ @add_empty_line = true
80
+ end
58
81
  end
59
-
82
+
60
83
  def indentation(char)
61
- char * 2 * @section_stack.size
84
+ char * 3 * @section_stack.size
85
+ end
86
+
87
+ def paint(string, query)
88
+ string.gsub(regex_for(query)) {|q| q.foreground(@color)}
89
+ end
90
+
91
+ def regex_for(query)
92
+ Regexp.new(Regexp.escape(query), (@options['sensitive'] ? 0 : Regexp::IGNORECASE))
62
93
  end
63
94
  end
64
95
  end
@@ -0,0 +1,105 @@
1
+ # encoding: UTF-8
2
+ require "hirb"
3
+
4
+ module Cheatorious
5
+ module Writer
6
+ class Tree
7
+ def initialize
8
+ @section_stack = []
9
+ @result = ""
10
+ @header_footer = []
11
+ @tree = []
12
+ @query = nil
13
+ @options = {}
14
+ @color = :yellow
15
+ @add_empty_line = true
16
+ end
17
+
18
+ def header(name, author = "", version = "", description = "")
19
+ line "-" * 80
20
+ line "#{name} (#{version})"
21
+ line
22
+ line "Author : #{author[0]} (#{author[1]})"
23
+ line "Description: #{description}"
24
+ line "-" * 80
25
+ line
26
+ @header_footer << @result
27
+ @add_empty_line = true
28
+ @result = ""
29
+ end
30
+
31
+ def search_header(query, results_count, options)
32
+ @query = query
33
+ @options = options
34
+ search_type = (options.keys - ["writer"]).join(", ")
35
+ search_type += " " if search_type.size > 0
36
+ line "Your #{search_type}search for '#{query.dup.foreground(@color)}' returned #{results_count} #{results_count > 1 ? "results" : "result"}:" if results_count != 0
37
+ line "Your #{search_type}search for '#{query.dup.foreground(@color)}' doesn't returned any result. Try with another keyword." if results_count == 0
38
+ line
39
+ @header_footer << @result
40
+ @add_empty_line = true
41
+ @result = ""
42
+ end
43
+
44
+ def footer
45
+ line
46
+ line "-" * 80
47
+ line "generated by Cheatorious (https://github.com/lfcipriani/cheatorious)"
48
+ @header_footer << @result
49
+ @result = nil
50
+ end
51
+
52
+ def section_start(section)
53
+ section = paint(section,@query) if @query && @options['section']
54
+ @tree << [@section_stack.size, "#".foreground(:red) + " " + section]
55
+ @section_stack.push(section)
56
+ end
57
+
58
+ def section_end
59
+ @section_stack.pop
60
+ end
61
+
62
+ def entry(name, *values)
63
+ value_text = values.join(", ")
64
+ if @options['reverse']
65
+ value_text = paint(value_text,@query) if @query
66
+ elsif !@options['section']
67
+ name = paint(name,@query) if @query
68
+ end
69
+ indent = @section_stack.size == 0 ? 0 : @section_stack.size+1
70
+ @tree << [indent, "=>".foreground(:blue) + " " + name]
71
+ value_text.split(", ").each do |v|
72
+ @tree << [indent+1, v]
73
+ end
74
+ end
75
+
76
+ def result
77
+ @result = @header_footer[0]
78
+ @result += Hirb::Helpers::Tree.render(@tree, :type => :directory, :multi_line_nodes => true)
79
+ @result += @header_footer[1] || ""
80
+ end
81
+
82
+ private
83
+
84
+ def line(str = "")
85
+ if str.empty?
86
+ if @add_empty_line
87
+ @result += str + "\n"
88
+ @add_empty_line = false
89
+ end
90
+ else
91
+ @result += str + "\n"
92
+ @add_empty_line = true
93
+ end
94
+ end
95
+
96
+ def paint(string, query)
97
+ string.gsub(regex_for(query)) {|q| q.foreground(@color)}
98
+ end
99
+
100
+ def regex_for(query)
101
+ Regexp.new(Regexp.escape(query), (@options['sensitive'] ? 0 : Regexp::IGNORECASE))
102
+ end
103
+ end
104
+ end
105
+ end
metadata CHANGED
@@ -1,45 +1,123 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: cheatorious
3
- version: !ruby/object:Gem::Version
4
- version: 0.2.1
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Luis Cipriani
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2011-12-29 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-01-19 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: thor
16
- requirement: &2160967780 !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
17
24
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 43
29
+ segments:
30
+ - 0
31
+ - 14
32
+ - 6
21
33
  version: 0.14.6
22
34
  type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rainbow
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 21
45
+ segments:
46
+ - 1
47
+ - 1
48
+ - 3
49
+ version: 1.1.3
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: hirb
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 7
61
+ segments:
62
+ - 0
63
+ - 6
64
+ - 0
65
+ version: 0.6.0
66
+ type: :runtime
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: hirb-unicode
23
70
  prerelease: false
24
- version_requirements: *2160967780
25
- - !ruby/object:Gem::Dependency
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 21
77
+ segments:
78
+ - 0
79
+ - 0
80
+ - 5
81
+ version: 0.0.5
82
+ type: :runtime
83
+ version_requirements: *id004
84
+ - !ruby/object:Gem::Dependency
26
85
  name: step-up
27
- requirement: &2160965500 !ruby/object:Gem::Requirement
86
+ prerelease: false
87
+ requirement: &id005 !ruby/object:Gem::Requirement
28
88
  none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
33
96
  type: :development
97
+ version_requirements: *id005
98
+ - !ruby/object:Gem::Dependency
99
+ name: rake
34
100
  prerelease: false
35
- version_requirements: *2160965500
101
+ requirement: &id006 !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ type: :development
111
+ version_requirements: *id006
36
112
  description:
37
113
  email: lfcipriani@gmail.com
38
- executables:
114
+ executables:
39
115
  - cheatorious
40
116
  extensions: []
117
+
41
118
  extra_rdoc_files: []
42
- files:
119
+
120
+ files:
43
121
  - .travis.yml
44
122
  - CHANGELOG.md
45
123
  - LICENSE
@@ -58,32 +136,39 @@ files:
58
136
  - lib/cheatorious/version.rb
59
137
  - lib/cheatorious/writer.rb
60
138
  - lib/cheatorious/writer/text.rb
61
- - GEM_VERSION
139
+ - lib/cheatorious/writer/tree.rb
62
140
  homepage: https://github.com/abril/cheatorious
63
141
  licenses: []
142
+
64
143
  post_install_message:
65
144
  rdoc_options: []
66
- require_paths:
145
+
146
+ require_paths:
67
147
  - lib
68
- required_ruby_version: !ruby/object:Gem::Requirement
148
+ required_ruby_version: !ruby/object:Gem::Requirement
69
149
  none: false
70
- requirements:
71
- - - ! '>='
72
- - !ruby/object:Gem::Version
73
- version: '0'
74
- segments:
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ hash: 3
154
+ segments:
75
155
  - 0
76
- hash: -297487151997060724
77
- required_rubygems_version: !ruby/object:Gem::Requirement
156
+ version: "0"
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
158
  none: false
79
- requirements:
80
- - - ! '>='
81
- - !ruby/object:Gem::Version
82
- version: '0'
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ hash: 3
163
+ segments:
164
+ - 0
165
+ version: "0"
83
166
  requirements: []
167
+
84
168
  rubyforge_project:
85
169
  rubygems_version: 1.8.10
86
170
  signing_key:
87
171
  specification_version: 3
88
172
  summary: generator of simple, searchable, command line cheatsheets
89
173
  test_files: []
174
+
data/GEM_VERSION DELETED
@@ -1 +0,0 @@
1
- v0.2.1+1