mostra numeri da stdin

This commit is contained in:
boyska 2021-09-23 17:48:42 +02:00
parent a5f837e49c
commit 6ce715c761

View file

@ -1,5 +1,7 @@
#!/usr/bin/env python3
import os
import sys
import argparse
import random
@ -13,13 +15,20 @@ from gi.repository import Gtk, Gio, cairo, Pango, Gdk, PangoCairo, GLib
def get_parser():
p = argparse.ArgumentParser()
p = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
p.add_argument(
"--read-only",
action="store_true",
dest="readonly",
help="Display but dont change the number",
)
p.add_argument(
"--stdin",
action="store_true",
default=True,
dest="stdin",
help="stdin is read and shown",
)
p.add_argument("--fullscreen", action="store_true", default=True, dest="fullscreen")
p.add_argument(
"--no-fullscreen", action="store_false", default=True, dest="fullscreen"
@ -84,7 +93,8 @@ class AppWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
self.app = kwargs["application"]
super().__init__(*args, **kwargs)
self.text = "666"
self.text = ""
self.buffer = ""
self.rotation = 0
self.invert_colors = False
self.drawing_area = Gtk.DrawingArea()
@ -96,17 +106,33 @@ class AppWindow(Gtk.ApplicationWindow):
if self.app.cli_args.fullscreen:
self.fullscreen()
self.show_all()
GLib.timeout_add(1000, self.refresh_number)
if self.app.cli_args.stdin:
os.set_blocking(sys.stdin.fileno(), False)
GLib.io_add_watch(sys.stdin.fileno(), GLib.IO_IN, self._on_stdin_data)
if self.app.cli_args.invert_every > 0:
GLib.timeout_add(self.app.cli_args.invert_every * 1000, self.swap_colors)
def force_redraw(self):
self.drawing_area.queue_draw()
def refresh_number(self):
self.text = str(random.randint(1, 150))
self.force_redraw()
def _on_stdin_data(self, *args, **kwargs):
max_line_length = 16
incoming_data = sys.stdin.read(max_line_length)
if not incoming_data:
return True
self.buffer += incoming_data
if len(self.buffer) == max_line_length:
self.buffer += '\n'
if "\n" in self.buffer:
parts = self.buffer.split("\n")
self.buffer = parts[-1]
self.change_text(parts[-2])
return True
def change_text(self, text: str):
self.text = text
self.force_redraw()
def swap_colors(self):
self.invert_colors = not self.invert_colors
self.force_redraw()