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

Updated vfs and mark

This commit is contained in:
Tony Tkáčik 2023-08-24 10:59:53 +02:00
parent 1cc5b48e4f
commit 05c2e0d2e0
2 changed files with 153 additions and 71 deletions

102
bin/mark
View file

@ -2,38 +2,16 @@
nux.use nux/fs
@prefix fs nux.fs.
@prefix check nux.check.
MARK_DIR_NAME=.by
MARK_DIR_NAME="${MARK_DIR_NAME:-.by}"
MARK_PREFIX=""
@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"
}
}
## Manages symlinks in closest mark (**.by**) directory, provides functionality to batch create
## them with relative paths.
##
## #Available tasks:
@namespace task. {
## tag:: <task> <task arguments...>
## Performs specified task in tag namespace (marks prefixed with **tag/**)
@command tag task {
@ -78,9 +56,83 @@ MARK_PREFIX=""
fi
echo $mark_root:
(
cd $(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/))
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 "$@"
}
@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"
}
}

48
bin/vfs
View file

@ -4,18 +4,26 @@ nux.use nux/fs
@prefix fs nux.fs.
VFS_SOURCES_FILE=".vfs.sources"
CURRENT_SOURCES_FILE=$(nux.fs.closest "$VFS_SOURCES_FILE");
if fs:exists "$CURRENT_SOURCES_FILE" {
CURRENT_MOUNT="${CURRENT_SOURCES_FILE%/*}";
declare -gA CURRENT_SOURCES
CURRENT_SOURCES_FILE=""
CURRENT_MOUNT=""
function vfs.init path {
CURRENT_SOURCES_FILE=$(nux.fs.closest "$VFS_SOURCES_FILE" "$path");
vfs.merger.load $CURRENT_SOURCES_FILE;
}
function vfs.merger.load path {
if fs:exists "$path" {
CURRENT_MOUNT="${path%/*}";
nux.log debug "Current VFS mount: ${CURRENT_MOUNT%/*}";
while read name path; do
CURRENT_SOURCES[${name}]="$path"
done < "$CURRENT_SOURCES_FILE"
}
}
function vfs.path name {
echo ${CURRENT_SOURCES[$name]};
@ -31,6 +39,15 @@ function vfs.path.real file {
done
}
function vfs.path.real.all file {
for root in "${CURRENT_SOURCES[@]}" ; do
nux.log trace "Testing $root$file"
if fs:exists "${root}${file}" {
echo ${root}${file};
}
done
}
## list::
## Lists all **mergerfs** based virtual filesystems managed by **vfs** tool
@ -40,7 +57,10 @@ function vfs.path.real file {
## info::
## Displays info about current path
@command info {
@command info path {
: ${path:=.}
nux.log debug "path" $path
vfs.init "$path"
nux.log debug "VFS mount: ${CURRENT_MOUNT}";
echo "path:" $CURRENT_MOUNT;
echo "sources:"
@ -63,6 +83,7 @@ function vfs.path.real file {
@command switch storage {
vfs.init .
target="$(vfs.path "$storage")"
if [ -z "$target" ] {
nux.fatal "$storage does not exists."
@ -101,11 +122,11 @@ function vfs.path.real file {
source_path="${source_path%/}"
nux.log debug " Source: $source_name Path: $source_path";
mount_paths="$mount_paths:${source_path}"
echo "$source_name $source_path" >> "$source_tempfs/.vfs.sources"
echo "$source_name $source_path" >> "$source_tempfs/$VFS_SOURCES_FILE"
done
echo "temp $source_tempfs" >> "$source_tempfs/$VFS_SOURCES_FILE="
echo "temp $source_tempfs" >> "$source_tempfs/$VFS_SOURCES_FILE"
mergerfs_mounts="$source_tempfs:${mount_paths}"
mergerfs_mounts="${source_tempfs}=RO${mount_paths}"
nux.log debug "MergerFS command:" $mergerfs_mounts;
mergerfs "$mergerfs_mounts" "$target"
(cd $target; vfs info )
@ -113,8 +134,17 @@ function vfs.path.real file {
## unmount:: <target>
## Unmounts target VFS filesystem.
@command :unmount target {
vfs.init "$target"
task.info "$target"
fusermount -u "$target"
fs:info "${CURRENT_SOURCES[temp]}" removing temporary metadata.
fs:stage rm -rf "${CURRENT_SOURCES[temp]}"
}
@command path file {
: ${file:=.}
vfs.init "$file"
rooted_path="/$(realpath -m --relative-to="$CURRENT_MOUNT" ${file%/})";
vfs.path.real.all "$rooted_path";
}