Skip to content

Instantly share code, notes, and snippets.

@abzrg
Last active June 14, 2024 00:54
Show Gist options
  • Save abzrg/cf0a61ec6205af2e24cb7dbc33110e19 to your computer and use it in GitHub Desktop.
Save abzrg/cf0a61ec6205af2e24cb7dbc33110e19 to your computer and use it in GitHub Desktop.
Take a Peek at an OpenFOAM Library/Applications
#!/bin/sh -e
# Take a Peek at an OpenFOAM Library/Application
#
# Usage: `foampeek [-l|--lucky] [-p|--project lib/app] [-f|--file file] [lib/app] [file]`
#
# Firstly, it prompts you to select a library (in `$FOAM_SRC`) or an application
# (in `$FOAM_APP`), and finally, it lists all the files in that lib/app for you
# to select.
#
# Optional arguments can be passed:
# 1. name of the lib/app (w/ or w/o -p|--project)
# 2. name of the file in that lib/app (w/ or w/o -f|--file)
# 3. I'm feeling lucky today (-l|--lucky)
#
# You can select more than one file to view in the editor (See the
# key bindings bellow).
#
# By default, it will look for the `EDITOR` environment
# variable. You can set this environment variable in your shell config (either
# in `.profile`, `.bash_profile`, `.zprofile`, `.bashrc`, or `.zshrc`) to your
# favroite editor.
#
# Bindings:
# - Tab: select the current item
# - Shift-Tab: de-select the current item
# - C-A: select all
# - C-D: de-select all
# - C-T: toggle selecting/de-selecting all
#
# Examples:
# $ foampeek
# $ foampeek meshtofoam
# $ foampeek icof createfie
# $ foampeek --file spheredrag --project lagrang
# $ foampeek -p 'lagrang intermed' -f spheredrag
# $ foampeek icofoam .C` -l
# $ foampeek --lucky pimpleFoam .C
#
# Todo:
# - It's a hack. improve readability.
# - unify test commands
# - think about best_match case
# - the case where only one query (the project) is provided
# - the case when -l is provided but not query (only -l is provided)
# - handle error codes. whether there was no match or the user simply hit esc/Ctrl-c.
usage() {
printf "%s" "Usage: `basename $0` [-l | --lucky] [-p | --project=<lib-or-app-query>] [-f|--file=<file-query>] [lib-or-app-query] [file-query]"
exit 1;
}
test -z $WM_PROJECT_DIR && { echo "OpenFOAM env is not loaded." && exit 1 ;}
# Parse arguments
best_match="tee"
while [ $# -gt 0 ]; do
case $1 in
-l | --lucky)
# I'm feeling lucky. Be non-interactive and hope for the best.
shift
opt_select_one_match='--select-1 --exit-0'
# To get the best match
best_match='tail -n 1'
opt_filter="--filter='"
;;
-p | --project | --project=)
# project: an application or a library
shift
project_query="$1"
[ -z $opt_filter ] || opt_filter_project="$opt_filter$project_query"
;;
-f | --file | --file=)
# file: a file in the project
shift
file_query="$1"
[ -z $opt_filter ] || opt_filter_file="$opt_filter$file_query"
;;
-* | --*)
printf "%s" "Wrong arg $1\n"
usage
;;
*)
# No more than 2 non-option args at this point
if [ $# -gt 2 ]; then
printf \
"%s%s%s" \
"Wrong number of arguments. Two optional args is needed," \
" but $(($# - 2)) extra args are provided.\n" \
&& echo; usage
fi
test -n "$1" && {
project_query="$1" && shift
[ -z $opt_filter ] || opt_filter_project="$opt_filter$project_query"
}
test -n "$1" && {
file_query="$1" && shift
[ -z $opt_filter ] || opt_filter_file="$opt_filter$file_query"
}
;;
esac
done
# Select the library or application
# Any directory that has a `Make` folder in it counts.
# src: path to the library/application
project=$( \
find $FOAM_SRC $FOAM_APP -type d -path "*Make" \
| sed -e 's#/Make$##' \
| sk $opt_filter_project $opt_select_one_match --query "$project_query" \
--delimiter '/' --with-nth '6..' --nth='-1' \
--preview 'tree --gitignore -I lnInclude -L 2 -C {}' \
--preview-window 'up:70%:border' \
| $best_match)
# In case user cancels operations at this point by pressing Escape
test -z "$project" && { echo "No library or application was found." && exit 0 ;}
# Select file or files in the selected code.
#
# To get LSP (clangd) detecting the project setup, I need to cd into the
# `$project`'s directory. Thus, a subshell is spawned to preserve the old working
# directory.
( cd $project
# --delimiter '/' --with-nth='6..'
# /home/user/OpenFOAM/OpenFOAM-x/src/...
# 12 3 4 5 6 7
files=$( \
find $project -type f -not \( \
-path "*.cache*" -o \
-path "*.json*" -o \
-path "*.vscode*" -o \
-path "*.git*" -o \
-path "*$WM_OPTIONS" \
\) \
| sk $opt_filter_file $opt_select_one_match --multi --query "$file_query" \
--delimiter '/' --with-nth='6..' --nth='-1' \
--bind 'ctrl-a:select-all,ctrl-d:deselect-all,ctrl-t:toggle-all' \
--preview 'test -n {} && nl -ba {}' --preview-window 'up:75%' \
--header='CTRL-A: select all; CTRL-D: de-select-all; CTRL-T: toggle' \
| $best_match)
# Edit in the Editor
test -z "$files" && {
echo "No file was found."
exit 1
} || {
case $EDITOR in
vim | nvim)
opts='-R' # Read-Only mode
;;
*)
# "TODO: Other editor configs"
opts=""
;;
esac
$EDITOR $opts $files
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment