Last active
March 2, 2021 12:21
-
-
Save dhhdev/e4fa9aca12880abdab180f38ffba7c97 to your computer and use it in GitHub Desktop.
Only allow students to access sessions in progress with the read only flag, teachers can create tmux sessions as they wish.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# ~/.tools/tmux-teacher.sh | |
# | |
# Author: Daniel H. Hemmingsen <[email protected]> | |
# Usage: tmux-teacher.sh session-name /path/to/start/from" | |
# | |
TEACHER_GROUP="teacher" | |
create_session() { | |
# Create and detach from new session | |
tmux new-session -s $1 -d | |
# Rename main window to the same as the session | |
tmux rename-window $1 -t $1 | |
# Split the window horizontally 50/50. | |
# The focus will be in the right pane. | |
tmux split-window -h -t $1 | |
# Split the right pane 50/50. | |
# The bottom right pane will now be in focus. | |
tmux split-window -v -t $1 | |
# If path is given, start all panes in given path | |
if [[ $2 ]]; then | |
tmux send-keys -t $1:0.0 'cd '$2'; clear' C-m | |
tmux send-keys -t $1:0.1 'cd '$2'; clear' C-m | |
tmux send-keys -t $1:0.2 'cd '$2'; clear' C-m | |
fi | |
# Focus the first pane | |
tmux select-pane -t $1:.+ | |
# Attach to new session | |
tmux attach -t $1 | |
} | |
# Main script | |
if [[ $1 ]]; then | |
# Check if there is already a session with the given name | |
if tmux has-session -t $1; then | |
if groups $(whoami) | grep -qw "$TEACHER_GROUP"; then | |
# Attach to session as teacher (write and read) | |
tmux attach -t $1 && exit 0 | |
else | |
# Attach to session as a student (read-only) | |
tmux attach -r -t $1 && exit 0 | |
fi | |
fi | |
# No session was found, so let's make one if we are a teacher | |
if groups $(whoami) | grep -qw "$TEACHER_GROUP"; then | |
# Create sesssion and attach to it | |
create_session $1 $2 | |
exit 0 | |
else | |
echo "You need to be a teacher to create a tmux session" | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment