Skip to content

Commit 0c645c0

Browse files
committed
First version of muxtab
1 parent fe95250 commit 0c645c0

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

muxtab.tmux

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
11
#!/bin/sh
2+
3+
# shellcheck source-path=SCRIPTDIR/src
4+
CURRENT_DIR=$(dirname "$(readlink -f "$0")")
5+
SCRIPTS_PATH="$CURRENT_DIR/src"
6+
. "$SCRIPTS_PATH/helpers.sh"
7+
8+
timer="#($SCRIPTS_PATH/timer.sh)"
9+
10+
set_tmux_option "@muxtab_file" "$HOME/.muxtab"
11+
12+
set_tmux_option "status-left" "$timer$(get_tmux_option "status-left" "")"

src/helpers.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/sh
2+
3+
# Copyright (C) Bruno Sutic
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining
6+
# a copy of this software and associated documentation files (the "Software"),
7+
# to deal in the Software without restriction, including without limitation
8+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
# and/or sell copies of the Software, and to permit persons to whom the
10+
# Software is furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included
13+
# in all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19+
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20+
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
21+
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
#
23+
# Source: https://github.com/tmux-plugins/tmux-continuum/
24+
#
25+
# Modifications:
26+
# - compatitibility with POSIX sh
27+
28+
get_tmux_option() {
29+
option="$1"
30+
default_value="$2"
31+
option_value=$(tmux show-option -gqv "$option")
32+
if [ -z "$option_value" ]; then
33+
echo "$default_value"
34+
else
35+
echo "$option_value"
36+
fi
37+
}
38+
39+
set_tmux_option() {
40+
option="$1"
41+
value="$2"
42+
tmux set-option -gq "$option" "$value"
43+
}
44+
45+
# multiple tmux server detection helpers
46+
47+
current_tmux_server_pid() {
48+
echo "$TMUX" |
49+
cut -f2 -d","
50+
}
51+
52+
all_tmux_processes() {
53+
# ignores `tmux source-file .tmux.conf` command used to reload tmux.conf
54+
user_id=$(id -u)
55+
ps -u "$user_id" -o "command pid" "^tmux" -v "^tmux source"
56+
}

src/timer.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/sh
2+
3+
# shellcheck source-path=SCRIPTDIR
4+
CURRENT_DIR=$(dirname "$(readlink -f "$0")")
5+
SCRIPTS_PATH="$CURRENT_DIR"
6+
. "$SCRIPTS_PATH/helpers.sh"
7+
8+
LOCKFILE="/tmp/muxtab.timer.lock"
9+
10+
main() {
11+
if [ -e ${LOCKFILE} ] && kill -0 "$(cat ${LOCKFILE})" 2>/dev/null; then
12+
# another timer is running
13+
exit 0
14+
fi
15+
16+
trap 'rm -f ${LOCKFILE}; exit 0' INT TERM EXIT
17+
echo $$ >${LOCKFILE}
18+
19+
current_time=$(date +%s)
20+
21+
muxtab_file=$(get_tmux_option "@muxtab_file" "$HOME/.muxtab")
22+
23+
if [ ! -e "$muxtab_file" ]; then
24+
# no muxtab file found
25+
exit 0
26+
fi
27+
28+
# loop through the muxtab file
29+
while read -r line; do
30+
# remove leading whitespaces
31+
cleaned_line=$(echo "$line" | sed -e 's/^[[:space:]]*//')
32+
# skip empty lines
33+
if [ -z "$cleaned_line" ]; then
34+
continue
35+
fi
36+
37+
# skip comments
38+
case "$cleaned_line" in
39+
\#*) continue ;;
40+
esac
41+
42+
# split line into interval and command
43+
set -- "$cleaned_line"
44+
interval=$(echo "$cleaned_line" | awk '{print $1}')
45+
command=$(echo "$cleaned_line" | awk '{$1=""; print $0}' | sed -e 's/^[[:space:]]*//')
46+
47+
# check if the command is due to be run
48+
hash=$(echo "$command" | sha256sum | cut -d' ' -f1)
49+
if [ -e "/tmp/muxtab.timer.$hash" ]; then
50+
last_time=$(cat "/tmp/muxtab.timer.$hash")
51+
if [ $((last_time + interval)) -gt "$current_time" ]; then
52+
# not due yet
53+
continue
54+
fi
55+
fi
56+
57+
# run command
58+
echo "$current_time" >"/tmp/muxtab.timer.$hash"
59+
eval "$command" &
60+
61+
done <"$muxtab_file"
62+
63+
rm -f ${LOCKFILE}
64+
}
65+
66+
main

0 commit comments

Comments
 (0)