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

Loading…
Cancel
Save