Skip to content

Commit 49bc615

Browse files
committed
Allow generating multi-line QR codes in batch mode
1 parent 55e274e commit 49bc615

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,20 @@ commands are:
191191
first.
192192
* `TEXTADD`: Adds an additional line to the current block of text. Used to
193193
create multiline text blocks.
194-
* `QR`: Creates a QR code block
194+
* `QRSTART`: Creates a QR code block
195+
* `QRADD`: Adds additional lines to the text that will be encoded to form the QR
196+
code (rarely used)
195197

196198
For instance, this input:
197199

198200
```text
199201
TEXTSTART:FD12
200202
TEXTADD:2013
201-
QR:1234
203+
QRSTART:1234
202204
TEXTSTART:BIG
203205
TEXTSTART:LINE1
204206
TEXTADD:LINE2
205-
QR:12345
207+
QRSTART:12345
206208
```
207209

208210
Creates a label with:

src/labelle/cli/cli.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -506,24 +506,44 @@ def render_text(lines):
506506

507507
if batch:
508508
text_accumulator: List[str] = []
509+
qr_accumulator: List[str] = []
510+
511+
def flush_text():
512+
nonlocal text_accumulator
513+
if len(text_accumulator) > 0:
514+
render_text(text_accumulator)
515+
text_accumulator = []
516+
517+
def flush_qr():
518+
nonlocal qr_accumulator
519+
if len(qr_accumulator) > 0:
520+
render_engines.append(
521+
QrRenderEngine(qr_callback("\n".join(qr_accumulator)))
522+
)
523+
qr_accumulator = []
524+
525+
def flush_both():
526+
flush_text()
527+
flush_qr()
528+
509529
for line in sys.stdin:
510530
line = line.rstrip("\r\n")
511531
parts = line.split(":", 1)
512532
if parts[0] == "TEXTSTART":
513-
if len(text_accumulator) > 0:
514-
render_text(text_accumulator)
515-
text_accumulator = [parts[1]]
533+
flush_both()
534+
text_accumulator.append(parts[1])
516535
elif parts[0] == "TEXTADD":
536+
flush_qr()
517537
text_accumulator.append(parts[1])
518-
elif parts[0] == "QR":
519-
if len(text_accumulator) > 0:
520-
render_text(text_accumulator)
521-
text_accumulator = []
522-
render_engines.append(QrRenderEngine(qr_callback(parts[1])))
538+
elif parts[0] == "QRSTART":
539+
flush_both()
540+
qr_accumulator.append(parts[1])
541+
elif parts[0] == "QRADD":
542+
flush_text()
543+
qr_accumulator.append(parts[1])
523544
else:
524545
print("WARNING: invalid command", line)
525-
if len(text_accumulator) > 0:
526-
render_text(text_accumulator)
546+
flush_both()
527547

528548
if fixed_length is None:
529549
min_label_mm_len = min_length

0 commit comments

Comments
 (0)