Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
István Bozsó committed Jun 10, 2019
1 parent 2fd3659 commit 0c3e348
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 99 deletions.
119 changes: 21 additions & 98 deletions menu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
progs="$HOME/progs"
icons="$progs/utils/icons"

. "$progs/utils/tools/map.sh"

set -e

opt="-fn -adobe-helvetica-bold-r-normal-*-25-180-100-100-p-138-iso8859-1"
Expand Down Expand Up @@ -35,43 +37,24 @@ dprompt() {
}


repos="
repos="\
insar_meteo:$progs/insar_meteo\n
utils:$progs/utils\n
texfiles:/home/istvan/Dokumentumok/texfiles
"

key() {
printf "%s\n" | awk -F ':' '{print $NF}'
}

value() {
printf "%s\n" | awk -F ':' '{print $1}'
}

get_name() {
awk -F ':' '{print $1}'
}

get_path() {
awk -F ':' '{print $NF}'
}


get_pair() {
for line in $repos; do
case $(echo $line | get_name) in
*$1*)
echo $(echo $line | get_path)
;;
esac
done
dselect() {
check_narg "$#" "1"
local table="$1"

if [ -n "$2" ]; then
echo "$(key "$table" | mymenu -p "$2")"
else
echo "$(key "$table" | mymenu -p)"
fi
}


repo_names=$(echo $repos | get_name)


notify() {
if [ -n "$3" ]; then
notify-send -i "$icons/$3" "$1" "$2" -t 1500
Expand All @@ -83,33 +66,27 @@ notify() {

pull_all() {
local tpl="https://bozso:%s@github.com/bozso"
pwd="$(dpass -p "GitHub password:")"

local pwd="$(dpass -p "GitHub password:")"
local tpl=$(printf "$tpl" $pwd)

for line in $repos; do
local name=$(echo $line | get_name)
local path=$(echo $line | get_path)
# cd
# notify "$path" "github.png"
local name=$(key "$line")
local path=$(value "$line")

[ ! -d "$path" ] && continue
# skip non existent directories
[ ! -d "$path" ] && continue

cd $path
local curr="$tpl/$name"

out=$(git pull $curr)

local out=$(git pull $curr)
notify "Pulling repo $name." "$out" "github.png"
done

}


extract_music() {
for zipfile in /tmp/*.zip; do
local outpath="/home/istvan/Zenék/$(basename "$zipfile" .zip)"
mkdir -p "$outpath"
# mkdir -p "$outpath"
notify "Extracting file" "$zipfile"

unzip "$zipfile" -d "$outpath"
Expand Down Expand Up @@ -153,19 +130,6 @@ playlists() {
}


in_str() {
local reqsubstr="$1"
shift
local string="$@"

if [ -z "${string##*$reqsubstr*}" ]; then
echo "true"
else
echo "false"
fi
}


workspace() {
check_narg $# "2"

Expand All @@ -189,8 +153,8 @@ workspace() {
}

work() {
local sel=$(printf "%s\n" $repo_names | mymenu -p "Select repo:")
local path=$(get_pair $sel)
local sel="$(dselect "$repos" "Select repo:")"
local path="$(mget "$sel" "$repos")"

workspace "$sel" "$path"
}
Expand Down Expand Up @@ -239,43 +203,6 @@ markdown() {
}


git_remote_add() {
check_narg $# 1

git remote add origin "$1"
}


git_push() {
check_narg $# 1

git commit -am "$*"
git push origin master
}


git_manage() {
check_narg $# 1

case $1 in
"stat")
git status
;;
"push")
shift
git_push "$@"
;;
"pull")
git pull origin master
;;
*)
printf "Unrecognized option %s!\n" $1 >&2
return 1
;;
esac
}


shutdown_now() {
dprompt "Did you push all git repositories?" ""
dprompt "Shutdown?" "shutdown -h now"
Expand Down Expand Up @@ -320,10 +247,6 @@ main() {
"markdown")
markdown $2
;;
"git")
shift
git_manage $@
;;
"shutdown")
shutdown_now
;;
Expand Down
47 changes: 47 additions & 0 deletions tools/git.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#! /usr/bin/env sh

check_narg() {
if [ "$1" -lt "$2" ]; then
perr "error: Wrong number of arguments!"
return 1
fi
}

git_remote_add() {
check_narg $# 1

git remote add origin "$1"
}


git_push() {
check_narg $# 1

git commit -am "$*"
git push origin master
}


main() {
check_narg "$#" "1"

case $1 in
"stat")
git status
;;
"push")
shift
echo "$@"
git_push "$@"
;;
"pull")
git pull origin master
;;
*)
printf "Unrecognized option %s!\n" $1 >&2
return 1
;;
esac
}

main "$@"
44 changes: 44 additions & 0 deletions tools/map.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# hash() {
# echo $((0x$(sha1sum <<<"$1")0))
# }


in_str() {
local reqsubstr="$1"
shift
local string="$@"

if [ -z "${string##*$reqsubstr*}" ]; then
echo "true"
else
echo "false"
fi
}


key() {
if [ -n "$1" ]; then
printf "%s" "$1" | awk -F ':' '{print $1}'
else
printf "%s" | awk -F ':' '{print $1}'
fi
}

value() {
if [ -n "$1" ]; then
echo "$(printf "%s\n" "$1" | awk -F ':' '{print $NF}' | tr -d '\n')"
else
echo "$(printf "%s\n" | awk -F ':' '{print $NF}' | tr -d '\n')"
fi
}

mget() {
local key="$1"
local table="$2"

for line in $table; do
if [ "$(in_str "$key" "$line")" = "true" ]; then
echo "$(value "$line")"
fi
done
}
2 changes: 1 addition & 1 deletion utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tar_ext() {


gm() {
sh $UTILS_DIR/menu.sh git $*
sh $UTILS_DIR/tools/git.sh $*
}


Expand Down

0 comments on commit 0c3e348

Please sign in to comment.