commentbox 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.
- checksums.yaml +7 -0
- data/lib/commentbox.rb +182 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 90f17dc47dac4efaadbc785344203e7e47f76cc6ac2231f75e35e58e3f95d223
|
4
|
+
data.tar.gz: ef860e5264a95a586d21700f3401504c83e191e41cf7e3a3d4712abe1abcba6a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 62542e2dcc6cf851a228f1ac355372dc110052671df44fc5b02c3d60d64e9dde9a4636087829013aff28592acda72c5028cccd91e6a1afd53f5e0b447f6f75af
|
7
|
+
data.tar.gz: 47975ff1fe460113e109125798dc6c8b1b27e64d3e67f9fda36d7dfdd8ebf52e5a82f529152c4cfbe3596a5736d7100cd77859ec9eecc2c2b07557e8821ac20a
|
data/lib/commentbox.rb
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
module CommentBoxStyles
|
2
|
+
private
|
3
|
+
DefaultParams = {
|
4
|
+
style: :stub,
|
5
|
+
padding: 4,
|
6
|
+
stretch: 0,
|
7
|
+
offset: 2,
|
8
|
+
spacelines: true,
|
9
|
+
alignment: :left
|
10
|
+
}
|
11
|
+
Styles = {
|
12
|
+
stub: {
|
13
|
+
hlines: '**',
|
14
|
+
oddlines: ['\\ ', ' \\'],
|
15
|
+
evenlines: ['/ ', ' /'],
|
16
|
+
oddcorners: ['=/','/=']
|
17
|
+
},
|
18
|
+
bars: {
|
19
|
+
hlines: '==',
|
20
|
+
oddlines: ['||', '||'],
|
21
|
+
evenlines: ['||', '||'],
|
22
|
+
oddcorners: ['/#','#/']
|
23
|
+
},
|
24
|
+
zigzag: {
|
25
|
+
hlines: '=-',
|
26
|
+
oddlines: ['\\ ', ' \\'],
|
27
|
+
evenlines: ['/ ', ' /'],
|
28
|
+
oddcorners: ['=O','O-']
|
29
|
+
},
|
30
|
+
money: {
|
31
|
+
hlines: '><',
|
32
|
+
oddlines: ['$!', '$!'],
|
33
|
+
evenlines: ['!$', '!$'],
|
34
|
+
oddcorners: ['>X','X<']
|
35
|
+
}
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
module CommentBoxIntegerExtensions
|
41
|
+
def is_even?
|
42
|
+
self % 2 == 0
|
43
|
+
end
|
44
|
+
end
|
45
|
+
module CommentBoxStringExtensions
|
46
|
+
Integer.prepend CommentBoxIntegerExtensions
|
47
|
+
def justify_to (length)
|
48
|
+
self << ' ' * (length - self.length)
|
49
|
+
end
|
50
|
+
def right_justify_to (length)
|
51
|
+
(' ' * (length - self.length)) + self
|
52
|
+
end
|
53
|
+
def center_align (length)
|
54
|
+
if !length.is_even? then raise 'String#center_align : length must be even' end
|
55
|
+
llength = ((length - self.length) / 2)
|
56
|
+
rlength = llength
|
57
|
+
if !self.length.is_even? then rlength += 1 end
|
58
|
+
(' ' * llength) + self + (' ' * rlength)
|
59
|
+
end
|
60
|
+
def align_to (side, length)
|
61
|
+
if side == :left
|
62
|
+
self.justify_to length
|
63
|
+
elsif side == :right
|
64
|
+
self.right_justify_to length
|
65
|
+
elsif side == :center
|
66
|
+
self.center_align length
|
67
|
+
else
|
68
|
+
raise 'String#align_to : expected :left, :right, or :center here'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class CommentBox
|
74
|
+
String.prepend CommentBoxStringExtensions # string align/justify methods
|
75
|
+
Integer.prepend CommentBoxIntegerExtensions # integer is_even? method
|
76
|
+
include CommentBoxStyles
|
77
|
+
|
78
|
+
attr_writer :padding, :spacelines, :alignment, :offset
|
79
|
+
# 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
|
81
|
+
def style= (value); @style = Styles[value]; self end
|
82
|
+
# there is no @stretch, stretch is just added to @max_line_length
|
83
|
+
# but, if stretch= is called, then @max_line_length must be recalculated
|
84
|
+
def stretch= (value); @max_line_length = @text.map(&:length).max + value; self end
|
85
|
+
|
86
|
+
def initialize (params) # params: {text: String or Array, style: Symbol, padding: Integer, spacelines: Boolean, alignment: Symbol or Array}
|
87
|
+
# for now require an argument of some sort
|
88
|
+
if params.class != Hash && params.class != String then raise 'CommentBox#initialize : you gotta initialize this with Hash or String.' end
|
89
|
+
|
90
|
+
# if it's not a hash, then make it one.
|
91
|
+
if params.is_a? String then params = {text: params} end
|
92
|
+
|
93
|
+
# fill in a bunch of instance variables from params or default values
|
94
|
+
style_symbol = params[:style] || DefaultParams[:style]; @style = Styles[style_symbol]
|
95
|
+
@padding = params[:padding] || DefaultParams[:padding]
|
96
|
+
@offset = params[:offset] || DefaultParams[:offset]
|
97
|
+
# one of the options for this is false, so it's not gonna play nice with ||
|
98
|
+
@spacelines = (params[:spacelines] != nil) ? params[:spacelines] : DefaultParams[:spacelines]
|
99
|
+
|
100
|
+
# call on some special methods to parse text and alignment
|
101
|
+
set_text params[:text]
|
102
|
+
set_alignment params[:alignment]
|
103
|
+
@max_line_length += (params[:stretch] || DefaultParams[:stretch]).to_i
|
104
|
+
end
|
105
|
+
def to_s
|
106
|
+
spaceLine = @spacelines ? [@style[:oddlines][0], ' ' * (@max_line_length + @padding * 2), @style[:oddlines][1], "\n"].join : ''
|
107
|
+
|
108
|
+
# construct an array of lines, join them together and return
|
109
|
+
return [
|
110
|
+
t_line(:begin),
|
111
|
+
spaceLine,
|
112
|
+
(0..@text.size - 1).map { |line| fmt_text_line line }, # this is a nested array and must be flattened
|
113
|
+
spaceLine,
|
114
|
+
t_line(:end)
|
115
|
+
# flatten, add offset to each line if it's not empty, join them together, and remove trailing newline
|
116
|
+
].flatten.map { |line| line == '' ? '' : (' ' * @offset) << line}.join.chomp
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
def set_text (string)
|
121
|
+
if string.is_a? String
|
122
|
+
@text = string.split "\n"
|
123
|
+
elsif string.is_a? Array
|
124
|
+
@text = string
|
125
|
+
else
|
126
|
+
raise 'CommentBox#text= : expected String or Array here'
|
127
|
+
end
|
128
|
+
@max_line_length = @text.map(&:length).max
|
129
|
+
if !@max_line_length.is_even? then @max_line_length += 1 end
|
130
|
+
insert_line_if_even
|
131
|
+
end
|
132
|
+
def set_alignment (align)
|
133
|
+
if align == nil
|
134
|
+
# set default value if no argument is given
|
135
|
+
@alignment = [ DefaultParams[:alignment] ] * (@text.size)
|
136
|
+
else
|
137
|
+
# fill array with align if one symbol is given
|
138
|
+
if align.is_a? Symbol
|
139
|
+
@alignment = [ align ] * (@text.size)
|
140
|
+
# set @alignment directly if an array is given
|
141
|
+
elsif align.is_a? Array
|
142
|
+
# if the array is too short, fill it with the last element
|
143
|
+
if align.size < @text.size then (@text.size - align.size).times { align.push align.last } end
|
144
|
+
@alignment = align
|
145
|
+
else
|
146
|
+
raise 'CommentBox#alignment= : expected Symbol or Array here'
|
147
|
+
end
|
148
|
+
end
|
149
|
+
# if the number of lines is even, insert a blank line between the first and second lines
|
150
|
+
insert_line_if_even
|
151
|
+
end
|
152
|
+
def insert_line_if_even # also will delete a blank line if there is one
|
153
|
+
# stop this function from raising errors we don't really care about if the instance isn't fully constructed yet
|
154
|
+
if !(@text.is_a?(Array) && @alignment.is_a?(Array)) then return end
|
155
|
+
|
156
|
+
if @text.size.is_even?
|
157
|
+
if @text[1] == '' then @text.delete_at 1; @alignment.delete_at 1
|
158
|
+
else @text.insert(1,''); @alignment.insert(1,:left) end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
def t_line (beginOrEnd)
|
162
|
+
if beginOrEnd == :begin
|
163
|
+
bchar = '/*'; echar = @style[:oddcorners][0]
|
164
|
+
else
|
165
|
+
bchar = @style[:oddcorners][1]; echar = '*/'
|
166
|
+
end
|
167
|
+
[bchar, @style[:hlines] * ((@max_line_length + @padding * 2) / 2), echar, "\n"].join
|
168
|
+
end
|
169
|
+
def fmt_text_line(line)
|
170
|
+
# justify the text
|
171
|
+
text = @text[line].align_to @alignment[line], @max_line_length
|
172
|
+
# pad the text
|
173
|
+
ret = (' ' * @padding) + (text) + (' ' * @padding)
|
174
|
+
# if there are no spacing lines, then even lines are odd & vice versa
|
175
|
+
line += (@spacelines ? 0 : 1)
|
176
|
+
# add border characters (according to @style) into even/odd lines respectively
|
177
|
+
if line.is_even? then @style[:evenlines][0] + ret + @style[:evenlines][1] + "\n"
|
178
|
+
else @style[:oddlines][0] + ret + @style[:oddlines][1] + "\n" end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
puts CommentBox.new "Lenny's box"
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: commentbox
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Leonard H. Phelan IV
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-12-31 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A little gem for generating pretty multiline comment boxes in C/C++ templates
|
14
|
+
email: lenny@lenny.beer
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/commentbox.rb
|
20
|
+
homepage: https://github.com/lennyitb/commentbox
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.0.0
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubygems_version: 3.3.26
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: Lenny C/C++ template CommentBox class
|
43
|
+
test_files: []
|