commentbox 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/commentbox.rb +47 -16
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c075de18d6c7e47412397f8429b95a4ccf4a510915c412912fecb899ea3c6e37
4
- data.tar.gz: 293ef1564eed9d108736d08284df7cc9d7e8f04961e0918fc5be062071d0e7ba
3
+ metadata.gz: 1ebd9f2c2594225bc341c2f5fe19c715ffbf5cb9baa10282738218f7aae49f5b
4
+ data.tar.gz: 9b6a3e592d4208f27392d03f74a8e9efcb0a7edd41557695a4b4e9b0e805506f
5
5
  SHA512:
6
- metadata.gz: 544071b2459e49a38323151808d87d172d0e29785cddb4aa54ef708a74479c93bcfe0a61f13791d672d0c1d38308b59acc9f6e3943f9ed2f669f18c43c9fd8ba
7
- data.tar.gz: a5ff842476987796f19f49b0523986e559b5868f3200b8fcf0b6b129407f4d43afeb87a87c43d5cede01ac66cfc842df1194e2502811a0d75fb173969584be6f
6
+ metadata.gz: b9fc9afd9b06cbb135e0bb0d43602b8e330d43409669d3c0a327b15009f44e8a46c0a4cbcd9cee70025608ea8ca1f2a429e36225b327101218c1b5dbfe37b98f
7
+ data.tar.gz: f2abc7f8fcdb3d98e1dc280a491e78eda83064bdd8e8aee0e775e7582b9238d41be48df936905ae390179037cbe99dc09c76cf5d7cbb5358ecf75a27e1029cf2
data/lib/commentbox.rb CHANGED
@@ -1,10 +1,12 @@
1
+
1
2
  module CommentBoxStyles
2
- private
3
3
  DefaultParams = {
4
4
  style: :stub,
5
5
  padding: 4,
6
6
  stretch: 0,
7
7
  offset: 2,
8
+ # TODO next. some people may want their boxes to all be the same width
9
+ min_width: 0,
8
10
  spacelines: true,
9
11
  alignment: :left
10
12
  }
@@ -15,6 +17,8 @@ private
15
17
  evenlines: ['/ ', ' /'],
16
18
  oddcorners: ['=/','/=']
17
19
  },
20
+ # i should rename this to parallax
21
+ # what a cool name
18
22
  bars: {
19
23
  hlines: '==',
20
24
  oddlines: ['||', '||'],
@@ -36,12 +40,12 @@ private
36
40
  }
37
41
  end
38
42
 
39
-
40
43
  module CommentBoxIntegerExtensions
41
44
  def is_even?
42
45
  self % 2 == 0
43
46
  end
44
47
  end
48
+
45
49
  module CommentBoxStringExtensions
46
50
  Integer.prepend CommentBoxIntegerExtensions
47
51
  def justify_to (length)
@@ -73,11 +77,30 @@ end
73
77
  class CommentBox
74
78
  String.prepend CommentBoxStringExtensions # string align/justify methods
75
79
  Integer.prepend CommentBoxIntegerExtensions # integer is_even? method
76
- include CommentBoxStyles
80
+ @@default_params = CommentBoxStyles::DefaultParams
81
+ @@styles = CommentBoxStyles::Styles
82
+
83
+ # class methods for messing with default values
84
+ def self.default_params; @@default_params end
85
+ def self.default_params=(value); @@default_params = value end
86
+ def self.set_default_params (value = {}); value.each { |key, val| @@default_params[key] = val } end
87
+ def self.styles; @@styles end
88
+ def self.add_style(value)
89
+ value.each do |key, val|
90
+ if val[:default] || val[:default?]
91
+ @@default_params[:style] = key
92
+ break # set the first default we find and stop the madness immediately
93
+ end
94
+ end
95
+ # I could attempt to remove a :default? key here but it doesn't matter
96
+ @@styles.merge! value
97
+ end
77
98
 
99
+ # instance setter methods
78
100
  attr_writer :padding, :spacelines, :alignment, :offset
79
101
  # I don't know how to call param= methods from the inside so I just use a set_param in private
80
- def text= (value); set_text value; self end; def alignment= (value); set_alignment value; self end
102
+ def text= (value); set_text value; self end;
103
+ def alignment= (value); set_alignment value; self end
81
104
  def style= (value); set_style value; self end
82
105
  # there is no @stretch, stretch is just added to @max_line_length
83
106
  # but, if stretch= is called, then @max_line_length must be recalculated
@@ -86,7 +109,8 @@ class CommentBox
86
109
  if !@max_line_length.is_even? then @max_line_length += 1 end
87
110
  self end
88
111
 
89
- def initialize (params) # params: {text: String or Array, style: Symbol, padding: Integer, spacelines: Boolean, alignment: Symbol or Array}
112
+ # needs more reliance on setter methods. changing instance variables after initialization is a total house of cards right now
113
+ def initialize (params) # params: {text: String or Array, style: Symbol, padding: Integer, spacelines: Boolean, alignment: Symbol or Array, and some others}
90
114
  # for now require an argument of some sort
91
115
  if params.class != Hash && params.class != String then raise 'CommentBox#initialize : you gotta initialize this with Hash or String.' end
92
116
 
@@ -94,20 +118,22 @@ class CommentBox
94
118
  if params.is_a? String then params = {text: params} end
95
119
 
96
120
  # fill in a bunch of instance variables from params or default values
97
- # style_symbol = params[:style] || DefaultParams[:style]; @style = Styles[style_symbol]
98
- @padding = params[:padding] || DefaultParams[:padding]
99
- @offset = params[:offset] || DefaultParams[:offset]
121
+ @padding = params[:padding] || @@default_params[:padding]
122
+ @offset = params[:offset] || @@default_params[:offset]
100
123
  # one of the options for this is false, so it's not gonna play nice with ||
101
- @spacelines = (params[:spacelines] != nil) ? params[:spacelines] : DefaultParams[:spacelines]
124
+ @spacelines = (params[:spacelines] != nil) ? params[:spacelines] : @@default_params[:spacelines]
125
+ @stretch = (params[:stretch] || @@default_params[:stretch]).to_i
126
+ @min_width = (params[:min_width] || @@default_params[:min_width]).to_i
102
127
 
103
128
  # call on some special methods to parse text and alignment
104
129
  set_text params[:text]
105
130
  set_alignment params[:alignment]
106
131
  set_style params[:style]
107
- @max_line_length += (params[:stretch] || DefaultParams[:stretch]).to_i
132
+ # @max_line_length += (params[:stretch] || @@default_params[:stretch]).to_i
108
133
  if !@max_line_length.is_even? then @max_line_length += 1 end
109
134
  end
110
135
  def to_s
136
+ # exact value of spaceLine is calculated if @spacelines is true, otherwise it's just an empty string
111
137
  spaceLine = @spacelines ? [@style[:oddlines][0], ' ' * (@max_line_length + @padding * 2), @style[:oddlines][1], "\n"].join : ''
112
138
 
113
139
  # construct an array of lines, join them together and return
@@ -130,35 +156,40 @@ private
130
156
  else
131
157
  raise 'CommentBox#text= : expected String or Array here'
132
158
  end
133
- @max_line_length = @text.map(&:length).max
159
+ @max_line_length = @text.map(&:length).max + @stretch
160
+ @max_line_length = @min_width unless @max_line_length > @min_width
134
161
  if !@max_line_length.is_even? then @max_line_length += 1 end
135
162
  insert_line_if_even
136
163
  end
137
164
  def set_alignment (align)
138
165
  if align == nil
139
166
  # set default value if no argument is given
140
- @alignment = [ DefaultParams[:alignment] ] * (@text.size)
167
+ @alignment = [ @@default_params[:alignment] ] * (@text.size)
141
168
  else
142
169
  # fill array with align if one symbol is given
143
170
  if align.is_a? Symbol
144
171
  @alignment = [ align ] * (@text.size)
145
172
  # set @alignment directly if an array is given
146
173
  elsif align.is_a? Array
174
+ align.map! { |a| a.to_sym }
147
175
  # if the array is too short, fill it with the last element
148
176
  if align.size < @text.size then (@text.size - align.size).times { align.push align.last } end
149
177
  @alignment = align
178
+ elsif align.is_a? String
179
+ @alignment = [ align.to_sym ] * (@text.size)
150
180
  else
151
- raise 'CommentBox#alignment= : expected Symbol or Array here'
181
+ raise 'CommentBox#alignment= : expected Symbol, Array, or String here'
152
182
  end
153
183
  end
154
184
  # if the number of lines is even, insert a blank line between the first and second lines
155
185
  insert_line_if_even
156
186
  end
157
187
  def set_style (style)
158
- if style == nil then @style = Styles[DefaultParams[:style]]
159
- elsif style.is_a? Symbol then @style = Styles[style]
188
+ if style == nil then @style = @@styles[@@default_params[:style]]
189
+ elsif style.is_a? Symbol then @style = @@styles[style]
160
190
  elsif style.is_a? Hash then @style = style
161
- else raise 'CommentBox#style= : expected Symbol or Hash here' end
191
+ elsif style.is_a? String then @style = @@styles[style.to_sym]
192
+ else raise 'CommentBox#style= : expected Symbol, Hash, or String here' end
162
193
  end
163
194
  def insert_line_if_even # also will delete a blank line if there is one
164
195
  # stop this function from raising errors we don't really care about if the instance isn't fully constructed yet
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commentbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leonard H. Phelan IV
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-31 00:00:00.000000000 Z
11
+ date: 2023-01-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A little gem for generating pretty multiline comment boxes in C/C++ templates
14
14
  email: lenny@lenny.beer