-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-gum.sh
executable file
·152 lines (139 loc) · 6.49 KB
/
chat-gum.sh
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env bash
command -v gum >/dev/null 2>&1 || { echo >&2 "This advanced version og GitSlick requires Gum to be installed. Install Gum or use pure bash chat.sh instead."; exit 1; }
WIDTH=99
# https://www.ditig.com/256-colors-cheat-sheet
export GUM_INPUT_CURSOR_FOREGROUND=45 # 51=Cyan1
export GUM_INPUT_PROMPT_FOREGROUND=46 # 45=Turquoise2
BORDER="rounded"
GITSLICK=$(gum style --foreground 13 --bold --background 17 "GitSlick")
WELCOME=$(gum style --background 17 --foreground 250 --width "$WIDTH" --align center --padding 1 --margin 0 --border-background="17" --border $BORDER "Welcome to" "$GITSLICK" "the Git based miniature chat app." )
C1=$(gum style --underline --bold 'Commands:')
git config alias.chatlog "log --reverse --pretty=\"format:%C(green)%aN %C(blue)[%ad]%C(reset)%n%B\" --date=relative"
get_local_branches () {
git branch --format='%(refname:short)'
}
get_remote_branches () {
git for-each-ref --format '%(refname:short)' refs/remotes |
while read ref; do
if ! git symbolic-ref -q "refs/remotes/$ref" > /dev/null; then
echo $ref
fi
done
}
get_branches () {
# help mapfile
# -t Remove a trailing DELIM from each line read (default newline)
# -C callback Evaluate CALLBACK each time QUANTUM lines are read
# -c quantum Specify the number of lines read between each call to CALLBACK
# -O origin Begin assigning to ARRAY at index ORIGIN. The default index is 0
# mapfile -O "${#BRANCHES[@]}" effecively appends to existing array as "${#BRANCHES[@]}" is the length of the current array
mapfile -t BRANCHES < <( get_local_branches ) # if you want the branches that were sent to mapfile in a new array as well
mapfile -t -O "${#BRANCHES[@]}" BRANCHES < <( get_remote_branches ) # if you want the branches that were sent to mapfile in a new array as well
echo "${BRANCHES[@]}"
}
commands() {
local C2 D2 C3 D3 C4 D4 C5 D5 C6 D6 C7 D7 C8 D8 COMMANDS DESCRIBS CMDTABLE
C2=$(gum style --foreground '10' '/help')
D2=$(gum style --italic '(print list of supported commands)')
C3=$(gum style --foreground '14' '/list')
D3=$(gum style --italic '(to list channels)' )
C4=$(gum style --foreground '14' '/switch <channel name>')
D4=$(gum style --italic '(to change #channel)')
C5=$(gum style --foreground '14' '/create <channel name>')
D5=$(gum style --italic '(to create a new #channel)')
C6=$(gum style --foreground '14' '/repeat [n]')
D6=$(gum style --italic '(repeat last n messages - default 5)')
C7=$(gum style --foreground '14' '/post')
D7=$(gum style --italic '(write a post aka. multiline message)')
C8=$(gum style --foreground '9' '/quit or /exit')
D8=$(gum style --italic '(to quit chat)')
COMMANDS=$(gum style --align left --padding "0 1" --margin 0 "$C2" "$C3" "$C4" "$C5" "$C6" "$C7" "$C8")
DESCRIBS=$(gum style --align left --padding "0 1" --margin 0 "$D2" "$D3" "$D4" "$D5" "$D6" "$D7" "$D8")
CMDTABLE=$(gum join --horizontal "$COMMANDS" "$DESCRIBS" )
CMDBOX=$(gum style --width "$WIDTH" --border $BORDER "$CMDTABLE")
#echo "$CMDBOX"
}
commands
welcome() {
gum join --vertical "$WELCOME" "$C1" "$CMDBOX"
gum style --width "$WIDTH" --align left --padding "0 1" --margin "0 0" --border hidden \
"Anything starting with a slash is a command. E.g. $(gum style --foreground '10' '/help')"\
"Anything else is treated as a chat message. It will be commited to current dir git repo and pushed immediately to origin" \
"with $(gum style --bold --foreground 9 "NO") safety checks or error handling."
gum style --width "$WIDTH" --align left --padding "0 1" --margin "0 0" --border $BORDER \
"$(gum style --bold "NOTE"): Currently only really supports single line messages. Behaviour undefined if pasting multiline content." \
"But you can now use the $(gum style --foreground '10' '/post') feature if you want to send multiline content."
# gum style --width "$WIDTH" --align left --padding "0 1" --margin "0 0" --border $BORDER \
# "NOTE: You can use $(gum style --foreground '10' '/repeat') to repeat last messages in current channel."
gum style --width "$WIDTH" --align left --padding "0 1" --margin "0 0" --border hidden \
"You are currently in folder $(gum style --foreground '14' "$(pwd)")" \
"And pushing to $(gum style --foreground '14' "$(git remote get-url origin --push)")"
}
title(){
BASE=$(basename -s .git "$(git config --get remote.origin.url)")
BRANCH=$(git branch --show-current)
TITLE="#$BRANCH @ $BASE"
printf '\033]2;%s\a' "$TITLE"
#printf "\[\e]2;%s\a\]"
}
fetch_print_ff() {
gum spin --spinner dot --title Fetching -- git fetch --quiet
git chatlog "..@{u}"
git merge --ff-only --quiet
}
main_loop() {
while true ; do
while IFS=" " read -r -e -p "$(git branch --show-current)> " cm options; do
fetch_print_ff
echo
if [[ "$cm" = "" ]]; then
:
elif [[ "$cm" = "/quit" || "$cm" = "/q" ]]; then
exit 0
elif [[ "$cm" = "/exit" || "$cm" = "/e" ]]; then
exit 0
elif [[ "$cm" = "/help" || "$cm" = "/h" ]]; then
echo "$CMDBOX"
echo
elif [[ "$cm" = "/switch" || "$cm" = "/s" ]]; then
git switch "$options"
title
echo "Showing last 5 messages in $options"
git chatlog -5
elif [[ "$cm" = "/k" ]]; then
NEWCHAN=$(gum choose --height=10 --selected=$(git branch --show-current) --limit=1 $(get_branches))
echo "Switching to $NEWCHAN"
git switch "$NEWCHAN"
title
echo "Showing last 5 messages in $options"
git chatlog -5
elif [[ "$cm" = "/list" || "$cm" = "/l" ]]; then
echo -e "Existing channels: (use ${GREEN}/switch <channelname>${RC} to change channel"
git branch -a
elif [[ "$cm" = "/create" || "$cm" = "/c" ]]; then
echo "Creating new channel $options"
git switch --quiet --orphan "$options"
git commit --allow-empty --message "Beginning of Channel $options"
git push --set-upstream --quiet origin "$options"
title
elif [[ "$cm" = "/repeat" || "$cm" = "/r" ]]; then
: "${options:=5}"
echo -e "Repeating last $options messages in ${RED}$(git branch --show-current)${RC}"
git chatlog "-$options"
elif [[ "$cm" = "/post" || "$cm" = "/p" ]]; then
git commit --quiet --allow-empty
git chatlog -1
git push --quiet origin
else
message="$cm $options"
echo git commit --quiet --allow-empty --message "$message"
echo git chatlog -1
echo git push --quiet origin
fi
done
done
}
welcome
title
#fetch_print_ff
#main_loop