forked from GLaD0S/thermal-printer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
89 lines (71 loc) · 2.6 KB
/
main.py
File metadata and controls
89 lines (71 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import argparse
import os
import sys
from escpos.printer import Network
def build_ornate_box(message: str, width: int = 68) -> str:
inner_width = width - 2
centered_message = f"{message}".center(inner_width)
lines = [
"+" + "=" * inner_width + "+",
"|" + "*" * inner_width + "|",
"|" + ("*~" * (inner_width // 2)).ljust(inner_width, "*") + "|",
centered_message,
"|" + ("~*" * (inner_width // 2)).ljust(inner_width, "*") + "|",
"|" + "*" * inner_width + "|",
"+" + "=" * inner_width + "+",
]
return "\n".join(lines) + "\n\n"
def ticket(message: str | None = None, jira_code: str | None = None, time_estimate: str | None = None) -> str:
ticket_text = ""
if jira_code:
ticket_text += f"\n\n{jira_code}:\n"
if message:
ticket_text += f"\n{message}\n"
if time_estimate:
ticket_text += f"\nTime: {time_estimate}\n\n"
return build_ornate_box(f"{ticket_text}") + "\n\n\n\n\n\n\n\n\n"
def main() -> None:
default_host = os.environ.get("THERMAL_PRINTER_HOST", "192.168.1.175")
parser = argparse.ArgumentParser(
description="Print a framed ticket on a network ESC/POS thermal printer.",
)
parser.add_argument(
"--host",
default=default_host,
help=f"Printer IP or hostname (default: {default_host}, or THERMAL_PRINTER_HOST).",
)
parser.add_argument(
"--profile",
default="TSP600",
help="python-escpos printer profile name (default: %(default)s).",
)
parser.add_argument("-m", "--message", help="Main ticket text.")
parser.add_argument("-j", "--jira-code", help="Jira issue key or label printed at the top.")
parser.add_argument("-t", "--time-estimate", help="Time estimate line.")
parser.add_argument(
"--dry-run",
action="store_true",
help="Write ticket text to stdout instead of printing.",
)
parser.add_argument(
"--no-cut",
action="store_true",
help="Do not send a cut command after printing.",
)
args = parser.parse_args()
if not any([args.message, args.jira_code, args.time_estimate]):
parser.error("Provide at least one of: --message, --jira-code, --time-estimate")
text = ticket(
message=args.message,
jira_code=args.jira_code,
time_estimate=args.time_estimate,
)
if args.dry_run:
sys.stdout.write(text)
return
printer = Network(args.host, profile=args.profile, port=9100, timeout=5)
printer.text(text)
if not args.no_cut:
printer.cut()
if __name__ == "__main__":
main()