commentbox 0.1.2 → 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 +42 -15
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 922e9531c4f2d9e403c9343697e58086c40217a625de7b66b8b145c619b8eae9
4
- data.tar.gz: 6d4ba601200f5fef70d43203dbc4e2ae4cd76d105c5e884cef96fccee26627ba
3
+ metadata.gz: 1ebd9f2c2594225bc341c2f5fe19c715ffbf5cb9baa10282738218f7aae49f5b
4
+ data.tar.gz: 9b6a3e592d4208f27392d03f74a8e9efcb0a7edd41557695a4b4e9b0e805506f
5
5
  SHA512:
6
- metadata.gz: f862760a55925ce00d08746251bc8512189e0acd301e6bb4c45d84cc42115da0fd97eb64eb508429ef959021c48e14361ddafe0e24e6ece5d9d9e09a776d4667
7
- data.tar.gz: 9eed5ab62484e8acefad8dccc13ddd843555adcac89eb6cc37a23bbd4cf06f3ae760475e75f498ce3b678ea7291ea79e67ad46310d164824947b7d5857d10df9
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,14 +156,15 @@ 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
@@ -158,10 +185,10 @@ private
158
185
  insert_line_if_even
159
186
  end
160
187
  def set_style (style)
161
- if style == nil then @style = Styles[DefaultParams[:style]]
162
- 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]
163
190
  elsif style.is_a? Hash then @style = style
164
- elsif style.is_a? String then @style = Styles[style.to_sym]
191
+ elsif style.is_a? String then @style = @@styles[style.to_sym]
165
192
  else raise 'CommentBox#style= : expected Symbol, Hash, or String here' end
166
193
  end
167
194
  def insert_line_if_even # also will delete a blank line if there is one
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.2
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