Replace project searcher with faster python script

Add a tiny suspend action.
This commit is contained in:
Sage Vaillancourt 2023-02-19 18:10:23 -05:00
parent 6c464b87df
commit 911303cb34
5 changed files with 96 additions and 65 deletions

View File

@ -7,7 +7,8 @@ configuration {
emoji,
"tabs:/home/sage/.rofi-tabs.sh",
// "history:/home/sage/.rofi-history-search.sh",
"projects:/home/sage/.rofi-list-git-repos.sh"
"projects:/home/sage/.rofi-list-git-repos.py",
"suspend:/home/sage/.rofi-suspend.sh"
];
show-icons: true;
drun {
@ -22,6 +23,9 @@ configuration {
projects {
display-name: "";
}
suspend {
display-name: "";
}
tabs {
display-name: "";
fallback-icon: "firefox-beta";

View File

@ -7,10 +7,14 @@ function duplicate_history_file() {
cp $PLACES_FILE $WORKING_PLACES
}
if [[ "$1" == "" ]] && [[ "1" == "$(( (`date +%s` - `stat -L --format %Y $CACHE_FILE.sqlite`) > (30*60) ))" ]]; then
if [[ "$1" == "" ]]; then
if [[ "1" == "$(( (`date +%s` - `stat -L --format %Y $CACHE_FILE.sqlite`) > (30*60) ))" ]]; then
duplicate_history_file
sqlite3 "$WORKING_PLACES" "SELECT datetime(a.visit_date/1000000,'unixepoch') AS visit_date, b.title, b.url FROM moz_historyvisits AS a JOIN moz_places AS b ON a.place_id=b.id WHERE 1 ORDER BY a.visit_date DESC LIMIT 10000;" > "$CACHE_FILE"
fi
cat "$CACHE_FILE"
else
fi
if [[ "$1" != "" ]]
firefox-beta-bin "$(grep "$1" "$CACHE_FILE" | head -n 1 | sed 's/.*|//g')"
fi

View File

@ -0,0 +1,76 @@
#!/usr/bin/python
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import os
import subprocess
import sys
home = os.environ['HOME']
git_dirs = [home + "/work", home + "/projects", home + "/git", home + "/WebStormProjects", home + "/RiderProjects"]
icons = {}
def get_application_desktop_file_info(application: str, prefix: str) -> str:
path = f"{home}/.local/share/applications/jetbrains-{application}.desktop"
with open(path, 'r') as file:
for line in file.readlines():
if line.startswith(prefix):
return line
def get_project_type(project) -> str:
for file in os.listdir(project):
if file.endswith("pom.xml"):
return "idea"
if file.endswith("package.json"):
return "webstorm"
if file.endswith("cargo.toml") or file.endswith(".c"):
return "clion"
if file.endswith(".csproj"):
return "rider"
if file.endswith(".py"):
return "pycharm"
if file.endswith(".go"):
return "goland"
return "idea"
def get_icon(project: str) -> str | None:
project_type = get_project_type(project)
if project_type is None:
return None
icon_line = get_application_desktop_file_info(project_type, 'Icon')
return icon_line[5:].rstrip()
def open_project(project: str) -> ():
project_type = get_project_type(project)
if project_type is None:
return
command = get_application_desktop_file_info(project_type, 'Exec')[6:-5]
subprocess.Popen([command, project], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def print_project_message(project: str) -> ():
# exe=$(grep "Exec" "$HOME/.local/share/applications/jetbrains-$application.desktop" | sed 's/.*"\([^"]*\)".*/\1/')
icon = get_icon(project)
if icon is None:
return
print(f'{project}\0icon\x1f{get_icon(project)}')
if __name__ == '__main__':
if len(sys.argv) == 1:
for git_dir in git_dirs:
if not os.path.isdir(git_dir):
continue
for project in os.listdir(git_dir):
project = f'{git_dir}/{project}'
if os.path.isdir(project):
print_project_message(project)
else:
open_project(sys.argv[1])

View File

@ -1,60 +0,0 @@
#!/bin/bash
GIT_DIRS=("$HOME/work" "$HOME/projects" "$HOME/git" "$HOME/WebStormProjects" "$HOME/RiderProjects")
function open_app() {
application="$1"
project="$2"
exe=$(grep "Exec" "$HOME/.local/share/applications/jetbrains-$application.desktop" | sed 's/.*"\([^"]*\)".*/\1/')
$exe "$project" &> /dev/null &
}
function get_icon {
application=$1
grep "Icon" "$HOME/.local/share/applications/jetbrains-$application.desktop" | sed 's/Icon=//'
}
function file_exists() {
project="$1"
file="$2"
files=($project/$file)
if [ -e "${files[0]}" ]; then
return 0
else
return 1
fi
}
function get_project_type() {
project="$1"
if file_exists "$project" "pom.xml"; then
echo "idea"
elif file_exists "$project" "package.json"; then
echo "webstorm"
elif file_exists "$project" "cargo.toml" || file_exists "$project" "*.c"; then
echo "clion"
elif file_exists "$project" "*.csproj"; then
echo "rider"
elif file_exists "$project" "*.py"; then
echo "pycharm"
elif file_exists "$project" "*.go"; then
echo "goland"
else
echo "idea"
fi
}
if [[ "$1" == "" ]]; then
for dir in $(echo ${GIT_DIRS[*]}); do
if [ -e "$dir" ]; then
projects=$(find $dir -maxdepth 1 -mindepth 1 -type d)
for project in $projects; do
echo -en "$project\0icon\x1f$(get_icon "$(get_project_type "$project")")\n"
done
fi
done
else
open_app "$(get_project_type "$1")" "$1"
fi

7
fake_home/.rofi-suspend.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
if [[ "$1" == "" ]]; then
echo "SUSPEND"
else
systemctl suspend
fi