pry-theme 0.0.10 → 0.1.0

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.
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  *~
2
2
  *.gem
3
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - ree
6
+ - rbx-18mode
7
+ - rbx-19mode
8
+ - jruby
9
+
10
+ branches:
11
+ only:
12
+ - master
data/CHANGELOG.md CHANGED
@@ -1,6 +1,45 @@
1
1
  Pry Theme changelog
2
2
  ===================
3
3
 
4
+ ### v0.1.0 (July 23, 2012)
5
+
6
+ * **HOT**: Boosted the load speed of the plugin:
7
+
8
+ Before:
9
+ ![Before](http://img-fotki.yandex.ru/get/6405/98991937.b/0_7f768_24d92170_orig)
10
+
11
+ % time pry -e 'exit'
12
+ 0.75s user 0.06s system 94% cpu 0.847 total
13
+
14
+ After:
15
+ ![After](http://img-fotki.yandex.ru/get/6400/98991937.b/0_7f767_7c1ad4e9_orig)
16
+
17
+ % time pry -e 'exit'
18
+ 0.69s user 0.04s system 94% cpu 0.773 total
19
+
20
+ * **WARM**: added `--edit` command (`-e`). `-e` brings new experience in
21
+ creating themes. With this command you can create and edit themes, both. For
22
+ example, you can create a new theme like this: `pry-theme -e +my-theme`. Check
23
+ out [a wiki entry][cli] for more information;
24
+
25
+ * **TEPID**: improved `--color` command (`-c`). In this version `-c` can
26
+ understand HEX, RGB, ANSI and human-readable colors. Interested in how to use
27
+ it? Check out [Pry Theme CLI][cli] article in wiki;
28
+
29
+ * Added [Pry Theme Cheatsheet][cheatsheet] to Pry Theme Wiki;
30
+ * Fixed output for `--all-colors`, `--test`, `--list` and `--remote--list`. It
31
+ was hard-coded to `$stdout`, so therefore, when it wasn't `$stdout`,
32
+ information would be printed in the wrong place;
33
+ * Fixed duplicate colors 86:aquamarine03 and 122:aquamarine03;
34
+ * Fixed a bug when a user doesn't specify `Pry.config.theme` and tries to run
35
+ `pry-theme -l`, which results in a TypeError;
36
+ * Added two new 8-color default themes for a users with limited terminals:
37
+ "vim-default" and "vim-detailed";
38
+ * Changed output for `--test`: it's more terse and concise now;
39
+ * Changed output for `--list`, because the older one looked really silly;
40
+ * Removed some spurious attributes for `.prytheme` files;
41
+ * Fixed the name of theme "saturday".
42
+
4
43
  ### v0.0.10 (July 07, 2012)
5
44
 
6
45
  * Themes can use any color from 256-color palette as background color. Before
@@ -96,3 +135,6 @@ Last but not least:
96
135
  ### v0.0.1.pre (June 26, 2012)
97
136
 
98
137
  * Initial release.
138
+
139
+ [cli]: https://github.com/kyrylo/pry-theme/wiki/Pry-Theme-CLI
140
+ [cheatsheet]: https://github.com/kyrylo/pry-theme/wiki/Pry-Theme-Cheatsheet
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/README.md CHANGED
@@ -107,7 +107,9 @@ Credits
107
107
  * Thanks to [banister][johndogg] for bringing the plugin in masses and
108
108
  contributing a bunch of themes;
109
109
  * Thanks to Karandashev for "Puzzle" font;
110
- * Thanks to Creatica for "Dited" font.
110
+ * Thanks to Creatica for "Dited" font;
111
+ * Thanks to [noprompt][noprompt] for a HEX to ANSI conversion Ruby
112
+ implementation, which I borrowed from one of his projects.
111
113
 
112
114
  License
113
115
  -------
@@ -122,3 +124,4 @@ The project uses Zlib License. See LICENSE file for more information.
122
124
  [wiki]: https://github.com/kyrylo/pry-theme/wiki
123
125
  [ptc]: https://github.com/kyrylo/pry-theme-collection
124
126
  [johndogg]: https://github.com/banister/ "John Dogg"
127
+ [noprompt]: https://github.com/noprompt/ "Joel Holdbrooks"
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ desc "Run tests"
2
+ task :test do
3
+ sh "bacon -Itest --automatic --quiet"
4
+ end
5
+
6
+ task :default => :test
@@ -0,0 +1,55 @@
1
+ module PryTheme
2
+ module ColorConverter
3
+
4
+ extend self
5
+
6
+ COLORS = Palette.new(256).colors
7
+
8
+ def rgb_to_hex(rgb)
9
+ "#%02x%02x%02x" % rgb.split(",")
10
+ end
11
+
12
+ def rgb_to_ansi(rgb)
13
+ hex_to_ansi(rgb_to_hex(rgb))
14
+ end
15
+
16
+ def hex_to_rgb(hex)
17
+ if m = hex.match(/\A#?([A-F\d]{2})([A-F\d]{2})([A-F\d]{2})\z/i)
18
+ m.captures.map(&:hex)
19
+ end
20
+ end
21
+
22
+ def hex_to_ansi(hex)
23
+ rgb = hex_to_rgb(hex)
24
+
25
+ unless Helper.rgb?(rgb.join(","))
26
+ return
27
+ end
28
+
29
+ if color = RGB.table.index(rgb)
30
+ return COLORS.find { |c| c.term == color }.term
31
+ end
32
+
33
+ increments = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
34
+ rgb.map! do |part|
35
+ for i in (0..4)
36
+ lower, upper = increments[i], increments[i+1]
37
+
38
+ next unless part.between?(lower, upper)
39
+
40
+ distance_from_lower = (lower - part).abs
41
+ distance_from_upper = (upper - part).abs
42
+ closest = distance_from_lower < distance_from_upper ? lower : upper
43
+ end
44
+ closest
45
+ end
46
+
47
+ COLORS.find { |c| c.term == RGB.table.index(rgb) }.term
48
+ end
49
+
50
+ def ansi_to_hex(ansi)
51
+ "#" + RGB.table[ansi].map { |v| (v.to_s(16) * 2)[0...2] }.join
52
+ end
53
+
54
+ end
55
+ end
@@ -1,7 +1,3 @@
1
- require 'net/https'
2
- require 'base64'
3
- require 'json'
4
-
5
1
  module PryTheme
6
2
  Commands = Pry::CommandSet.new do
7
3
 
@@ -34,6 +30,7 @@ module PryTheme
34
30
  opt.on :a, "all-colors", "Show all available 8/256 colors."
35
31
  opt.on :c, "color", "Show information about a specific color (256)."
36
32
  opt.on :t, "test", "Test your current theme", :argument => false
33
+ opt.on :e, "edit", "Edit or create a theme"
37
34
  opt.on :l, "list", "Show a list of installed themes", :argument => false
38
35
  opt.on :r, "remote-list", "Show a list of themes from Pry Theme Collection", :argument => false
39
36
  opt.on :i, "install", "Install a theme from Pry Theme Collection"
@@ -46,6 +43,8 @@ module PryTheme
46
43
  show_specific_color
47
44
  elsif opts.t?
48
45
  test_theme
46
+ elsif opts.e?
47
+ edit_theme
49
48
  elsif opts.l?
50
49
  show_list
51
50
  elsif opts.r?
@@ -71,7 +70,7 @@ module PryTheme
71
70
  end
72
71
 
73
72
  def show_palette_colors
74
- lputs in_columns(Palette.new(args[0]).to_a)
73
+ lputs in_columns(Palette.new(args[0]).to_a), output
75
74
  end
76
75
 
77
76
  def in_columns(list, cols=3)
@@ -79,7 +78,7 @@ module PryTheme
79
78
 
80
79
  list.each_slice(cols) do |slice|
81
80
  slice.each do |c|
82
- color = c.scan(/(\e\[[[0-9];]+m)(\w+)(\e\[0m)(:)(\e\[[[0-9];]+m)(\w+)(\e\[0m)/).flatten
81
+ color = c.scan(/(\e\[[\d;]+m)(\w+)(\e\[0m)(:)(\e\[[\d;]+m)(\w+)(\e\[0m)/).flatten
83
82
  color[1] = color[1].ljust(3)
84
83
  color[-2] = color[-2].ljust(22)
85
84
  color_table << color
@@ -90,54 +89,155 @@ module PryTheme
90
89
  end
91
90
 
92
91
  def show_specific_color
93
- unless args[0] =~ /\A(\d{1,3})\z/ && (0...256).include?($1.to_i)
94
- raise NoColorError, "Invalid color number: #{ args[0] }"
95
- end
92
+ c = args[0]
96
93
 
97
- pal = Palette.new(256)
98
- color = pal.colors.detect { |c| c.term == args[0].to_i }
94
+ unless c
95
+ output.puts "This option requires an argument. Specify a color. Examples:"
96
+ output.puts "Pry Theme: black; RGB: 0,0,0; HEX: #000000, ANSI: 0."
97
+ return
98
+ end
99
99
 
100
- if color
101
- output.puts color.to_term(pal.notation)
100
+ color = if ansi?(c)
101
+ find_color(c)
102
+ elsif hex?(c)
103
+ find_color(ColorConverter.hex_to_ansi(c))
104
+ elsif rgb?(c)
105
+ find_color(ColorConverter.rgb_to_ansi(c))
106
+ else
107
+ h = ColorConverter::COLORS.select do |color|
108
+ (color.human).to_s[c]
109
+ end
110
+
111
+ # Return human-readable colors if the
112
+ # select selected any. If not, return nil.
113
+ h if h.any?
114
+ end
115
+
116
+ case color
117
+ when Array
118
+ lputs color.map { |c| c.to_term(TermNotation::COLOR256) }.join("\n"), output
119
+ when nil
120
+ output.puts "Invalid color: #{ c }"
121
+ else
122
+ output.puts color.to_term(TermNotation::COLOR256)
102
123
  end
103
124
  end
104
125
 
105
- def test_theme
106
- example = <<-TEST
107
- # Time for testing your colors!
108
- module PryTheme
109
- module JustTesting
110
-
111
- THIS_IS_CONSTANT = :this_is_symbol
112
-
113
- class ThisIsClass
126
+ def find_color(color)
127
+ ColorConverter::COLORS.find { |c| c.term == color.to_i }
128
+ end
114
129
 
115
- def this_is_method
116
- this_is_float = 10_000.00
117
- this_is_integer = 10_000
130
+ def test_theme
131
+ example = <<-'TEST'
132
+ # "#{ PryTheme.current_theme }" theme.
133
+ class PryTheme::ThisIsAClass
134
+ def this_is_a_method
135
+ THIS_IS_A_CONSTANT = :this_is_a_symbol
136
+ this_is_a_local_var = "#{this} is a string.\n"
137
+ this_is_a_float = 10_000.00
138
+ this_is_an_integer = 10_000
139
+
140
+ # TRUE and FALSE are predefined constants.
141
+ $this_is_a_global_variable = TRUE or FALSE
142
+
143
+ @this_is_an_instance_variable = `echo '#{system} call\n'`
144
+ @@this_is_a_class_variable = @$ # An error.
145
+
146
+ /[0-9]{1,3}this #{is} a regexp\w+/xi
147
+ end
148
+ end
149
+ TEST
118
150
 
119
- "this_is_string"
151
+ lputs colorize_code(example), output
152
+ end
120
153
 
121
- TRUE or FALSE or ARGV # <-- Predefined constants
154
+ def edit_theme
155
+ theme = args.first || PryTheme.current_theme
156
+ theme.tr!("+", "") if new_theme_flag = (theme[0] == "+")
122
157
 
123
- @this_is_instance_variable
124
- @@this_is_class_variable
158
+ if not new_theme_flag and not installed?(theme)
159
+ output.puts %(Can't find "#{ theme }" theme in `#{ THEME_DIR }`.)
160
+ output.puts "To create a new theme, prepend a `+` sign to its name."
161
+ output.puts "Example: `pry-theme -e +#{ theme }`."
162
+ return
163
+ elsif new_theme_flag and installed?(theme)
164
+ output.puts "Can't create a theme with the given name."
165
+ output.puts %(The "#{ theme }" theme is already exist in your system.)
166
+ output.puts "You can edit it with `pry-theme -e #{ theme }` command."
167
+ return
168
+ end
125
169
 
126
- `echo 'Hello, hi, from "system" call!'`
170
+ theme_path = pathify_theme(theme)
171
+
172
+ if new_theme_flag
173
+ template = <<-TEMPLATE
174
+ ---
175
+ meta:
176
+ theme-name : #{ theme }
177
+ version : 1
178
+ color-depth : 256 # Supports 8 or 256 colors.
179
+ description : # Should be less than 80 characters.
180
+ author : # John Doe <johndoe@example.com>
181
+
182
+ theme:
183
+ class : # blue on yellow
184
+ class_variable : # red
185
+ comment : # on green
186
+ constant : # (bu)
187
+ error : # black (b) on white
188
+ float : # (i)
189
+ global_variable :
190
+ instance_variable :
191
+ integer :
192
+ keyword :
193
+ method :
194
+ predefined_constant :
195
+ regexp:
196
+ self :
197
+ content :
198
+ delimiter :
199
+ modifier :
200
+ function :
201
+ shell:
202
+ self :
203
+ content :
204
+ delimiter :
205
+ string:
206
+ self :
207
+ content :
208
+ modifier :
209
+ escape :
210
+ delimiter :
211
+ symbol :
212
+ TEMPLATE
213
+
214
+ # Create a template in themes directory.
215
+ File.open(theme_path, "w") { |f| f.puts template }
216
+
217
+ output.puts %(Created "#{ theme }" theme in `#{ THEME_DIR }`.)
218
+ output.puts "Opened it in #{ Pry.config.editor } for editing."
219
+ else
220
+ output.puts "Opened #{ theme } theme in #{ Pry.config.editor } for editing."
221
+ output.puts "Don't forget to increment a version number of theme!"
222
+ end
127
223
 
128
- $ # <-- The dollar is an error!
224
+ begin
225
+ old_theme = PryTheme.current_theme
129
226
 
130
- /[0-9]{1,3}this is regexp\\w+/xi
131
- $1 or $2 or $3333
227
+ display_preview!(theme, %<Current "#{ theme }">)
228
+ invoke_editor(theme_path, line=1, reloading=true)
229
+ display_preview!(theme, %<Edited "#{ theme }">)
230
+ ensure
231
+ PryTheme.set_theme(old_theme)
232
+ end
132
233
  end
133
234
 
134
- end
135
- end
136
- end
137
- # "#{PryTheme.current_theme}" theme.
138
- TEST
139
-
140
- lputs colorize_code(example)
235
+ # Please, pay attention to the fact that this method changes current
236
+ # theme to the given theme.
237
+ def display_preview!(theme, header_text)
238
+ PryTheme.set_theme(theme)
239
+ display_header(header_text, output)
240
+ test_theme
141
241
  end
142
242
 
143
243
  def show_list
@@ -149,9 +249,9 @@ end
149
249
  PryTheme.set_theme(theme)
150
250
 
151
251
  chunk = <<-CHUNK
152
- class PickMe
153
- def please
154
- @i, @@beg, you = 10_000, 400.00, "please!"
252
+ class Theme
253
+ def method
254
+ @ivar, @@cvar, lvar = 10_000, 400.00, "string"
155
255
  end
156
256
  end
157
257
  CHUNK
@@ -162,12 +262,15 @@ end
162
262
  [header, meta.description, "---", snippet].compact.join("\n")
163
263
  end
164
264
 
165
- lputs all_themes.join("\n")
265
+ lputs all_themes.join("\n"), output
166
266
  ensure
167
267
  PryTheme.set_theme(old_theme)
168
268
  end
169
269
 
170
270
  def show_remote_list
271
+ require 'net/https'
272
+ require 'json'
273
+
171
274
  body = {}
172
275
  fetch_collection("/") do |http, uri|
173
276
  output.puts "Fetching list of themes..."
@@ -177,17 +280,21 @@ end
177
280
 
178
281
  i = 0
179
282
  remote_themes = body.map do |theme|
180
- if (name = theme["name"]) =~ /\A[[a-z][0-9]-]+\z/
283
+ if (name = theme["name"]) =~ /\A(?:[a-z]|[0-9]|-)+\z/
181
284
  "#{i+=1}. #{installed?(name) ? make_bold(name) : name}"
182
285
  end
183
286
  end.compact
184
287
 
185
- lputs remote_themes.join("\n")
288
+ lputs remote_themes.join("\n"), output
186
289
  end
187
290
 
188
291
  def install_theme
189
292
  return unless args[0]
190
293
 
294
+ require 'net/https'
295
+ require 'json'
296
+ require 'base64'
297
+
191
298
  body = {}
192
299
  fetch_collection("/#{args[0]}/#{args[0]}.prytheme") do |http, uri|
193
300
  output.puts "Fetching theme from the collection..."
@@ -36,6 +36,10 @@ module PryTheme
36
36
  File.join(THEME_DIR, name)
37
37
  end
38
38
 
39
+ def pathify_theme(name)
40
+ File.join(THEME_DIR, "#{ name }.prytheme")
41
+ end
42
+
39
43
  def fetch_collection(path, &block)
40
44
  uri = URI.parse(COLLECTION + path)
41
45
  http = Net::HTTP.new(uri.host, uri.port)
@@ -44,5 +48,40 @@ module PryTheme
44
48
  yield(http, uri)
45
49
  end
46
50
 
51
+ def ansi?(color)
52
+ if color.to_s =~ /\A(\d{1,3})\z/
53
+ (0..255).include?(color.to_i)
54
+ else
55
+ false
56
+ end
57
+ end
58
+
59
+ def rgb?(color)
60
+ rgb_pattern = /
61
+ \A
62
+
63
+ ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
64
+ ,
65
+ ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
66
+ ,
67
+ ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
68
+
69
+ \z
70
+ /x
71
+ color =~ rgb_pattern ? true : false
72
+ end
73
+
74
+ def hex?(color)
75
+ color =~ /\A#?[A-F\d]{6}\z/i ? true : false
76
+ end
77
+
78
+ def display_header(text, out)
79
+ safe_width = 80
80
+
81
+ out.puts "-" * safe_width
82
+ out.puts make_bold(text.center(safe_width))
83
+ out.puts "-" * safe_width
84
+ end
85
+
47
86
  end
48
87
  end