From b5753d1453a977c4000d6f01bb84beea1025acee Mon Sep 17 00:00:00 2001 From: Chris Toph Date: Wed, 16 Apr 2025 04:14:07 -0400 Subject: [PATCH] Refactor commands and add archive utilities - Replaces direct chafa call with nix run invocation - Implements zipz and unzipz for tar/zstd compression and extraction - Enhances error handling and usage feedback --- .../common/core/fastfetch/scripts/gen.fish | 2 +- home/toph/common/core/fish/init.fish | 85 +++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/home/toph/common/core/fastfetch/scripts/gen.fish b/home/toph/common/core/fastfetch/scripts/gen.fish index 457839c..e438182 100644 --- a/home/toph/common/core/fastfetch/scripts/gen.fish +++ b/home/toph/common/core/fastfetch/scripts/gen.fish @@ -21,7 +21,7 @@ for i in (seq 1 $num) set type2 $types[$j] set combination "$type1+$type2" echo "Creating with type $combination" - chafa -s 24x11 -w 9 --symbols $combination --view-size 24x11 $input_png + nix run nixpkgs#chafa -- -s 24x11 -w 9 --symbols $combination --view-size 24x11 $input_png # chafa -s 23x12 -w 9 --stretch --symbols $combination --view-size 23x12 $input_png end end diff --git a/home/toph/common/core/fish/init.fish b/home/toph/common/core/fish/init.fish index 1a8274f..55b76ef 100644 --- a/home/toph/common/core/fish/init.fish +++ b/home/toph/common/core/fish/init.fish @@ -79,6 +79,91 @@ function s end end +function zipz + # Ensure exactly two arguments are provided + if test (count $argv) -ne 2 + echo "Usage: bipzstd " + return 1 + end + + set directory $argv[1] + set output_file $argv[2] + + # Verify that the specified directory exists + if not test -d $directory + echo "Error: '$directory' is not a valid directory." + return 1 + end + + # Correct output filename to always end with tar.zst + if not string match -q "*tar.zst" $output_file + # Remove any existing extension, if present + set base (string replace -r '\..*$' '' $output_file) + set output_file "$base.tar.zst" + echo "Output filename corrected to: $output_file" + end + + # Check if the output file already exists to avoid accidental overwrite + if test -f $output_file + echo "Error: Output file '$output_file' already exists. Please remove it or choose another name." + return 1 + end + + # Create a tar archive of the directory and compress it with zstd. + # - The tar command outputs the archive to stdout. + # - zstd compresses it using 4 threads (-T4) and a compression level of 12 (-12). + # - The -c flag forces zstd to write to stdout. + tar cf - $directory | nix run nixpkgs#zstd -- -c -T5 -15 -v > $output_file + + # Check the exit status of the pipeline + if test $status -eq 0 + echo "Compression successful: $output_file" + else + echo "Compression failed." + return 1 + end +end + +function unzipz + # Ensure exactly two arguments are provided + if test (count $argv) -ne 2 + echo "Usage: unzipz " + return 1 + end + + set input_file $argv[1] + set destination $argv[2] + + # Verify that the input file exists + if not test -f $input_file + echo "Error: '$input_file' is not a valid file." + return 1 + end + + # Create the destination directory if it does not exist + if not test -d $destination + mkdir -p $destination + if test $status -ne 0 + echo "Error: Failed to create destination directory '$destination'." + return 1 + end + end + + # Decompress the file: + # - The zstd command (via nix) decompresses the compressed file, + # using the -d flag (decompress) and -c to output to stdout. + # - The decompressed stream is piped to tar to extract its contents into the destination directory. + cat $input_file | nix run nixpkgs#zstd -- -d -c -v | tar xf - -C $destination + + # Check the exit status of the pipeline + if test $status -eq 0 + echo "Decompression successful: files extracted to $destination" + else + echo "Decompression failed." + return 1 + end +end + set fish_greeting # Disable greeting fastfetch