Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ Use the -e or --export flag to export the current session configuration to a YAM

- Commands for each pane.

- Note: The export of panes in split is currently not implemented.

### Kill Session or Server

Use the `-k <session-name>` or --kill flag to kill the selected session. To kill the Tmux server and close all sessions use `-k all`
Expand Down Expand Up @@ -112,17 +114,14 @@ sessions:
- command: nvim .
- command: htop
- name: Server
layout: even-vertical
panes:
- command: npm run dev
- command: tail -f logs.txt
command: npm run dev
```
### Supported Layouts
- tiled : Panes are arranged in a grid.
- even-horizontal : Panel are arranged horizontally with equal width.
- even-vertical : Panel are arranged horizontally with equal height.
- main-horizontal : Panel are arranged horizontally with equal height but the first index, this will occupy half of the screen.
- main-vertical : Panel are arranged horizontally with equal width but the first index, this will occupy half of the screen.
- `tiled` : Panes are arranged in a grid.
- `even-horizontal `: Panel are arranged horizontally with equal width.
- `even-vertical `: Panel are arranged horizontally with equal height.
- `main-horizontal` : Panel are arranged horizontally with equal height but the first index, this will occupy half of the screen.
- `main-vertical` : Panel are arranged horizontally with equal width but the first index, this will occupy half of the screen.


## Configuration ⚙️
Expand Down Expand Up @@ -151,10 +150,7 @@ sessions:
- command: nvim .
- command: htop
- name: Server
layout: even-vertical
panes:
- command: npm run dev
- command: tail -f logs.txt
command: npm run dev
```
### Key Fields 🔑
- root: Root directory for the session.
Expand All @@ -169,6 +165,8 @@ sessions:

- panes: (Optional) List of panes for splits. Each pane can have a command. (v0.4.0+)

- Note: Due to how tmux commands works, I suggest to not put dots '.' in the name of windows and panes, because it leads to undefined behaviour that (currently) I cannot fix.

## Roadmap | TODO 🗺️
- [x] Create Session with `zmux <session_name>`
- [x] Assign root directory
Expand All @@ -178,6 +176,7 @@ sessions:
- [x] `zmux` without args list all available sessions opened and in the configuration file
- [x] fzf
- [x] Export the current session into a YAML file for future usage
- [ ] Export working with splits!
- [x] Use multiple files instead of only `config.yaml`
- [x] Configuration Checker
- [x] Preview windows in fzf list
Expand All @@ -188,6 +187,17 @@ sessions:

## Changelog 📜

### [0.4.1] - 2024-03-25

#### Added
- Error Handling: Created a default config because of configuration checks [#4](https://github.com/MrSloth-dev/Zmux/issues/4)

#### Changed
- Prevented some helper functions to be passed for subshells. [#5](https://github.com/MrSloth-dev/Zmux/issues/5)

#### Bugfix
- Fixed Typos.

### [0.4.0] - 2024-01-31

#### Added
Expand Down
Binary file modified assets/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 44 additions & 27 deletions zmux
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# A script to manage and create Tmux Sessions.

set -euo pipefail
VERSION="v0.4.0"
VERSION="v0.4.1"
export CONFIG_DIR="${HOME}/.config/zmux"
export CONFIG_FILE="${HOME}/.config/zmux/config.yaml"

Expand All @@ -14,7 +14,7 @@ EXIT_DEPENDENCY_ERROR=1
EXIT_CONFIG_ERROR=2
EXIT_INVALID_SESSION_NAME=3
EXIT_TMUX_NOT_RUNNING=4
EXIT_IVALID_INPUT=5
EXIT_INVALID_INPUT=5

#Requirements
requirements() {
Expand Down Expand Up @@ -44,14 +44,29 @@ session_exists() {
}

check_config() {
requirements
if [[ ! -d "${CONFIG_DIR}" ]]; then
echo -e "zmux: Error: Config directory not found at '${CONFIG_FILE}'" >&2
read -p "Want to create Config Directory? [Y/n] " yn
case $yn in
[Yy]* )
mkdir -p "${CONFIG_DIR}" && echo -e "Config directory created at '${CONFIG_DIR}'";
touch "${CONFIG_FILE}" && echo -e "Default config file created at '${CONFIG_FILE}'";
cat <<- EOF > "${CONFIG_FILE}"
---
sessions:
default:
root: \$HOME
windows:
- name: Editor
layout: tiled
panes:
- command: nvim .
- command: htop
- name: Compile
command: ls
EOF
# echo -e "${default_file}" >> "${CONFIG_FILE}";

exit "${EXIT_SUCCESS}";;
[Nn]* )
echo -e "zmux: Error: Need to have Config directory";
Expand Down Expand Up @@ -116,13 +131,13 @@ preview() {
if session_exists "${session_name}"; then
echo -e "\nConfigured Windows:"
tmux list-windows -t ${session_name} | while IFS= read -r window; do
window_name=$(echo -e "$window" | awk -F':' '{print $2}' | awk '{print $1}' | tr -d '\*#-')
echo -e "- $window_name"
pane_count=$(tmux list-panes -t "${session_name}:${window_name}" | wc -l)
if [[ $pane_count -gt 1 ]]; then
echo -e " - Contains $pane_count panes"
fi
done
window_name=$(echo -e "$window" | awk -F':' '{print $2}' | awk '{print $1}' | tr -d '\*#-')
echo -e "- $window_name"
pane_count=$(tmux list-panes -t "${session_name}:${window_name}" | wc -l)
if [[ $pane_count -gt 1 ]]; then
echo -e " - Contains ${pane_count} pane(s)"
fi
done
fi
for yaml_file in "${CONFIG_DIR}"/*.yaml; do
if (yq ".sessions.${session_name}" "${yaml_file}" | grep -v null &>/dev/null); then
Expand All @@ -140,6 +155,7 @@ preview() {
fi
fi
done
unset -f session_exists
}

export -f preview
Expand All @@ -166,8 +182,10 @@ list_sessions() {
--bind='ctrl-t:toggle-preview' \
--preview="bash -c 'preview {}'" --header='Please select a session')
fi
unset -f preview
if [[ -n "$session" ]]; then
if ! session_exists "$session"; then
unset -f session_exists
create_session "$session"
fi
if is_tmux_open; then
Expand Down Expand Up @@ -223,8 +241,9 @@ create_session() {
tmux send-keys -t "${session_name}:${window_name}.$((j + 1))" "$pane_command" C-m
fi
done
elif [[ -n "$window_command" ]]; then
tmux send-keys -t "${session_name}":"$(printf '%q' ${window_name})" "$window_command" C-m
elif [[ -n "${window_command}" ]]; then
tmux select-window -t "${session_name}":"$(printf '%q' ${window_name})"
tmux send-keys -t "${session_name}":"$(printf '%q' "${window_name}")" "${window_command}" C-m
fi
done
}
Expand Down Expand Up @@ -257,31 +276,29 @@ export_config() {
exit "${EXIT_CONFIG_ERROR}";
fi
fi
local yaml_content=""
if [[ ! -f "${export_file}" ]]; then
yaml_content="---\n"
yaml_content+="sessions:
${session_name}:
root: ${root_dir}
start_index: 1
windows:"
yaml_content+="sessions:\n"
yaml_content+=" ${session_name}:\n"
yaml_content+=" root: ${root_dir}\n"
yaml_content+=" windows:\n"
else
yaml_content=" ${session_name}:
root: ${root_dir}
start_index: 1
windows:"
yaml_content=" ${session_name}:\n"
yaml_content+=" root: ${root_dir}\n"
yaml_content+=" windows:\n"
fi
local windows=$(tmux list-windows -t $session_name -F '#I #W #{pane_current_path}')
while IFS=' ' read -r index name path; do
last_command=""
yaml_content+="
- name: $name
command: \"$last_command\""
yaml_content+=" - name: $name\n"
yaml_content+=" command: $last_command\n"
done <<< "$windows"
echo -e "${yaml_content}" >> $export_file;
if [[ ! -f "${export_file}" ]]; then
echo -e "Created config file for '${session_name}' on '${export_file}'"
else
echo -e "Appended config file for '${session_name}' on '${export_file}'"
echo -e "Appended to config file for '${session_name}' on '${export_file}'"
fi
}

Expand Down Expand Up @@ -312,7 +329,7 @@ grep_config() {
kill () {
if [[ $# -eq 0 ]]; then
echo -e "zmux: Error: Must specify session name or 'all' to kill server." >&2;
exit "${EXIT_IVALID_INPUT}";
exit "${EXIT_INVALID_INPUT}";
fi
if [[ $1 == all ]]; then
tmux kill-server
Expand Down Expand Up @@ -366,7 +383,7 @@ main() {
check_config
if [[ $(echo -e "${configured}" | uniq -d) ]]; then
dup=$(echo -e "${configured}" | uniq -d | tr '\n' ' ')
echo -e "zmux: error: duplicate session(s) found '${dup}', exiting.." >&2;
echo -e "zmux: Error: duplicate session(s) found '${dup}', exiting.." >&2;
exit "${EXIT_CONFIG_ERROR}";
fi
local session_name="$1"
Expand Down