ruby-escpos 0.0.3 → 0.0.4

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ruby-escpos.rb +45 -3
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a725c3d8908c99bb7199a01f49bd5667696bcfd1
4
- data.tar.gz: c5c9d5c327a8881b37078bc2424c2cdebd10a24f
3
+ metadata.gz: 87e97248f89df4e3486aa126d1341ac6b54d0a06
4
+ data.tar.gz: 5665c08d5fe5b577c4fdde1148c5daa8729ef950
5
5
  SHA512:
6
- metadata.gz: 8df40d8ffb77e1ae9a0e682521bdf8afa411a7e981c47960fb1910062e35cccafcc180b8960c05288ccf23262cf86251a0ad6901b8f745dfc92b544898a81e1f
7
- data.tar.gz: 7168865b0fcd7cdc72d16c6e0b4af9278d91f6ea7a93665c8802c97b1c445827ccf5a57c0961c46a71d70e585ae05c9c8d7ae2e6ff69363efa191d1c3c86d362
6
+ metadata.gz: 231d9e6f3805ba35c806ba6a847b39e62576b9af6596df1da55e3aded766cc73a437c5fedf30e56c7e0e725311e2698807d435089cd026eb4673191b36de5605
7
+ data.tar.gz: 04c3a1256493d7aad0156a7661da98b70e2d6034f39f1f67874556607172e832601b975734878cdfb5ed846bdf2f4f29cf4b2e5699edf02965d50f1e6efab7da
data/lib/ruby-escpos.rb CHANGED
@@ -83,6 +83,7 @@ class RubyEscPos
83
83
  write font
84
84
  write bc
85
85
  write code
86
+ new_line
86
87
  end
87
88
 
88
89
  def text(txt, new_lines = 1)
@@ -98,15 +99,56 @@ class RubyEscPos
98
99
  write cut_type
99
100
  end
100
101
 
102
+ def table(rows, new_lines = 1, main_column_index = 1, max_width = 42)
103
+ max_widths = Array.new(rows[0].count) { |i| 0 }
104
+
105
+ rows.each_with_index do |row, i|
106
+ row.each_with_index do |cell, y|
107
+ if y < row.length - 1
108
+ rows[i][y][:text] += ' '
109
+ end
110
+
111
+ if max_widths[y] < cell[:text].length
112
+ max_widths[y] = cell[:text].length
113
+ end
114
+
115
+ if max_widths[y] < cell[:width].to_i
116
+ max_widths[y] = cell[:width]
117
+ end
118
+
119
+ if cell[:text].length < cell[:width].to_i
120
+ cell[:text] = (' ' * (cell[:width] - cell[:text].length)) + cell[:text]
121
+ end
122
+ end
123
+ end
124
+
125
+ rows.each_with_index do |row, i|
126
+ row.each_with_index do |cell, y|
127
+ padding = ''
128
+ if y == main_column_index
129
+ padding = (' ' * (max_width - row.map { |x| x[:text].length }.inject(0, :+)))
130
+ elsif y < row.length - 1
131
+ padding = (' ' * (max_widths[y] - cell[:text].length))
132
+ end
133
+
134
+ if cell[:align]
135
+ write(cell[:text] = padding + cell[:text])
136
+ else
137
+ write(cell[:text] = cell[:text] + padding)
138
+ end
139
+ end
140
+ new_line
141
+ end
142
+
143
+ new_line new_lines - 1
144
+ end
145
+
101
146
  private
102
147
 
103
148
  def write(data)
104
149
  if data.is_a? String
105
150
  @buffer += data
106
151
  elsif data.is_a? Array
107
- #@buffer += data.join
108
- #@buffer += data.map { |b| sprintf(", 0x%02X", b) }.join
109
- #@buffer += data.map { |x| x.hex }.pack('c*')
110
152
  @buffer += data.pack('U*')
111
153
  end
112
154
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-escpos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederick Lemire-Collu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-23 00:00:00.000000000 Z
11
+ date: 2015-04-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby library to manipulate ESC/POS Printers
14
14
  email: frederick@buildrevision.com