dotfiles/fake_home/.sofi.sh

240 lines
6.8 KiB
Bash
Raw Permalink Normal View History

2023-04-04 20:16:50 -04:00
#!/bin/bash
WINDOW_NAME="active-sofi-window"
CACHE_FILE="$HOME/.app-cache-sofi"
IGNORED_APPS="Firetools|cmatrix"
APP_LOCATIONS="/usr/share/applications $HOME/.local/share/applications "
FILE_LOCATIONS="$HOME/Documents $HOME/Downloads"
2024-02-19 21:36:41 -05:00
[[ "$SOFI_FONT" == "" ]] && SOFI_FONT="FiraCode Nerd Font"
! [[ "$XDG_SESSION_TYPE" = "x11" ]]
IS_X11="$?"
2023-12-13 12:24:21 -05:00
sofi_id() {
gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.List | cut -c 3- | rev | cut -c4- | rev | jq '.[] | .id' | while read id; do
if gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.GetTitle $id | grep "SOFI" &> /dev/null; then
echo $id
fi
done
}
2023-04-04 20:16:50 -04:00
# Open search window or raise existing one
if [[ "$1" == "--launch" ]]; then
if (($IS_X11)) && existing="$(xdotool search $WINDOW_NAME)"; then
2023-04-04 20:16:50 -04:00
xdotool windowraise "$existing"
else
# xfce4-terminal --role="$WINDOW_NAME" --title="$WINDOW_NAME" --startup-id="$WINDOW_NAME" -e "bash -c '$HOME/.sofi.sh'"
2023-12-13 12:24:21 -05:00
id="$(sofi_id)"
if [[ "$id" != "" ]]; then
gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.Activate $id
else
2024-02-19 21:36:41 -05:00
xterm -geometry 120x40 -fa "$SOFI_FONT" -fs 11 -T "SOFI" -e "bash -c '$HOME/.sofi.sh'"
2023-12-13 12:24:21 -05:00
fi
2023-04-04 20:16:50 -04:00
fi
exit $?
fi
windows() {
if ! (($IS_X11)); then
2023-12-13 12:24:21 -05:00
gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.List | cut -c 3- | rev | cut -c4- | rev | jq '.[] | .id' | while read id; do
echo "win" "$(gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.GetTitle $id | cut -c 3- | rev | cut -c4- | rev)" "|" $id
done
else
xprop -root _NET_CLIENT_LIST |
pcregrep -o1 '# (.*)' |
sed 's/, /\n/g' |
xargs -I {} -n1 xprop -id {} _NET_WM_NAME |
grep -v '"Desktop"\|"xfce4-panel"' |
2023-12-13 12:26:39 -05:00
sed 's/_NET_WM_NAME(UTF8_STRING) = "/win /' |
sed 's/"$//' |
grep -v 'not found'
fi
2023-04-04 20:16:50 -04:00
}
function read_app {
echo "app $(grep '^Name=' "$1" | grep -Eiv "$IGNORED_APPS" | sed 's/Name=//' | head -n 1) <= $1"
}
2023-04-04 20:16:50 -04:00
read_apps() {
export -f read_app
export APP_LOCATIONS
export IGNORED_APPS
if [[ "$1" == "--now" ]]; then
# shellcheck disable=SC2086
find $APP_LOCATIONS -name '*.desktop' -exec bash -c 'read_app "{}"' \;
return $?
fi
cat "$CACHE_FILE"
nohup find $APP_LOCATIONS -name '*.desktop' -exec bash -c 'read_app "{}"' \;
2023-04-04 20:16:50 -04:00
}
2023-12-13 12:24:21 -05:00
read_snap_apps() {
if command -v snap &> /dev/null; then
snap list | tail -n +2 | awk '{print "snp " $1}'
fi
2023-12-13 12:24:21 -05:00
}
2024-02-24 12:04:44 -05:00
apps() {
read_apps $1
read_snap_apps $1
2024-02-24 12:04:44 -05:00
}
2024-03-17 19:56:50 -04:00
RECENTS="$HOME/.sofi-recent"
recent() {
test -f "$RECENTS" && cat "$RECENTS"
}
add-recent() {
if grep "$1" "$RECENTS"; then
return
fi
echo "$1" >> "$RECENTS"
2024-03-17 20:00:38 -04:00
grep -v "^$" "$RECENTS" | tail -n 10 "$RECENTS" > "$RECENTS.temp"
2024-03-17 19:56:50 -04:00
mv "$RECENTS.temp" "$RECENTS"
}
2023-04-04 20:16:50 -04:00
open_app() {
desktop_file="$(echo "$1" | sed 's/app .*<= *//')"
setsid exo-open "$desktop_file" >/dev/null 2>&1 </dev/null &
2023-04-04 20:16:50 -04:00
}
list_files() {
find $FILE_LOCATIONS -maxdepth 1 -not -name '.*' |
2023-04-04 20:16:50 -04:00
sed "s@$HOME@\~@" |
sed 's/^/fil /'
}
paste_emoji() {
emoji="$(echo -n "$1" | cut -c 5- | sed 's/ .*//')"
nohup xclip -selection clipboard <(echo -n $emoji) &> /dev/null &
}
system_info() {
echo "inf $(date)"
}
print-only() {
cat \
<(system_info) \
<(windows) \
<($HOME/.rofi-list-git-repos.py --no-icon) \
<(apps $1) \
<(list_files) \
$HOME/.emojis
}
if [[ "$1" == "--print-only" ]]; then
print-only
exit 1
fi
if [[ "$1" == "--update-listings" ]]; then
print-only --now > "$HOME/.sofi-listings"
exit 0
fi
2024-02-24 12:04:44 -05:00
if [[ "$1" == "" ]]; then
choice="$(cat <(recent) "$HOME/.sofi-listings" | fzf --print-query --preview="$HOME/.sofi-preview.sh {} {q}")"
2024-02-24 12:04:44 -05:00
else
choice="$(cat <($1) | fzf --print-query --preview="$HOME/.sofi-preview.sh {} {q}")"
fi
2023-12-13 12:24:21 -05:00
echo
qstring="$(echo "$choice" | head -n 1)"
choice="$(echo "$choice" | tail -n 1)"
mvn_handler() {
qarg="$(echo "$1" | cut -c 5-)"
row_count=10
arr_result="$(curl -s "https://search.maven.org/solrsearch/select?q=$qarg&rows=$row_count&wt=json" | jq '[.response.docs[] | { g, a, latestVersion } ]')"
for i in 0 1 2 3 4 5 6 7 8 9; do
groupId="$(echo -n "$arr_result" | jq -r ".[$i].g")"
artifactId="$(echo -n "$arr_result" | jq -r ".[$i].a")"
latestVersion="$(echo -n "$arr_result" | jq -r ".[$i].latestVersion")"
echo "mvr $groupId:$artifactId $latestVersion"
done | fzf --print-query --preview="$HOME/.sofi-preview.sh {} {q}"
}
npm_handler() {
qarg="$(echo "$1" | cut -c 5-)"
NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
package="$(npm search $qarg | tail -n +2 | fzf | sed 's/ .*//')"
setsid xdg-open "https://npmjs.com/package/$package" &> /dev/null &
}
2023-04-04 20:16:50 -04:00
2024-03-17 19:56:50 -04:00
add-recent "$choice"
2023-04-04 20:16:50 -04:00
case $choice in
git*)
setsid "$HOME/.rofi-list-git-repos.py" "$choice" &> /dev/null &
2023-04-04 20:16:50 -04:00
;;
win*)
2023-12-13 12:24:21 -05:00
if (($IS_X11)); then
wmctrl -i -R "$(xwininfo -root -tree |
grep "$(echo "$choice" | cut -c 5-)" |
awk '{ print $1 }' |
sed 's/0x/0x00/')"
else
gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/Windows --method org.gnome.Shell.Extensions.Windows.Activate "$(echo "$choice" | sed 's/.* \([0-9]\+\)$/\1/')"
fi
2023-04-04 20:16:50 -04:00
;;
app*)
open_app "$choice"
;;
2023-12-13 12:24:21 -05:00
snp*)
app="$(echo "$choice" | cut -c 5-)"
setsid $app >/dev/null 2>&1 </dev/null &
;;
2023-04-04 20:16:50 -04:00
emj*)
paste_emoji "$choice"
;;
fil*)
setsid xdg-open "$(echo $choice | cut -c 5- | sed "s@~@$HOME@")" &> /dev/null &
2023-04-04 20:16:50 -04:00
;;
2023-12-13 12:24:21 -05:00
time*)
;;
2023-12-13 12:24:21 -05:00
*)
case $qstring in
mvn*)
lib="$(mvn_handler "$qstring")"
lib="$(echo "$lib" | cut -c 5-)"
groupId="$(echo -n "${lib//[$'\t\r\n']}" | sed 's/\(.*\):.*/\1/')"
artifactId="$(echo -n "${lib//[$'\t\r\n']}" | sed 's/.*:\(.*\) .*/\1/')"
latestVersion="$(echo -n "${lib//[$'\t\r\n']}" | sed 's/.* \(.*\)/\1/')"
xml="$(cat <<EOF
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${latestVersion}</version>
</dependency>
EOF
)"
if (($IS_X11)); then
nohup xclip -selection clipboard <(echo -n "$xml") &> /dev/null &
else
wl-copy "$xml"
fi
echo "Copied to clipboard!"
sleep 0.5
;;
npm*)
lib="$(npm_handler "$qstring")"
lib="$(echo "$lib" | cut -c 5-)"
;;
esac
;;
2023-04-04 20:16:50 -04:00
esac
2023-12-13 12:24:21 -05:00
2023-04-04 20:16:50 -04:00
sleep 0.1