From ae4260f54e863292a5a46315dc1c9c95fe62af5b Mon Sep 17 00:00:00 2001 From: Brendan Howell Date: Sun, 22 Nov 2020 20:08:45 +0100 Subject: [PATCH] nicer linewrap algo for the small printer --- screenless/bureau/bureau.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/screenless/bureau/bureau.py b/screenless/bureau/bureau.py index e658439..41a90f6 100644 --- a/screenless/bureau/bureau.py +++ b/screenless/bureau/bureau.py @@ -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):