From b64b534394ad55136b7ba223bf023c6914aaa5da Mon Sep 17 00:00:00 2001 From: Romain Porte Date: Tue, 1 Aug 2017 17:09:24 +0200 Subject: [PATCH] Add methods for simpler newlines (#246) --- src/escpos/escpos.py | 22 ++++++++++++++++++++++ test/test_function_text.py | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index 6d5cf5c..8d1fd81 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -460,6 +460,28 @@ class Escpos(object): txt = six.text_type(txt) self.magic.write(txt) + def textln(self, txt=''): + """Print alpha-numeric text with a newline + + The text has to be encoded in the currently selected codepage. + The input text has to be encoded in unicode. + + :param txt: text to be printed with a newline + :raises: :py:exc:`~escpos.exceptions.TextError` + """ + self.text('{}\n'.format(txt)) + + def ln(self, count=1): + """Print a newline or more + + :param count: number of newlines to print + :raises: :py:exc:`ValueError` if count < 0 + """ + if count < 0: + raise ValueError('Count cannot be lesser than 0') + if count > 0: + self.text('\n' * count) + def block_text(self, txt, font=None, columns=None): """ Text is printed wrapped to specified columns diff --git a/test/test_function_text.py b/test/test_function_text.py index 04222a1..81b0792 100644 --- a/test/test_function_text.py +++ b/test/test_function_text.py @@ -39,3 +39,27 @@ def test_block_text(): "All the presidents men were eating falafel for breakfast.", font='a') assert printer.output == \ b'All the presidents men were eating falafel\nfor breakfast.' + + +def test_textln(): + printer = get_printer() + printer.textln('hello, world') + assert printer.output == b'hello, world\n' + + +def test_textln_empty(): + printer = get_printer() + printer.textln() + assert printer.output == b'\n' + + +def test_ln(): + printer = get_printer() + printer.ln() + assert printer.output == b'\n' + + +def test_multiple_ln(): + printer = get_printer() + printer.ln(3) + assert printer.output == b'\n\n\n'