geek_painter 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  == Geek Painter
2
2
 
3
- A very simple DSL to generate a colored shell prompt to use in *nix OS
3
+ A very simple DSL to generate colored strings to use in *nix-like OS's terminals
4
4
 
5
5
  == Installation
6
6
 
@@ -8,28 +8,90 @@ A very simple DSL to generate a colored shell prompt to use in *nix OS
8
8
 
9
9
  == Usage
10
10
 
11
- #put the following lines in a file (ie. geek_painter_client.rb or whatever you want)
12
- painting = GeekPainter.paint do
13
- trace "[".in :brown
14
- trace "\\u".in :red
15
- trace ":".in :yellow
16
- trace "\\h".in :red
17
- trace "] ".in :magenta
18
- trace "\\w ".in :default
19
- trace "\$(__git_ps1)".in :yellow
20
- trace " $ ".reset
11
+ === Generate a valid string to be used as a prompt string (PS1)
12
+
13
+ Check this link to see the available variables: http://www.ibm.com/developerworks/linux/library/l-tip-prompt/
14
+
15
+ Put the following lines in a file (ie. ~/.my_scripts/my_prompt.rb or whatever you want)
16
+
17
+ master_piece = GeekPainter.paint do
18
+ label "[".in(:brown).on :blue
19
+ label "\\u".in :red
20
+ label ":".in :yellow
21
+ label "\\h".in :red
22
+ label "] ".in(:magenta).on :red
23
+ label "\\W ".in :default
24
+ label "\$(__git_ps1)".in :yellow <= This might not work if you don't have git installed
21
25
  end
22
26
 
23
- #it shows the actual string ready to be copied and pasted in ~/.bashrc or ~/.bash_profile files like this => PS1='the_string'
24
- # just do: $ruby geek_painter_client.rb
25
- painting.print_raw
27
+ puts master_piece.prompt
28
+
29
+ Now open your ~/.bash_profile, ~/.bashrc or ~/.profile and put this on it
30
+
31
+ PS1=`ruby ~/.my_scripts/my_prompt.rb`
32
+
33
+ Open a console and you'll see your new shiny and colorful prompt.
34
+
35
+ If you just want to copy and paste the string run the same script like this in a Ruby terminal (irb):
36
+
37
+ puts master_pieces.raw_prompt
38
+
39
+ Copy the resulting string and paste it in your PS1.
40
+
41
+ === Generate a valid string to be used as colored text in the terminal
42
+
43
+ my_text = GeekPainter.paint do
44
+ label "Hello, ".in(:red).on(:blue)
45
+ label "World.".in(:yellow).on(:cyan)
46
+ end
26
47
 
27
- #the GeekPainter#show method is meant to be used when geek painter is used like this:
28
- #
29
- # in ~/.bashrc
30
- # PS1='$(ruby geek_painter_client.rb)'
31
- painting.show
48
+ puts my_text.raw_text
32
49
 
50
+ In this case "raw_text" will return the following string:
51
+
52
+ \e[1;31;44mHello, \e[1;33;46mWorld.\e[0m
53
+
54
+ Copy it and...
55
+
56
+ $ printf "\e[1;31;44mHello, \e[1;33;46mWorld.\e[0m"
57
+
58
+ To print the colored text in the Interactive Ruby console (IRb) do the following
59
+
60
+ puts my_text.text # it will print the colored text itself not the escaped string
61
+
62
+ == Available colors
63
+
64
+ === Foreground (These colors can be used as a parameter in the "in" method)
65
+
66
+ :black
67
+ :dark_gray
68
+ :light_gray
69
+ :light_red
70
+ :red
71
+ :light_green
72
+ :green
73
+ :yellow
74
+ :brown
75
+ :light_blue
76
+ :blue
77
+ :light_magenta
78
+ :magenta
79
+ :light_cyan
80
+ :cyan
81
+ :white
82
+ :default
83
+
84
+ === Background (These colors can be used as a parameter in the "on" method)
85
+
86
+ :black
87
+ :red
88
+ :green
89
+ :brown
90
+ :blue
91
+ :magenta
92
+ :cyan
93
+ :white
94
+ :default
33
95
 
34
96
  == Note on Patches/Pull Requests
35
97
 
@@ -41,6 +103,10 @@ A very simple DSL to generate a colored shell prompt to use in *nix OS
41
103
  (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
42
104
  * Send me a pull request. Bonus points for topic branches.
43
105
 
106
+ == TODO
107
+
108
+ * generate string to be used like this => echo -e "the_string", right now the generated string shows extra characters if used this way.
109
+
44
110
  == Copyright
45
111
 
46
112
  Copyright (c) 2010 Rafael Magaña. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/geek_painter.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{geek_painter}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Rafael Magana"]
12
- s.date = %q{2010-04-03}
12
+ s.date = %q{2010-07-22}
13
13
  s.description = %q{A very simple DSL to generate a colored shell prompt string to use in *nix OS terminals. it might be used to generate a valid string to be used as a prompt or to generated strings that represent colored text on a terminal.}
14
14
  s.email = %q{raf.magana@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -27,16 +27,15 @@ Gem::Specification.new do |s|
27
27
  "geek_painter_client.rb",
28
28
  "lib/geek_painter.rb",
29
29
  "lib/geek_painter/canvas.rb",
30
+ "lib/geek_painter/core-ext.rb",
30
31
  "lib/geek_painter/escaping_tools.rb",
31
- "lib/geek_painter/palette.rb",
32
- "lib/geek_painter/string.rb",
33
32
  "test/helper.rb",
34
33
  "test/test_geek_painter.rb"
35
34
  ]
36
35
  s.homepage = %q{http://github.com/rafmagana/geek_painter}
37
36
  s.rdoc_options = ["--charset=UTF-8"]
38
37
  s.require_paths = ["lib"]
39
- s.rubygems_version = %q{1.3.6}
38
+ s.rubygems_version = %q{1.3.7}
40
39
  s.summary = %q{A very simple DSL to generate a colored shell prompt string to use in *nix OS terminals}
41
40
  s.test_files = [
42
41
  "test/helper.rb",
@@ -47,7 +46,7 @@ Gem::Specification.new do |s|
47
46
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
47
  s.specification_version = 3
49
48
 
50
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
50
  s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
52
51
  else
53
52
  s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
@@ -1,19 +1,15 @@
1
1
  require "rubygems"
2
2
  require "geek_painter"
3
3
 
4
+
4
5
  painting = GeekPainter.paint do
5
- trace "[".in :brown
6
- trace "\\u".in :red
7
- trace ":".in :yellow
8
- trace "\\h".in :red
9
- trace "] ".in :magenta
10
- trace "\\w ".in :default
11
- trace "\$(__git_ps1)".in :yellow
12
- trace " $ ".in :default
6
+ label "[".in :brown
7
+ label "\\u".in :red
8
+ label ":".in :yellow
9
+ label "\\h".in :red
10
+ label "] ".in :magenta
11
+ label "\\w ".in :default
12
+ label "\$(__git_ps1)".in :blue
13
13
  end
14
14
 
15
- painting.print_raw
16
- # >>
17
- # >> \[\033[0;33m\][\[\033[0;31m\]\u\[\033[1;33m\]:\[\033[0;31m\]\h\[\033[0;35m\]] \[\033[0m\]\w \[\033[1;33m\]$(__git_ps1)\[\033[0m\] $ \[\033[0m\]
18
- # >>
19
- # >> copy the string above and put it in your ~/.bashrc or ~/.bash_profile like this => PS1='the_string'
15
+ puts painting.prompt
data/lib/geek_painter.rb CHANGED
@@ -1,12 +1,10 @@
1
1
  require 'geek_painter/escaping_tools'
2
- require 'geek_painter/string'
3
- require 'geek_painter/palette'
4
2
  require 'geek_painter/canvas'
5
3
 
6
4
  module GeekPainter
7
5
  def self.paint(&block)
8
- @canvas = Canvas.new
9
- @canvas.instance_eval(&block)
10
- @canvas
6
+ canvas = Canvas.new
7
+ canvas.instance_eval(&block)
8
+ canvas
11
9
  end
12
10
  end
@@ -1,43 +1,68 @@
1
+ require 'geek_painter/core-ext'
2
+
1
3
  module GeekPainter
2
4
  #
3
- # The main DSL, every single call to the "trace" method pushes the parameters to an array
5
+ # The main DSL, every single call to the "label" method pushes the parameters to an array
4
6
  # and "raw" method then joins the array to generate the actual string to be used in PS1 or wherever.
5
7
  #
6
8
  # ie. canvas = Canvas.new
7
- # canvas.trace "\\[\\033[33m\\]user:" => yellow text
8
- # canvas.trace "\\[\\033[34m\\]host" => blue text
9
+ # canvas.label "\\[\\033[33m\\]user:" => yellow text
10
+ # canvas.label "\\[\\033[34m\\]host" => blue text
9
11
  # canvas.show => \\[\\033[33m\\]user:\\[\\033[34m\\]host => user:host
10
12
 
11
13
  class Canvas
12
14
  include EscapingTools
13
-
15
+
16
+ attr_reader :labels
17
+
14
18
  def initialize
15
- @traces = []
19
+ @labels = []
20
+ end
21
+
22
+ def label(string_color_pair)
23
+ @labels << string_color_pair
24
+ end
25
+ #for backwards compatibility with <= 0.1.1
26
+ alias :trace :label
27
+
28
+ def prompt
29
+ render :for_prompt
30
+ end
31
+
32
+ def text
33
+ render :for_text
16
34
  end
17
35
 
18
- def trace(string)
19
- add_trace(string)
20
- end
21
-
22
- def raw
23
- @traces << escape_char("0")
24
- @traces.join("")
25
- end
26
-
27
- def print_raw
28
- puts ""
29
- puts raw
30
- puts ""
31
- puts "copy the string above and put it in your ~/.bashrc or ~/.bash_profile like this => PS1='the_string'"
32
- end
33
-
34
- def show
35
- puts raw
36
- end
37
-
38
- protected
39
- def add_trace(trace)
40
- @traces << trace
41
- end
36
+ def for_prompt
37
+ prompt.to_raw
38
+ end
39
+
40
+ def for_text
41
+ text.to_raw
42
+ end
43
+
44
+ def reset_color
45
+ @labels << ["", :default]
46
+ end
47
+
48
+ private
49
+ def render(format=:for_text)
50
+ colored_labels = []
51
+ reset_color unless @labels.last[1] == :default if @labels.last
52
+ @labels.each do |t|
53
+ unless t[2]
54
+ background_color = :default
55
+ else
56
+ background_color = t[2]
57
+ end
58
+
59
+ if t.is_a? Array
60
+ colored_labels << escape(t[0].to_s, t[1].to_fg_code, background_color.to_bg_code, format)
61
+ else
62
+ colored_labels << t
63
+ end
64
+ end
65
+ colored_labels.join ""
66
+ end
42
67
  end
43
68
  end
@@ -0,0 +1,72 @@
1
+ module GeekPainter
2
+ #
3
+ # Extends String to help the DSL to be more readable
4
+ # adds an "in" method to specify the color of the text
5
+ # ie. "my_text".in :red
6
+ #
7
+ module RubyCoreExtensions
8
+
9
+ module String
10
+ def in(color_name)
11
+ [self, color_name]
12
+ end
13
+
14
+ def to_raw
15
+ self.gsub("\e", "\\e")
16
+ end
17
+ end
18
+
19
+ module Symbol
20
+ def to_fg_code
21
+ case self
22
+ when :black then '0;30'
23
+ when :dark_gray then '1;30'
24
+ when :light_gray then '1;37'
25
+ when :light_red then '0;31'
26
+ when :red then '1;31'
27
+ when :light_green then '0;32'
28
+ when :green then '1;32'
29
+ when :yellow then '1;33'
30
+ when :brown then '0;33'
31
+ when :light_blue then '0;34'
32
+ when :blue then '1;34'
33
+ when :light_magenta then '0;35'
34
+ when :magenta then '1;35'
35
+ when :light_cyan then '0;36'
36
+ when :cyan then '1;36'
37
+ when :white then '1;37'
38
+ when :default then '0'
39
+ else '0'
40
+ end
41
+ end
42
+
43
+ def to_bg_code
44
+ case self
45
+ when :black then '40'
46
+ when :red then '41'
47
+ when :green then '42'
48
+ when :brown then '43'
49
+ when :blue then '44'
50
+ when :magenta then '45'
51
+ when :cyan then '46'
52
+ when :white then '47'
53
+ when :default then '0'
54
+ else '0'
55
+ end
56
+
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ String.class_eval do
63
+ include GeekPainter::RubyCoreExtensions::String
64
+ end
65
+
66
+ Symbol.class_eval do
67
+ include GeekPainter::RubyCoreExtensions::Symbol
68
+ end
69
+
70
+ Array.class_eval do
71
+ alias :on <<
72
+ end
@@ -1,15 +1,43 @@
1
1
  module GeekPainter
2
+
3
+ #TODO: Refactor!!!!!
2
4
  module EscapingTools
3
- def open_esc_char
4
- "\\[\\033["
5
+
6
+ def open_escape
7
+ '\['
5
8
  end
6
-
7
- def close_esc_char
8
- "m\\]"
9
+
10
+ #con dos funciona para prompt, pero no para text
11
+ #con una funciona para text, pero no para prompt
12
+ def start_color
13
+ "\033["
9
14
  end
10
-
11
- def escape_char(char)
12
- open_esc_char + char + close_esc_char
15
+
16
+ def end_color
17
+ 'm'
13
18
  end
19
+
20
+ def end_escape
21
+ '\]'
22
+ end
23
+
24
+ def escape(string, foreground_color_code, background_color_code, format)
25
+ color_delimiter = ";"
26
+ escaped_string = ''
27
+
28
+ if background_color_code == '0'
29
+ color_delimiter = ""
30
+ background_color_code = ""
31
+ end
32
+
33
+ if format == :for_prompt
34
+ escaped_string = open_escape + start_color + foreground_color_code + color_delimiter + background_color_code + end_color + end_escape + string
35
+ elsif format == :for_text
36
+ escaped_string = start_color + foreground_color_code + color_delimiter + background_color_code + end_color + string
37
+ end
38
+
39
+ escaped_string
40
+ end
41
+
14
42
  end
15
43
  end
@@ -1,7 +1,10 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestGeekPainter < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
4
+
5
+ context "Raf" do
6
+ should "write tests for his geek_painter gem" do
7
+ flunk "\n\n\nComing soon! ^_^\n\n\n"
8
+ end
6
9
  end
7
10
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geek_painter
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 23
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Rafael Magana
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-04-03 00:00:00 -06:00
18
+ date: 2010-07-22 00:00:00 -05:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: thoughtbot-shoulda
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 3
27
30
  segments:
28
31
  - 0
29
32
  version: "0"
@@ -49,9 +52,8 @@ files:
49
52
  - geek_painter_client.rb
50
53
  - lib/geek_painter.rb
51
54
  - lib/geek_painter/canvas.rb
55
+ - lib/geek_painter/core-ext.rb
52
56
  - lib/geek_painter/escaping_tools.rb
53
- - lib/geek_painter/palette.rb
54
- - lib/geek_painter/string.rb
55
57
  - test/helper.rb
56
58
  - test/test_geek_painter.rb
57
59
  has_rdoc: true
@@ -64,23 +66,27 @@ rdoc_options:
64
66
  require_paths:
65
67
  - lib
66
68
  required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
67
70
  requirements:
68
71
  - - ">="
69
72
  - !ruby/object:Gem::Version
73
+ hash: 3
70
74
  segments:
71
75
  - 0
72
76
  version: "0"
73
77
  required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
74
79
  requirements:
75
80
  - - ">="
76
81
  - !ruby/object:Gem::Version
82
+ hash: 3
77
83
  segments:
78
84
  - 0
79
85
  version: "0"
80
86
  requirements: []
81
87
 
82
88
  rubyforge_project:
83
- rubygems_version: 1.3.6
89
+ rubygems_version: 1.3.7
84
90
  signing_key:
85
91
  specification_version: 3
86
92
  summary: A very simple DSL to generate a colored shell prompt string to use in *nix OS terminals
@@ -1,34 +0,0 @@
1
- module GeekPainter
2
- #
3
- # The palette of colors
4
- #
5
- class Palette
6
- extend EscapingTools
7
- def self.grab_escaped(color_name)
8
- case color_name
9
- when :black then escape "0;30"
10
- when :dark_gray then escape "1;30"
11
- when :light_gray then escape "1;37"
12
- when :red then escape "0;31"
13
- when :light_red then escape "1;31"
14
- when :green then escape "0;32"
15
- when :light_green then escape "1;32"
16
- when :yellow then escape "1;33"
17
- when :brown then escape "0;33"
18
- when :blue then escape "0;34"
19
- when :light_blue then escape "1;34"
20
- when :magenta then escape "0;35"
21
- when :light_magenta then escape "1;35"
22
- when :cyan then escape "0;36"
23
- when :light_cyan then escape "1;36;5"
24
- when :white then escape "1;37"
25
- when :default then escape "0"
26
- end
27
- end
28
-
29
- private
30
- def self.escape(color_code)
31
- self.escape_char(color_code.to_s)
32
- end
33
- end
34
- end
@@ -1,22 +0,0 @@
1
- #
2
- # Extend String to help the DSL to be more readable
3
- # adds a "in" method to specify the color of the text
4
- # ie. "my_text".in :red
5
- #
6
- module GeekPainter
7
- module RubyCoreExtensions
8
- module String
9
- def in(color_name)
10
- Palette.grab_escaped(color_name) + self.to_s
11
- end
12
-
13
- def reset
14
- self + escape_char("0")
15
- end
16
- end
17
- end
18
- end
19
-
20
- String.class_eval do
21
- include GeekPainter::RubyCoreExtensions::String
22
- end