Skip to content

Commit

Permalink
feat: new tools
Browse files Browse the repository at this point in the history
  • Loading branch information
sioodmy committed Oct 14, 2023
1 parent f9c10a6 commit b35dc1f
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 1 deletion.
116 changes: 116 additions & 0 deletions home/cli/tools/compilec.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
_: ''
# Function to compile and run a single .cpp file
function compile_and_run_file() {
filename="$1"
basename="''${filename%.*}"
echo "Compiling $filename..."
g++ -o "$basename" "$filename"
if [ $? -eq 0 ]; then
echo "Running $basename..."
"./$basename"
else
echo "Compilation failed."
fi
}
# Function to prompt user to choose a .cpp file using skim or fzf
function choose_cpp_file() {
directory="$1"
if command -v skim >/dev/null 2>&1; then
file=$(find "$directory" -maxdepth 1 -type f -name "*.cpp" | skim --ansi --query "")
elif command -v fzf >/dev/null 2>&1; then
file=$(find "$directory" -maxdepth 1 -type f -name "*.cpp" | fzf)
else
echo "Error: skim or fzf is required for file selection."
exit 1
fi
if [ -n "$file" ]; then
compile_and_run_file "$file"
else
echo "No .cpp file selected."
fi
}
# Function to prompt user to choose a .cpp file recursively using skim or fzf
function choose_cpp_file_recursive() {
directory="$1"
if command -v skim >/dev/null 2>&1; then
file=$(find "$directory" -type f -name "*.cpp" | sk --ansi --query "")
elif command -v fzf >/dev/null 2>&1; then
file=$(find "$directory" -type f -name "*.cpp" | fzf)
else
echo "Error: skim or fzf is required for file selection."
exit 1
fi
if [ -n "$file" ]; then
compile_and_run_file "$file"
else
echo "No .cpp file selected."
fi
}
# Help menu
function display_help() {
echo "Usage: $0 [options] <file/directory>"
echo "Options:"
echo " --recursive Look for .cpp files recursively"
echo " --help Display this help menu"
echo
echo "Examples:"
echo " $0 ~/Dev/test.cpp"
echo " $0 ~/Dev"
echo " $0 ~/Dev --recursive"
}
# Parse command line arguments
recursive=false
directory=""
# Check if --help is passed
if [[ "$1" == "--help" ]]; then
display_help
exit 0
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--recursive)
recursive=true
shift
;;
*)
directory="$1"
shift
;;
esac
done
# Check if directory is provided
if [ -z "$directory" ]; then
echo "Error: No directory specified."
display_help
exit 1
fi
# Check if directory exists
if [ ! -d "$directory" ]; then
echo "Error: Directory does not exist."
display_help
exit 1
fi
# Compile and run or display help menu
if [ -f "$directory" ]; then
compile_and_run_file "$directory"
elif [ "$recursive" = true ]; then
choose_cpp_file_recursive "$directory"
else
choose_cpp_file "$directory"
fi
''
33 changes: 32 additions & 1 deletion home/cli/tools/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,38 @@
while inotifywait -e close_write $1; do pandoc $@; done
'';
in {
home.packages = with pkgs; [ocrScript texlive pandoc pandoc-watch];
home = {
sessionPath = [
"${config.home.homeDirectory}/.local/bin"
];

file = {
".local/bin/updoot" = {
# Upload and get link
executable = true;
text = import ./updoot.nix {inherit lib pkgs;};
};

".local/bin/preview" = {
# Preview script for fzf tab
executable = true;
text = import ./preview.nix {inherit lib pkgs;};
};

".local/bin/extract" = {
# Extract the compressed file with the correct tool based on the extension
executable = true;
text = import ./extract.nix {inherit lib pkgs;};
};

".local/bin/compilec" = {
executable = true;
text = import ./compilec.nix {};
};
};

packages = with pkgs; [ocrScript texlive pandoc pandoc-watch];
};
services = {
udiskie.enable = true;
gpg-agent = {
Expand Down
52 changes: 52 additions & 0 deletions home/cli/tools/extract.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
lib,
pkgs,
...
}: ''
SAVEIFS=$IFS
IFS="$(printf '\n\t')"
function extract {
if [ $# -eq 0 ]; then
# display usage if no parameters given
echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz|.zlib|.cso>"
echo " extract <path/file_name_1.ext> [path/file_name_2.ext] [path/file_name_3.ext]"
fi
for n in "$@"; do
if [ ! -f "$n" ]; then
echo "'$n' - file doesn't exist"
return 1
fi
case "''${n%,}" in
*.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
tar zxvf "$n" ;;
*.lzma) unlzma ./"$n" ;;
*.bz2) bunzip2 ./"$n" ;;
*.cbr|*.rar) unrar x -ad ./"$n" ;;
*.gz) gunzip ./"$n" ;;
*.cbz|*.epub|*.zip) unzip ./"$n" ;;
*.z) uncompress ./"$n" ;;
*.7z|*.apk|*.arj|*.cab|*.cb7|*.chm|*.deb|*.iso|*.lzh|*.msi|*.pkg|*.rpm|*.udf|*.wim|*.xar|*.vhd)
7z x ./"$n" ;;
*.xz) unxz ./"$n" ;;
*.exe) cabextract ./"$n" ;;
*.cpio) cpio -id < ./"$n" ;;
*.cba|*.ace) unace x ./"$n" ;;
*.zpaq) zpaq x ./"$n" ;;
*.arc) arc e ./"$n" ;;
*.cso) ciso 0 ./"$n" ./"$n.iso" && \
extract "$n.iso" && \rm -f "$n" ;;
*.zlib) zlib-flate -uncompress < ./"$n" > ./"$n.tmp" && \
mv ./"$n.tmp" ./"''${n%.*zlib}" && rm -f "$n" ;;
*.dmg)
hdiutil mount ./"$n" -mountpoint "./$n.mounted" ;;
*)
echo "extract: '$n' - unknown archive method"
return 1
;;
esac
done
}
IFS=$SAVEIFS''
27 changes: 27 additions & 0 deletions home/cli/tools/preview.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
lib,
pkgs,
...
}: ''
#> Syntax: bash
# Copied from Elkowar
case "$1" in
-*) exit 0;;
esac
case "$(${lib.getExe pkgs.file} --mime-type "$1")" in
*text*)
${lib.getExe pkgs.bat} --color always --plain "$1"
;;
*image* | *pdf)
${lib.getExe pkgs.catimg} -w 100 -r 2 "$1"
;;
*directory*)
${lib.getExe pkgs.eza} --icons -1 --color=always "$1"
;;
*)
echo "unknown file format"
;;
esac
''
16 changes: 16 additions & 0 deletions home/cli/tools/updoot.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
lib,
pkgs,
...
}: ''
#> Syntax: bash
# Send to host
[ -f "$1" ] && op="cat"
''${op:-echo} "''${@:-$(cat -)}" \
| ${lib.getExe pkgs.curl} -sF file='@-' 'http://0x0.st' \
| tee /dev/stderr \
| tr -d '\n' \
| ${pkgs.wl-clipboard}/bin/wl-copy -n
''

0 comments on commit b35dc1f

Please sign in to comment.