1
1
Fork 0
mirror of https://github.com/tonydamage/nux-env.git synced 2025-12-11 13:24:28 +01:00
nux-env/bin/mark

197 lines
5.3 KiB
Text
Executable file

#!/usr/bin/env nuxsh
nux.use nux/fs
@prefix fs nux.fs.
@prefix check nux.check.
MARK_DIR_NAME="${MARK_DIR_NAME:-.by}"
MARK_PREFIX=""
## Manages symlinks in closest mark (**.by**) directory, provides functionality to batch create
## them with relative paths.
##
## #Available tasks:
## tag:: <task> <task arguments...>
## Performs specified task in tag namespace (marks prefixed with **tag/**)
@command tag task {
MARK_PREFIX="tag/"
nuxr.run "$task" "$@"
}
## this:: <mark> [marks...]
## Marks **current folder** with specific markers.
## This creates symlinks in **mark** folder pointing to **current folder**.
@command this mark {
nux.log debug "Args $#"
item=$(pwd)
mark_root=$(mark.dir "$item")
mark.mark "$mark_root" "$item" "$MARK_PREFIX$mark"
while [ "$#" -gt 0 ]; do
mark="$1"; shift;
mark.mark "$mark_root" "$item" "$MARK_PREFIX$mark"
done
}
## file:: <file> <marks...>
## Marks **files** with specific **mark**.
## This creates symlinks for files in **mark** folder.
@command file file {
mark_root=$(mark.dir "$pwd")
while [ "$#" -gt 0 ]; do
mark="$1"; shift;
mark.mark "$mark_root" "$file" "$MARK_PREFIX$mark"
done
}
## multiple:: <mark> <files...>
## Marks **files** with specific **mark**.
## This creates symlinks for files in **mark** folder.
@command multiple mark {
pwd=$(pwd)
mark_root=$(mark.dir "$pwd")
while [ "$#" -gt 0 ]; do
item="$1"; shift;
mark.mark "$mark_root" "$pwd/$item" "$MARK_PREFIX$mark"
done
}
## display:: [mark]
## Displays path to current mark folder and displays available marks.
## If **mark** is provided list nested marks.
@command display mark {
mark_root=$(mark.dir $pwd)
prefix="$MARK_PREFIX"
if [ -n "$mark" ]; then
prefix="$MARK_PREFIX$mark/";
fi
echo $mark_root:
(
cd "$mark_root";
for mark in "$prefix"* ; do
echo ${mark#$MARK_PREFIX};
done;
)
}
## mark-to-target:: mark target [suffix]
## Moves non-symlinks to *target* and creates symlinks in mark folder
## This also applies to submarks
@command mark-to-target mark target {
suffix="$1";
nux.log info "Suffix is $suffix"
mark=$(mark.dir $pwd)/$mark
nux.log debug $(mark.dir $pwd) $mark;
find "$mark" -type f | while read file; do
if ! nux.check.file.symlink "$file"; then
name=$(nux.fs.name "$file")
file_mark=$(nux.fs.dirname "$file")
fs:info "$file" Moving to $target/$name
fs:move "$target" "$file"
fs:info "$target/$name" Creating symlink in $file_mark
fs:symlink "$target/$name" "$file_mark" "$name"
fi
done
}
## visual:: [image+]
## Display images using feh and allows adding marks using 1-9 key.
## The list of marks is speficied by environment variable *MARK_TAGS*
##
@command visual {
nux.require feh
marks=${MARK_TAGS:-person woman man selfie}
mark_root="$(nux.fs.path.relative.pwd $(mark.dir "$pwd"))"
#mark_root=$(mark.dir $pwd)
nux.log debug "Mark Root:" $mark_root
actions="";
for mark in $marks; do
((i++))
mark=$MARK_PREFIX$mark
if [ $i -gt 9 ]; then
break;
fi
action="--action$i '[$mark] mkdir -p $mark_root/$mark; ln -svft $mark_root/$mark \$(realpath -Lms --relative-to=$mark_root/$mark %F)'";
actions="$actions $action";
done;
nux.log debug Feh actions "$actions"
nux.eval feh \
--zoom max \
--scale-down \
-g 900x1000 \
-G \
"--action '[keep]echo %F: Next file.'" \
"$actions" \
"--info 'echo %n: %wx%h'" \
--draw-tinted "$@"
}
## unmarked:: mark [file+]
## List files, which are not marked by particular mark
## Note that this assumes current filename is same
## TODO: Add support for different filenames in future
##
@command unmarked mark {
mark_root=$(mark.dir $pwd)
nux.log debug $(mark.dir $pwd) $mark_root;
for path in "$@"; do
nux.log debug "Checking file $path";
name=${path##*/};
marks="$(mark.marks-for-path "$mark_root" "$mark" "$path")"
nux.log debug "$path has '$marks'";
if [ -z "$marks" ]; then
echo $path;
fi
done
}
## unmarked:: mark [file+]
## List files, which are not marked by particular mark
## Note that this assumes current filename is same
## TODO: Add support for different filenames in future
##
@command list {
mark_root=$(mark.dir $pwd)
nux.log debug $(mark.dir $pwd) $mark_root;
for path in "$@"; do
nux.log debug "Checking file $path";
name=${path##*/};
marks="$(mark.marks-for-path "$mark_root" "$mark" "$path")"
for m in ${marks}; do
fs:info "$path" "$m"
done
done
}
@namespace mark. {
function :dir item {
if [ -n "$MARK_DIR" ]; then
echo $MARK_DIR;
else
fs:closest "$MARK_DIR_NAME" "$item"
fi
}
function :mark root item mark {
name=""
if [ -e "$root/.path-names" ]; then
rel_path=$(nux.fs.path.relative "$root/.." "$item");
name=${rel_path//\//-}
fs:info "$item" Creating symlink: $(nux.fs.path.display "$root/$mark/$name")
else
fs:info "$item" Creating symlink in $(nux.fs.path.display "$root/$mark")
fi
fs:symlink "$item" "$root/$mark" "$name"
}
function :marks-for-path root mark path {
name=${path##*/};
find "$root/$mark" -iname "$name" 2> /dev/null | while read mark; do
m="${mark#$root/}"
m="${m%/*}";
echo "$m";
done
}
}