PrettyComment 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +191 -0
  2. data/lib/PrettyComment.rb +150 -15
  3. metadata +9 -4
@@ -0,0 +1,191 @@
1
+ PrettyComment
2
+ =============
3
+
4
+ Ruby gem to pretty-print status messages and comments to the terminal. The text is formatted to fill the full width
5
+ of the terminal.
6
+
7
+
8
+ Usage:
9
+ ------
10
+
11
+ ### Headers:
12
+ ```
13
+ puts PrettyComment.h1("To be or not to be")
14
+ puts
15
+ puts PrettyComment.h2("That is the question")
16
+ puts
17
+ puts PrettyComment.h3("Whether 'tis nobler in the mind")
18
+ ```
19
+
20
+ will result in:
21
+
22
+ ```
23
+ ################################################################################
24
+ # To be or not to be #
25
+ ################################################################################
26
+
27
+ #==============================================================================#
28
+ # That is the question #
29
+ #==============================================================================#
30
+
31
+ #------------------------------------------------------------------------------#
32
+ # Whether 'tis nobler in the mind #
33
+ #------------------------------------------------------------------------------#
34
+ ```
35
+
36
+ ### Text Formatting:
37
+
38
+ ```
39
+ Prefix = "Summary:"
40
+ PrefixOnlyFirstLine = true
41
+ puts PrettyComment.format_line(Summary, Prefix, PrefixOnlyFirstLine)
42
+ ```
43
+
44
+ will result in:
45
+
46
+ ```
47
+ Summary: The play is set in Denmark at the Castle Elsinore. The King Hamlet
48
+ has just died and his brother, Claudius, has replaced him and also
49
+ married his dead brother's wife, Gertrude. In the second scene of ACT
50
+ I, Claudius addresses the court on his recent marriage to Gertrude,
51
+ on political matters and also gives his blessings to Laertes, the son
52
+ of the Lord Chamberlain Polonius, who is about to leave for France.
53
+ He then turns to Hamlet, who is still mourning his father and doesn't
54
+ seem to get over it. Even if it's natural to mourn his father's
55
+ death, he argues, he should get over it.
56
+ ```
57
+
58
+ ### Separators:
59
+ ```
60
+ puts PrettyComment.separator
61
+ puts PrettyComment.small_separator
62
+ puts PrettyComment.separator '*'
63
+ ```
64
+
65
+ ```
66
+ #==============================================================================#
67
+ #------------------------------------------------------------------------------#
68
+ #******************************************************************************#
69
+ ```
70
+
71
+ ### Boxes:
72
+ ```
73
+ a_box = PrettyComment::Box.new do
74
+ para "Summary:"
75
+ hline '-'
76
+ para "The play is set in Denmark at the Castle Elsinore. The King Hamlet has just died and his brother, Claudius, has replaced him and also married his dead brother's wife, Gertrude. In the second scene of ACT I, Claudius addresses the court on his recent marriage to Gertrude, on political matters and also gives his blessings to Laertes, the son of the Lord Chamberlain Polonius, who is about to leave for France. He then turns to Hamlet, who is still mourning his father and doesn't seem to get over it. Even if it's natural to mourn his father's death, he argues, he should get over it."
77
+ h3 "Dramatis Personae:"
78
+ para "Hamlet: Son of the former king, and nephew of the present King\n"
79
+ para "Claudius: King of Denmark, Hamlet's uncle.\n"
80
+ para "Gertrude: Queen of Denmark, and mother to Hamlet\n"
81
+ para "Polonius: Lord Chamberlain\n"
82
+ para "Ophelia: Daughter to Polonius\n"
83
+ para "Horatio: Friend to Hamlet\n"
84
+ para "Laertes: Son to Polonius\n"
85
+ para "Voltimand, Cornelius: Courtiers\n"
86
+ para "Rosencrantz, Guildenstern: Courtiers, friends to Hamlet\n"
87
+ para "Osric: a Courtier\n"
88
+ para "Marcellus: an Officer\n"
89
+ para "Bernardo: an Officer\n"
90
+ para "Francisco: a Soldier\n"
91
+ para "Reynaldo: Servant to Polonius\n"
92
+ para "Ghost of Hamlet's Father\n"
93
+ para "Fortinbras: Prince of Norway\n"
94
+ para "Gravediggers: A sexton and a clown\n"
95
+ para "Player King, Player Queen, Lucianus, etc.: Players\n"
96
+ end
97
+ ```
98
+
99
+ will result in:
100
+ ```
101
+ #==============================================================================#
102
+ # Summary: #
103
+ #------------------------------------------------------------------------------#
104
+ # The play is set in Denmark at the Castle Elsinore. The King Hamlet has just #
105
+ # died and his brother, Claudius, has replaced him and also married his dead #
106
+ # brother's wife, Gertrude. In the second scene of ACT I, Claudius addresses #
107
+ # the court on his recent marriage to Gertrude, on political matters and also #
108
+ # gives his blessings to Laertes, the son of the Lord Chamberlain Polonius, #
109
+ # who is about to leave for France. He then turns to Hamlet, who is still #
110
+ # mourning his father and doesn't seem to get over it. Even if it's natural #
111
+ # to mourn his father's death, he argues, he should get over it. #
112
+ #------------------------------------------------------------------------------#
113
+ # Dramatis Personae: #
114
+ #------------------------------------------------------------------------------#
115
+ # Hamlet: Son of the former king, and nephew of the present King #
116
+ # Claudius: King of Denmark, Hamlet's uncle. #
117
+ # Gertrude: Queen of Denmark, and mother to Hamlet #
118
+ # Polonius: Lord Chamberlain #
119
+ # Ophelia: Daughter to Polonius #
120
+ # Horatio: Friend to Hamlet #
121
+ # Laertes: Son to Polonius #
122
+ # Voltimand, Cornelius: Courtiers #
123
+ # Rosencrantz, Guildenstern: Courtiers, friends to Hamlet #
124
+ # Osric: a Courtier #
125
+ # Marcellus: an Officer #
126
+ # Bernardo: an Officer #
127
+ # Francisco: a Soldier #
128
+ # Reynaldo: Servant to Polonius #
129
+ # Ghost of Hamlet's Father #
130
+ # Fortinbras: Prince of Norway #
131
+ # Gravediggers: A sexton and a clown #
132
+ # Player King, Player Queen, Lucianus, etc.: Players #
133
+ #==============================================================================#
134
+ ```
135
+
136
+ ###Styles:
137
+
138
+ ```
139
+ puts PrettyComment::Box.new {
140
+ box_style 'c++'
141
+ box_width 120
142
+ para "This is a long c++ source comment box."
143
+ }
144
+ puts
145
+
146
+ puts PrettyComment::Box.new {
147
+ box_style 'latex'
148
+ para "This is a latex comment box."
149
+ }
150
+ puts
151
+
152
+ puts PrettyComment::Box.new {
153
+ box_left '|'
154
+ box_right '/'
155
+ box_top '*'
156
+ box_bottom '#'
157
+ para "This is a custom box."
158
+ }
159
+ ```
160
+
161
+ will result in:
162
+
163
+
164
+ ```
165
+ //====================================================================================================================//
166
+ // This is a long c++ source comment box. //
167
+ //====================================================================================================================//
168
+
169
+ %==============================================================================%
170
+ % This is a latex comment box. %
171
+ %==============================================================================%
172
+
173
+ |******************************************************************************/
174
+ | This is a free-form box. /
175
+ |##############################################################################/
176
+ ```
177
+
178
+
179
+ Currently the following styles are implemented:
180
+ * default for terminal, ruby, python etc.
181
+ * c++
182
+ * latex
183
+
184
+ Styles can also be set globally on the Box class:
185
+
186
+ ```
187
+ PrettyComment::Box::style = 'c++'
188
+ PrettyComment::Box::width = 120
189
+ PrettyComment::Box::left = '#'
190
+ etc.
191
+ ```
@@ -5,9 +5,35 @@ require 'terminfo'
5
5
  #======================================================================================================================#
6
6
 
7
7
  module PrettyComment
8
+ @@width = TermInfo.screen_size[1]
9
+ @@styles = {
10
+ 'default' => { :left => '#', :right => '#' },
11
+ 'c++' => { :left => '//', :right => '//'},
12
+ 'latex' => { :left => '%', :right => '%'}
13
+ }
14
+
15
+ #======================================================================================================================#
16
+
17
+ def self.width
18
+ @@width
19
+ end
8
20
 
9
21
 
10
- #======================================================================================================================#
22
+ #----------------------------------------------------------------------------------------------------------------------#
23
+
24
+ def self.width=(a_width)
25
+ @@width = a_width
26
+ end
27
+
28
+
29
+ #----------------------------------------------------------------------------------------------------------------------#
30
+
31
+ def self.styles
32
+ @@styles
33
+ end
34
+
35
+
36
+ #----------------------------------------------------------------------------------------------------------------------#
11
37
 
12
38
  def self.terminal_width
13
39
  TermInfo.screen_size[1]
@@ -16,8 +42,8 @@ end
16
42
 
17
43
  #----------------------------------------------------------------------------------------------------------------------#
18
44
 
19
- def self.separator(char='=', left='#', right='#')
20
- left + (char * (terminal_width - left.length - right.length)) + right
45
+ def self.separator(char='=', left='#', right='#', width=@@width)
46
+ left + (char * (width - left.length - right.length)) + right
21
47
  end
22
48
 
23
49
 
@@ -31,7 +57,7 @@ end
31
57
  #----------------------------------------------------------------------------------------------------------------------#
32
58
 
33
59
  def self.comment(comment)
34
- pad_string("\# #{comment}", "#", terminal_width)
60
+ pad_string("\# #{comment}", "#", width)
35
61
  end
36
62
 
37
63
 
@@ -51,9 +77,9 @@ end
51
77
 
52
78
  #----------------------------------------------------------------------------------------------------------------------#
53
79
 
54
- def self.format_line(text, prefix, only_first_line_prefix=false, suffix='', alternate_prefix='')
80
+ def self.format_line(text, prefix, only_first_line_prefix=false, suffix='', alternate_prefix='', width=@@width)
55
81
  the_suffix = suffix.length ? " " + suffix : ""
56
- text_width = terminal_width - 1 - the_suffix.length
82
+ text_width = width - 1 - the_suffix.length
57
83
  title_array = text.split(" ")
58
84
  result = []
59
85
  current_line = prefix.dup
@@ -64,12 +90,12 @@ def self.format_line(text, prefix, only_first_line_prefix=false, suffix='', alte
64
90
 
65
91
  title_array.each do |w|
66
92
  (current_line.length + w.length + 1 > text_width) \
67
- && result << pad_string(current_line, suffix, terminal_width) \
93
+ && result << pad_string(current_line, suffix, width) \
68
94
  && current_line = current_prefix.dup
69
95
  current_line << " " << w
70
96
  end
71
97
 
72
- current_line.length > current_prefix.length && result << pad_string(current_line, suffix, terminal_width)
98
+ current_line.length > current_prefix.length && result << pad_string(current_line, suffix, width)
73
99
  result.join("\n")
74
100
  end
75
101
 
@@ -105,20 +131,119 @@ end
105
131
  #----------------------------------------------------------------------------------------------------------------------#
106
132
 
107
133
  class Box
108
- attr_accessor :left, :right, :top, :bottom
134
+ class << self
135
+ attr_accessor :right, :top, :bottom, :width
136
+ end
137
+ @@left = @@right = '#'
138
+ @@top = @@bottom = '='
139
+ @@width = PrettyComment::width
140
+
141
+ attr_accessor :left, :right, :top, :bottom, :width
109
142
  def initialize *a, &b
110
- @left = @right = '#'
111
- @top = @bottom = '='
112
- @width = PrettyComment.terminal_width
143
+ @left = @@left
144
+ @right = @@right
145
+ @top = @@top
146
+ @bottom = @@bottom
147
+ @width = @@width
113
148
  @linebuffer = []
114
149
  cloaker(&b).bind(self).call(*a) if b
115
150
  end
116
151
 
117
152
 
153
+ #----------------------------------------------------------------------------------------------------------------------#
154
+
155
+ def self.left=(a_left)
156
+ @@left = a_left
157
+ end
158
+
159
+
160
+ #----------------------------------------------------------------------------------------------------------------------#
161
+
162
+ def self.right=(a_right)
163
+ @@right = a_right
164
+ end
165
+
166
+
167
+ #----------------------------------------------------------------------------------------------------------------------#
168
+
169
+ def self.top=(a_top)
170
+ @@top = a_top
171
+ end
172
+
173
+
174
+ #----------------------------------------------------------------------------------------------------------------------#
175
+
176
+ def self.bottom=(a_bottom)
177
+ @@bottom = a_bottom
178
+ end
179
+
180
+
181
+ #----------------------------------------------------------------------------------------------------------------------#
182
+
183
+ def self.width=(a_width)
184
+ @@width = a_width
185
+ end
186
+
187
+
188
+ #----------------------------------------------------------------------------------------------------------------------#
189
+
190
+ def self.style=(a_style)
191
+ style = PrettyComment::styles[a_style]
192
+
193
+ @@left = style[:left]
194
+ @@right = style[:right]
195
+ end
196
+
197
+
198
+ #----------------------------------------------------------------------------------------------------------------------#
199
+
200
+ def box_style(a_style)
201
+ style = PrettyComment.styles[a_style]
202
+
203
+ @left = style[:left]
204
+ @right = style[:right]
205
+ end
206
+
207
+
208
+ #----------------------------------------------------------------------------------------------------------------------#
209
+
210
+ def box_left(a_left)
211
+ @left = a_left
212
+ end
213
+
214
+
215
+ #----------------------------------------------------------------------------------------------------------------------#
216
+
217
+ def box_right(a_right)
218
+ @right = a_right
219
+ end
220
+
221
+
222
+ #----------------------------------------------------------------------------------------------------------------------#
223
+
224
+ def box_top(a_top)
225
+ @top = a_top
226
+ end
227
+
228
+
229
+ #----------------------------------------------------------------------------------------------------------------------#
230
+
231
+ def box_bottom(a_bottom)
232
+ @bottom = a_bottom
233
+ end
234
+
235
+
236
+ #----------------------------------------------------------------------------------------------------------------------#
237
+
238
+ def box_width(a_width)
239
+ @width = a_width
240
+ end
241
+
242
+
118
243
  #----------------------------------------------------------------------------------------------------------------------#
119
244
 
120
245
  def para(a_string)
121
- @linebuffer << PrettyComment::format_line(a_string, left, false, right)
246
+ @linebuffer << PrettyComment::format_line(a_string, left, false, right, '', width)
122
247
  end
123
248
 
124
249
 
@@ -133,7 +258,7 @@ class Box
133
258
 
134
259
  def heading(a_string, a_char)
135
260
  @linebuffer << PrettyComment.separator(a_char, left, right)
136
- @linebuffer << PrettyComment::format_line(a_string, left, false, right)
261
+ @linebuffer << PrettyComment::format_line(a_string, left, false, right, '', width)
137
262
  @linebuffer << PrettyComment.separator(a_char, left, right)
138
263
  end
139
264
 
@@ -163,9 +288,19 @@ class Box
163
288
 
164
289
  def to_s
165
290
  newline = "\n"
166
- PrettyComment::separator(top) + newline + @linebuffer.join("\n") + newline + PrettyComment::separator(bottom)
291
+ result = PrettyComment::separator(top, left, right, width) << newline
292
+ result << @linebuffer.join("\n") << newline
293
+ result << PrettyComment::separator(bottom, left, right, width)
294
+ end
295
+
296
+
297
+ #----------------------------------------------------------------------------------------------------------------------#
298
+
299
+ def inspect
300
+ to_s
167
301
  end
168
302
 
303
+
169
304
  #----------------------------------------------------------------------------------------------------------------------#
170
305
 
171
306
  def cloaker &b
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: PrettyComment
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-20 00:00:00.000000000 Z
12
+ date: 2012-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ruby-terminfo
@@ -27,13 +27,18 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: 0.1.1
30
- description: Formatting of status messages and comments for terminal output.
30
+ description: ! 'Formatting of status messages and comments for terminal output and
31
+ source files.
32
+
33
+ '
31
34
  email: wolfgang.steiner@gmail.com
32
35
  executables: []
33
36
  extensions: []
34
- extra_rdoc_files: []
37
+ extra_rdoc_files:
38
+ - README.md
35
39
  files:
36
40
  - lib/PrettyComment.rb
41
+ - README.md
37
42
  homepage: https://github.com/WolfgangSteiner/PrettyComment
38
43
  licenses: []
39
44
  post_install_message: