nicer linewrap algo for the small printer

workspace
Brendan Howell 4 years ago
parent 9d687ad5cc
commit ae4260f54e

@ -8,11 +8,11 @@ import json
import logging
import os.path
import random
import re
import shutil
import string
import subprocess
import tempfile
import textwrap
import threading
import lmdb
@ -93,9 +93,12 @@ class LogPrinter(logging.Handler):
in_ep=self.printer["inep"],
out_ep=self.printer["outep"])
msg = self.format(record)
text = textwrap.fill(msg, width=self.printer["textwidth"])
text += "\r\n" * 4
prn.text(text)
#text = textwrap.fill(msg, width=self.printer["textwidth"])
spliter_regex = r'.{1,' + self.printer["textwidth"] + '}(?:\s+|$)'
out_text = "\r\n".join(line.strip() for line in
re.findall(spliter_regex, text)))
out_text += "\r\n" * 4
prn.text(out_text)
prn.cut()
@ -437,18 +440,22 @@ class Bureau(object):
+ pdfpath, shell=True)
# TODO: make this asynchronous
def print_small(self, text, cut=True):
def print_small(self, text, cut=True, linefeed=True):
"""
print on Thermal Line printer.
"""
prn = self._get_small_printer()
text = "\r\n" + textwrap.fill(text, width=self.smprint["textwidth"])
text += "\r\n" * 2
prn.text(text + "\r\n\r\n")
spliter_regex = r'.{1,' + self.printer["textwidth"] + '}(?:\s+|$)'
out_text = "\r\n".join(line.strip() for line in re.findall(splitter_regex, text)))
prn.text(out_text + "\r\n")
if linefeed:
prn.text("\r\n\r\n")
if cut:
prn.cut()
def print_small_image(self, img):
def print_small_image(self, img, linefeed=True):
"""
print an image on the mini thermal printer.
"""
@ -465,6 +472,8 @@ class Bureau(object):
im.thumbnail((self.smprint["width"], 1024), PIL.Image.ANTIALIAS)
# not using this bitImageColumn crashes some printers, sigh
prn.image(im, impl="bitImageColumn")
if linefeed:
prn.text("\r\n")
@add_command("test")
def test(self, data=None):

Loading…
Cancel
Save