From 792f77f2182a891686c3d182617aa45f7bb7fd71 Mon Sep 17 00:00:00 2001 From: ns Date: Thu, 4 Jun 2026 18:20:05 -0500 Subject: [PATCH] waybar integration --- README.md | 3 +++ main.py | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/README.md b/README.md index a51678f..79fb99d 100644 --- a/README.md +++ b/README.md @@ -22,5 +22,8 @@ requires `~/.config/simplewhispr/config.json`: } ``` +## integrations +waybar: `tail -F /tmp/simplewhispr-waybar.log`, json output + --- Copyright (C) Noah Swerhun under the terms of [GNU GPLv3](https://www.gnu.org/licenses/gpl-3.0-standalone.html). diff --git a/main.py b/main.py index 7f543c3..15ce217 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,7 @@ from openai import OpenAI # flag to control recording recording = True +status_file = None def parse_config() -> tuple[str, str]: @@ -33,6 +34,21 @@ def parse_config() -> tuple[str, str]: raise Exception(f"fatal: configuration error: {e}") +def report_status(message: str): + global status_file + if status_file is None: + try: + status_file = open("/tmp/simplewhispr-waybar.log", "w") + except OSError: + return + + try: + status_file.write(f'{{"text":"{message}"}}\n') + status_file.flush() + except OSError: + pass + + def handle_sigusr1(signum, frame): global recording print("\ninfo: SIGUSR1 received. stopping recording.") @@ -48,11 +64,13 @@ def transcribe_audio(filename: str) -> str: client = OpenAI(api_key=api_key) with open(filename, "rb") as audio_file: + report_status("󰙏") transcription = client.audio.transcriptions.create( model=model, file=audio_file, ) + report_status("󰥔") return transcription.text @@ -64,6 +82,7 @@ def grab_recording() -> str: print( f"info: starting recording to {output_filename}. send SIGUSR1 (kill -usr1 {os.getpid()}) to stop." ) + report_status("󰍬") process = subprocess.Popen( cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True @@ -74,12 +93,14 @@ def grab_recording() -> str: time.sleep(0.5) if process.poll() is not None: print("fatal: ffmpeg exited unexpectedly.") + report_status("") sys.exit(1) # stop recording process.terminate() process.wait() print("info: recording stopped.") + report_status("󰥔") return output_filename @@ -87,13 +108,17 @@ def main(): with open("/tmp/simplewhispr.pid", "w") as f: f.write(str(os.getpid())) + report_status("󰥔") + recording_file = grab_recording() print("info: transcribing...") transcription = transcribe_audio(recording_file) print(f"info: transcription: {transcription}") # use wtype to type the output + report_status("󰌌") subprocess.run(["wtype", transcription]) + report_status("") if os.path.exists("/tmp/simplewhispr.pid"): os.remove("/tmp/simplewhispr.pid")