Last active
July 2, 2025 02:15
-
-
Save cumulus13/8130060e81566b7002d468c3208faf43 to your computer and use it in GitHub Desktop.
growli: send message to growl from sys.stdin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import os | |
from pathlib import Path | |
from rich.console import Console | |
from rich.syntax import Syntax | |
# Import dengan error handling | |
try: | |
from gntplib import Publisher | |
except ImportError: | |
print("Error: GNTPlib was not found. Install with: pip install gntplib") | |
sys.exit(1) | |
console = Console() | |
def main(): | |
# Path icon yang lebih robust | |
icon_path = Path(__file__).parent / 'icon.png' | |
# Inisialisasi Publisher | |
growl = Publisher( | |
"Growl STDIN", | |
["events", "progress", "error", "info", "warning"], | |
icon=str(icon_path) if icon_path.exists() else None | |
) | |
# Register dengan error handling yang lebih baik | |
try: | |
growl.register() | |
console.log("[#00FFFF]Growl was successfully registered[/]") | |
except Exception as e: | |
console.log(f"[white on red]Gagal mendaftar Growl: {e}[/]") | |
sys.exit(1) | |
# Baca dari stdin dengan error handling | |
try: | |
line_count = 0 | |
for line in sys.stdin: | |
line = line.strip() # Hapus whitespace | |
if line: # Skip baris kosong | |
growl.publish("info", "stdin", line, icon=str(icon_path) if icon_path.exists() else None) | |
line_syntax = Syntax(line, "python", theme = 'fruity') | |
console.log(line_syntax) | |
line_count += 1 | |
console.log(f"[#FFFF00]Finished processing {line_count} line[/]") | |
except KeyboardInterrupt: | |
console.log("[red]Stopped by the user") | |
sys.exit(0) | |
except Exception as e: | |
console.log(f"[red]Error when reading Stdin: {e}[/]") | |
sys.exit(1) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment