Browse Source

Added updating and recent files

master
= 3 years ago
parent
commit
7921683dd7
1 changed files with 39 additions and 19 deletions
  1. +39
    -19
      mru

+ 39
- 19
mru View File

@@ -8,8 +8,10 @@ XDG_CACHE_HOME="$HOME/.cache"


init() { init() {
mkdir -p $XDG_CACHE_HOME/mru mkdir -p $XDG_CACHE_HOME/mru
file=XDG_CACHE_HOME/mru/$1
create_new "$1" create_new "$1"
mv "$file.new" "$file" mv "$file.new" "$file"
touch $XDG_CACHE_HOME/mru/$file.recent
} }


get_cache_path() { get_cache_path() {
@@ -27,19 +29,37 @@ create_new() {
-path '*Backups/my-plugins*' -o -path '*.sw[po]' \) -printf \ -path '*Backups/my-plugins*' -o -path '*.sw[po]' \) -printf \
'%TY-%Tm-%Td\t%TT\t%p\n' | sort -r > $file'.new' '%TY-%Tm-%Td\t%TT\t%p\n' | sort -r > $file'.new'
} }

update() { update() {
clean
file=$XDG_CACHE_HOME/mru/$1
name="$1" name="$1"
if [ -z "$1" ]; then
name='home'
fi
if [ -z "$1" ]; then name='home'; fi

create_new "$name" create_new "$name"
mv "$file.new" "$file"
rm_recent "$1"
}

# This remove duplicates by deleting files in recent from the main file
rm_recent() {
file=$XDG_CACHE_HOME/mru/$1
if [ -z "$1" ]; then file=$XDG_CACHE_HOME/mru/home; fi
while read line
do
l=$(printf "$line" | cut -f3)
if [ ! -z $l ]; then sed -i "\|.*\t.*\t$l|d" $file; fi
done <$file.recent
} }


output() { output() {
if [ -z "$1" ]; then if [ -z "$1" ]; then
cat $XDG_CACHE_HOME/mru/home; else cat "$XDG_CACHE_HOME/mru/$1";
file1="$XDG_CACHE_HOME/mru/home.recent"; file2="$XDG_CACHE_HOME/mru/home"
else
file1="$XDG_CACHE_HOME/mru/$1.recent"; file2="$XDG_CACHE_HOME/mru/$1"
fi fi

cat $file1; printf "\n"; cat $file2
} }


list() { list() {
@@ -48,25 +68,26 @@ list() {


insert() { insert() {
if [ -z "$1" ]; then printf "No path given\n" 1>&2; exit; fi if [ -z "$1" ]; then printf "No path given\n" 1>&2; exit; fi
cache_name=${1+'home'}
cache_name=$2
if [ -z "$2" ]; then cache_name=home; fi
cache_path=$(get_cache_path "$cache_name") cache_path=$(get_cache_path "$cache_name")

file_path=$(realpath "$1") file_path=$(realpath "$1")

if [ -e "$file_path" ]; then if [ -e "$file_path" ]; then
# sed "0,\|.*\t.*\t$file_path|s|||" $XDG_CACHE_HOME/mru/$cache_name
sed -e "0,\|.*\t.*\t$file_path|d" -e "1s;^;$(date '+%Y-%m-%d%t%T')\t$file_path\n;" <$XDG_CACHE_HOME/mru/$cache_name \
>$XDG_CACHE_HOME/mru/$cache_name.insert$$
mv $XDG_CACHE_HOME/mru/$cache_name.insert$$ $XDG_CACHE_HOME/mru/$cache_name
else
sed "0,\|.*\t.*\t$file_path|d" <$XDG_CACHE_HOME/mru/$cache_name \
>$XDG_CACHE_HOME/mru/$cache_name.insert$$
mv $XDG_CACHE_HOME/mru/$cache_name.insert$$ $XDG_CACHE_HOME/mru/$cache_name
sed -i -e "0,\|.*\t.*\t$file_path|d" $XDG_CACHE_HOME/mru/$cache_name.recent
if [ -s $XDG_CACHE_HOME/mru/$cache_name.recent ]; then
echo "sed starts$(date '+%Y-%m-%d%t%T')\t$file_path\n"
sed -i "1i$(date '+%Y-%m-%d%t%T')\t$file_path" $XDG_CACHE_HOME/mru/$cache_name.recent
echo 'sed ends'
else
printf "$(date '+%Y-%m-%d%t%T')\t$file_path" >$XDG_CACHE_HOME/mru/$cache_name.recent
fi
fi


if [ ! -z $file_path ]; then
sed -i "0,\|.*\t.*\t$file_path|d" $XDG_CACHE_HOME/mru/$cache_name
fi fi
}


clean() {
rm $XDG_CACHE_HOME/mru/*.{insert,new}*
} }


case "$1" in case "$1" in
@@ -75,5 +96,4 @@ case "$1" in
output) output "$2";; output) output "$2";;
list) list "$2";; list) list "$2";;
insert) insert "$2" "$3";; insert) insert "$2" "$3";;
clean) clean;;
esac esac

Loading…
Cancel
Save