Add Kavita Sort desktop entry and initial zsh function documentation

This commit is contained in:
2026-02-07 14:57:32 +01:00
parent b19e2b5f44
commit 179c61a5b6
3 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=inode/directory;
Actions=kavitaSort;
[Desktop Action kavitaSort]
Name=Kavita Sort
Icon=folder-sync
Exec=konsole --workdir %f -e zsh -ic 'kavita_sort'

11
KavitaSorter/idea.md Normal file
View File

@@ -0,0 +1,11 @@
Create an zsh function, it should in the current folder
when there is no subfolder
- create an folder named Chapter #1
- move all files and only files to the newly created folder
when there is already an folder with naming Pattern Chapter#{number}
- increment the {number} in the Foldername by 1 and create an new folder named Chapter #[newnumber}
- move all Files and only files to the newly created folder, do not touch files in other folders then the current one

74
KavitaSorter/solution.md Normal file
View File

@@ -0,0 +1,74 @@
# Kavita Sorter Zsh Function
This zsh function helps organize files in the current directory into `Chapter #N` folders. It automatically detects existing chapter folders and creates the next sequential one.
```zsh
function kavitasort {
local chapter_dir_prefix="Chapter #"
local next_chapter_num=1
local existing_chapter_nums=()
# Find existing "Chapter #" directories and extract their numbers
for dir in "${chapter_dir_prefix}"*(N); do
if [[ -d "$dir" && "$dir" =~ "${chapter_dir_prefix}"([0-9]+) ]]; then
existing_chapter_nums+=("${MATCH[1]}")
fi
done
# Determine the next chapter number
if (( ${#existing_chapter_nums[@]} > 0 )); then
# Sort numbers in descending order to easily find the max
IFS=$'\n' existing_chapter_nums=($(sort -nr <<<"${existing_chapter_nums[*]}"))
unset IFS
next_chapter_num=$(( ${existing_chapter_nums[1]} + 1 ))
fi
local new_chapter_dir="${chapter_dir_prefix}${next_chapter_num}"
# Create the new chapter directory if it doesn't exist
if [[ ! -d "$new_chapter_dir" ]]; then
echo "Creating directory: $new_chapter_dir"
mkdir "$new_chapter_dir"
else
echo "Directory $new_chapter_dir already exists. This should not happen with sequential numbering. Aborting."
return 1
fi
# Move all files (and only files) from the current directory into the new chapter directory
local files_moved=0
for file in *; do
if [[ -f "$file" ]]; then # Check if it's a regular file
echo "Moving '$file' to '$new_chapter_dir/'"
mv "$file" "$new_chapter_dir/"
files_moved=$((files_moved + 1))
fi
done
if (( files_moved > 0 )); then
echo "Moved $files_moved files to $new_chapter_dir."
else
echo "No files found to move in the current directory."
fi
}
```
### Usage:
1. **Add to your `.zshrc`:**
To make this function permanently available, add the code block above to your `~/.zshrc` file.
After adding, run `source ~/.zshrc` or open a new terminal session.
2. **Run the function:**
Navigate to the directory where your comic/manga files are located and simply run:
```bash
kavitasort
```
### How it works:
* It first scans the current directory for any folders starting with "Chapter #".
* It extracts the numerical part from these folder names (e.g., "1" from "Chapter #1").
* If no such folders exist, it defaults to creating "Chapter #1".
* If they exist, it finds the highest number and increments it by one to create the next chapter folder (e.g., if "Chapter #1" and "Chapter #2" exist, it will create "Chapter #3").
* It then iterates through all items in the current directory and moves only the regular files (ignoring subdirectories) into the newly created chapter folder.
* It includes checks to ensure the new directory is created and provides feedback on moved files.