dotfiles/local/bin/brwmenu
2025-07-31 01:51:10 +03:30

99 lines
3.3 KiB
Python
Executable file

#!/usr/bin/env python3
import subprocess
import sys
import shutil
import urllib.parse
import os
DMENU_OPTS = ["-i", "-fn", "monospace:size=12", "-nb", "#151515", "-nf", "#bbbbbb", "-sb", "#005577", "-sf", "#eeeeee"]
BROWSER = "qutebrowser"
NOTIFIER = "notify-send"
CHOICES = [
"Paulgo", "URL", "Incognito URL", "YouTube", "Codeberg", "Tildegit", "IPLeak", "Safebooru", "Wikipedia"
]
def notify(title, message):
subprocess.run([NOTIFIER, title, message], stderr=subprocess.DEVNULL)
def run_dmenu(prompt, options=None):
cmd = ["dmenu", *DMENU_OPTS, "-p", prompt]
stdin = "\n".join(options) if options else None
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
out, _ = proc.communicate(stdin)
return out.strip()
def get_input(prompt, prefix=""):
text = run_dmenu(prompt)
if not text:
notify("Error", "No input provided")
sys.exit(1)
return prefix + urllib.parse.quote_plus(text)
def is_browser_running():
try:
proc = subprocess.Popen(["pgrep", "-f", BROWSER], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
proc.communicate()
return proc.returncode == 0
except:
return False
def open_url(url):
try:
if is_browser_running():
subprocess.Popen([BROWSER, ":open", "-t", url])
else:
subprocess.Popen([BROWSER, url])
except Exception as e:
notify("Error", f"Failed to open browser: {url}\n{e}")
sys.exit(1)
def search_with_paulgio(query):
return f"https://paulgo.io/search?q={urllib.parse.quote_plus(query)}"
def main():
choice = run_dmenu("Where to?", CHOICES)
if not choice:
notify("Error", "No selection made")
sys.exit(1)
elif choice == "Codeberg":
open_url(get_input("Username & repo:", "https://codeberg.org/"))
elif choice == "Tildegit":
open_url(get_input("Username & repo:", "https://tildegit.org/"))
elif choice == "IPLeak":
open_url("https://ipleak.net")
elif choice == "Paulgo":
open_url(search_with_paulgio(get_input("Search Paulgo:")))
elif choice == "Wikipedia":
open_url("https://en.wikipedia.org/wiki/Special:Search?search=" + get_input("Search Wikipedia:"))
elif choice == "URL":
url = run_dmenu("Enter URL:")
if not url:
notify("Error", "No URL entered")
sys.exit(1)
if not url.startswith("http"):
url = "https://" + url
open_url(url)
elif choice == "Incognito URL":
url = run_dmenu("Enter incognito URL:")
if not url:
notify("Error", "No URL entered")
sys.exit(1)
if not url.startswith("http"):
url = "https://" + url
subprocess.Popen(["brave-bin", "--incognito", url])
elif choice == "YouTube":
open_url("https://youtube.com/results?search_query=" + get_input("Search YouTube:"))
elif choice == "Safebooru":
open_url("https://safebooru.org/index.php?page=post&s=list&tags=" + get_input("Search Safebooru:"))
else:
notify("Error", f"Invalid selection: {choice}")
sys.exit(1)
if __name__ == "__main__":
for cmd in ["dmenu", BROWSER]:
if shutil.which(cmd) is None:
print(f"Error: Required command '{cmd}' not found", file=sys.stderr)
sys.exit(1)
main()