-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarmarks
executable file
Β·136 lines (118 loc) Β· 2.71 KB
/
starmarks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/sh
# ============ @author davidk55 ============
# inspired by: https://github.com/jocelynmallon/zshmarks
# ============ UTIL ============
check_if_pos_integer() {
case "$1" in
*[!0123456789]*) return 1 ;;
'') return 1 ;;
*) return 0 ;;
esac
}
# ============ HELP MESSAGE ============
HELP="
-a add π
-d delete π
-j jump to π
"
# ============ SETUP ============
# Create env variables if not exist
if [ -z "$STARMARKS_PATH" ] && [ -z "$STARMARKS_FILE" ]; then
export STARMARKS_PATH="$HOME/.local/share/starmarks/"
export STARMARKS_FILE="saved_starmarks"
fi
# Create directories if they do not exist
if [ ! -d "$STARMARKS_PATH" ]; then
mkdir -p "$STARMARKS_PATH"
fi
# Create file if it do not exist
if [ ! -f "$STARMARKS_FILE" ]; then
touch "$STARMARKS_PATH$STARMARKS_FILE"
fi
# ============ MAIN FUNCTIONS ============
add() {
path_exist=false
if [ "$PATH_TO_ADD" = "c" ]; then
pwd >> "$STARMARKS_PATH$STARMARKS_FILE"
echo a new π was added: current
return
fi
# compare each line with path to add
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
if [ "$LINE" = "$PATH_TO_ADD" ]; then
path_exist=true
fi
done < "$STARMARKS_PATH$STARMARKS_FILE"
if [ "$path_exist" = false ]; then
echo "$PATH_TO_ADD" >> "$STARMARKS_PATH$STARMARKS_FILE"
echo a new π was added: "$PATH_TO_ADD"
else
echo this π already exist: "$PATH_TO_ADD"
fi
}
jump() {
clear
cur_id=1
echo type π id to jump to
echo
echo " ID PATH"
echo "--------------------------------------"
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
echo " $cur_id ${LINE}"
cur_id=$((cur_id+1))
done < "$STARMARKS_PATH$STARMARKS_FILE"
echo
read -r
cur_id=1
clear
if (check_if_pos_integer "$REPLY") then
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
if [ "$cur_id" = "$REPLY" ]; then
cd "${LINE}" || exit
fi
cur_id=$((cur_id+1))
done < "$STARMARKS_PATH$STARMARKS_FILE"
fi
}
delete() {
clear
cur_id=1
echo type π id to delete
echo
echo " ID PATH"
echo "--------------------------------------"
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
echo " $cur_id ${LINE}"
cur_id=$((cur_id+1))
done < "$STARMARKS_PATH$STARMARKS_FILE"
echo
read -r
clear
if (check_if_pos_integer "$REPLY") then
sed -i "$REPLY d" "$STARMARKS_PATH$STARMARKS_FILE"
fi
}
# ============ HANDLE INPUT ============
OPTIND=1
while getopts la:djh arg; do
case "${arg}" in
a)
PATH_TO_ADD=${OPTARG}
add
;;
d)
delete
;;
j)
jump
;;
h)
echo "$HELP"
;;
*)
;;
esac
done
if [ $OPTIND -eq 1 ]; then
echo "$HELP"
fi