-
Notifications
You must be signed in to change notification settings - Fork 2
/
zc-rename
87 lines (69 loc) · 2.59 KB
/
zc-rename
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
setopt localoptions extendedglob clobber
function __zconvey_usage_zc-rename() {
__zconvey_pinfo2 "Renames current Zsh session, or one given via ID or (old) NAME"
__zconvey_pinfo "Usage: zc-rename [-i ID|-n NAME] [-q|--quiet] [-h|--help] NEW_NAME"
print -- "-h/--help - this message"
print -- "-i ID / --id ID - ID (number) of Zsh session"
print -- "-n NAME / --name NAME - NAME of Zsh session"
print -- "-q/--quiet - don't output status messages"
}
local -A opthash
zparseopts -E -D -A opthash h -help q -quiet i: -id: n: -name: || { __zconvey_usage_zc-rename; return 1; }
integer have_id=0 have_name=0 quiet=0
local id name new_name="$1"
# Help
(( ${+opthash[-h]} + ${+opthash[--help]} )) && { __zconvey_usage_zc-rename; return 0; }
[ -z "$new_name" ] && { echo "No new name given"; __zconvey_usage_zc-rename; return 1; }
# ID
have_id=$(( ${+opthash[-i]} + ${+opthash[--id]} ))
(( ${+opthash[-i]} )) && id="${opthash[-i]}"
(( ${+opthash[--id]} )) && id="${opthash[--id]}"
# NAME
have_name=$(( ${+opthash[-n]} + ${+opthash[--name]} ))
(( ${+opthash[-n]} )) && name="${opthash[-n]}"
(( ${+opthash[--name]} )) && name="${opthash[--name]}"
# QUIET
(( quiet = ${+opthash[-q]} + ${+opthash[--quiet]} ))
if [[ "$have_id" != "0" && "$have_name" != "0" ]]; then
__zconvey_pinfo "Please supply only one of ID (-i) and NAME (-n)"
return 1
fi
if [[ "$have_id" != "0" && ( "$id" != <-> || "$id" = "0" ) ]]; then
__zconvey_pinfo "ID must be numeric, 1..100"
return 1
fi
# Rename via NAME?
if (( $have_name )); then
__zconvey_resolve_name_to_id "$name"
local resolved="$REPLY"
if [ -z "$resolved" ]; then
__zconvey_pinfo "Could not find session named: \`$name'"
return 1
fi
# Store the resolved ID and continue normally,
# with ID as the main specifier of session
id="$resolved"
elif (( $have_id == 0 )); then
id="$ZCONVEY_ID"
fi
__zconvey_resolve_name_to_id "$new_name"
if [ -n "$REPLY" ]; then
__zconvey_pinfo "A session already has target name: \`$new_name' (its ID: $REPLY)"
return 1
fi
if [[ "$id" != <-> || "$id" = "0" ]]; then
__zconvey_pinfo "Bad ID ($id), aborting"
return 1
fi
if [[ "$id" -gt "100" ]]; then
__zconvey_pinfo "Maximum nr of sessions is 100, aborting"
return 1
fi
print ":$new_name:" > "$ZCONVEY_NAMES_DIR"/"$id".name
if (( ${quiet} == 0 )); then
__zconvey_pinfo2 "Renamed session $id to: $new_name"
fi
local ls_after_rename
zstyle -b ":plugin:zconvey" ls_after_rename ls_after_rename || ls_after_rename="no"
[ "$ls_after_rename" = "yes" ] && print && zc-ls
# vim:ft=zsh