Add scripts necessary for the tmux url opener i created

This commit is contained in:
cubernetes 2023-08-12 21:34:50 +02:00
parent f297ff713b
commit 02c72e5889
2 changed files with 75 additions and 0 deletions

31
.local/bin/tmux_urls.py Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import os
import subprocess as sp
from typing import *
"""
the web url matching regex used by markdown
http://daringfireball.net/2010/07/improved_regex_for_matching_urls
https://gist.github.com/gruber/8891611
"""
URL_REGEX = r"\b((?:https?://)?(?:(?:www\.)?(?:[\da-z\.-]+)\.(?:[a-z]{2,6})|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:(?:[0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:(?:(?::[0-9a-fA-F]{1,4}){1,6})|:(?:(?::[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(?::[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])|(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])))(?::[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])?(?:/[\w\.-]*)*/?)\b"
def main() -> int:
proc = sp.Popen(['tmux', 'capture-pane', '-J', '-p'],
stdout=sp.PIPE,
stderr=sp.DEVNULL)
pane_out, pane_err = proc.communicate()
if proc.returncode != 0:
print("Could not capture tmux pane output")
print("Err:\033[31m", pane_err, "\033[m")
input("Press any key to exit")
return 1
matches = re.findall(URL_REGEX, pane_out.decode())
sp.run([os.environ['HOME'] + '/.local/bin/tui_choice.sh'] + matches)
return 0
if __name__ == '__main__':
raise SystemExit(main())

44
.local/bin/tui_choice.sh Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/bash
choose_from_menu () {
local prompt="${1}"
local outvar="${2}"
shift
shift
local options=("$@")
local cur=0
local count=${#options[@]}
local index=0
printf '\033[31m%s\033[m\n' "${prompt}"
while true ; do
# list all options (option list is zero-based)
index='0'
for o in "${options[@]}" ; do
if [ "${index}" = "${cur}" ] ; then
printf ' > \033[30;42m%s\033[m\n' "${o}"
else
printf ' %s\n' "${o}"
fi
index="$(( index + 1 ))"
done
# shellcheck disable=SC2162
read -sn1 key
if [ "${key}" = "k" ] ; then
cur="$(( (count + cur - 1) % count ))"
elif [ "${key}" = "j" ] ; then
cur="$(( (count + cur + 1) % count ))"
elif [ "${key}" = "" ] ; then
printf '\033[%sA\033[J' "${count}"
printf '\033[30;42m%s\033[m\n' "brave ${options["${cur}"]}"
break
fi
printf '\033[%sA' "${count}"
done
printf -v "${outvar}" '%s' "${options["${cur}"]}"
}
choose_from_menu "Open URL with brave:" choice "${@}"
if [ -n "${choice}" ] ; then
DISPLAY=:0 nohup brave "${choice}" 2>/dev/null 1>&2 & disown
fi