mirror of
https://github.com/jimeh/tmuxifier.git
synced 2026-02-19 01:46:40 +00:00
Make layout API-interface a bit simpler
Additionally, also fix a bug when creating a window, where as if the session name was the same as a window that already existed, it would fail, accidentally using the window rather than the session at a target.
This commit is contained in:
116
layout-helpers.sh
Normal file
116
layout-helpers.sh
Normal file
@@ -0,0 +1,116 @@
|
||||
#
|
||||
# Layout Helpers
|
||||
#
|
||||
# These functions are available exclusively within layout files, and enable
|
||||
# the layout files to function at all, but also provide useful short-hands to
|
||||
# otherwise more complex means.
|
||||
#
|
||||
|
||||
# Create a new window.
|
||||
new_window() {
|
||||
if [ ! -z "$1" ]; then
|
||||
window="$1"
|
||||
fi
|
||||
tmux new-window -t "$session:" -n "$window"
|
||||
}
|
||||
|
||||
# Load specified window layout.
|
||||
load_window() {
|
||||
local file="$TMUXIFIER_LAYOUT_PATH/$1.window.sh"
|
||||
if [ -f "$file" ]; then
|
||||
window="$1"
|
||||
source "$file"
|
||||
|
||||
# Reset `$window_root`.
|
||||
if [[ "$window_root" != "$session_root" ]]; then
|
||||
window_root "$session_root"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Load specified session layout.
|
||||
load_session() {
|
||||
local file="$TMUXIFIER_LAYOUT_PATH/$1.session.sh"
|
||||
if [ -f "$file" ]; then
|
||||
session="$1"
|
||||
source "$file"
|
||||
|
||||
# Reset `$session_root`.
|
||||
if [[ "$session_root" != "$HOME" ]]; then
|
||||
session_root="$HOME"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Cusomize session root path. Default is `$HOME`.
|
||||
session_root() {
|
||||
local dir="$(__expand_path $@)"
|
||||
if [ -d "$dir" ]; then
|
||||
session_root="$dir"
|
||||
fi
|
||||
}
|
||||
|
||||
# Customize window root path. Default is `$session_root`.
|
||||
window_root() {
|
||||
local dir="$(__expand_path $@)"
|
||||
if [ -d "$dir" ]; then
|
||||
cd "$dir"
|
||||
fi
|
||||
}
|
||||
|
||||
# Create a new session, returning 0 on success, 1 on failure.
|
||||
#
|
||||
# Example usage:
|
||||
#
|
||||
# if initialize_session; then
|
||||
# load_window "example"
|
||||
# fi
|
||||
#
|
||||
initialize_session() {
|
||||
if [ ! -z "$1" ]; then
|
||||
session="$1"
|
||||
fi
|
||||
|
||||
# Ensure tmux server is running for has-session check.
|
||||
tmux start-server
|
||||
|
||||
# Check if the named session already exists.
|
||||
if ! tmux has-session -t "$session" 2>/dev/null; then
|
||||
# Create the new session.
|
||||
env TMUX= tmux new-session -d -s "$session"
|
||||
|
||||
# Set default-path for session
|
||||
if [ ! -z "$session_root" ] && [ -d "$session_root" ]; then
|
||||
tmux set-option -t "$session" default-path "$session_root" 1>/dev/null
|
||||
fi
|
||||
|
||||
# In order to ensure only specified windows are created, we move the
|
||||
# default window to position 99, and later remove it with the
|
||||
# `finalize_session` function.
|
||||
tmux move-window -s "$session:0" -t "$session:99"
|
||||
|
||||
# Ensure correct pane splitting.
|
||||
__go_to_session
|
||||
|
||||
# Session created, return ok exit status.
|
||||
return 0
|
||||
fi
|
||||
# Session already existed, return error exit status.
|
||||
return 1
|
||||
}
|
||||
|
||||
# Finalize session creation and then switch to it if needed.
|
||||
#
|
||||
# When the session is created, it leaves a unused window in position #99, this
|
||||
# is the default window which was created with the session, but it's also a
|
||||
# window that was not explicitly created. Hence we kill it.
|
||||
#
|
||||
# If the session was created, we've already been switched to it. If it was not
|
||||
# created, the session already exists, and we'll need to specifically switch
|
||||
# to it here.
|
||||
finalize_and_go_to_session() {
|
||||
tmux kill-window -t "$session:99" 2>/dev/null || true
|
||||
if [[ "$(tmuxifier-current-session)" != "$session" ]]; then
|
||||
__go_to_session
|
||||
fi
|
||||
}
|
||||
@@ -24,5 +24,6 @@ if [ -f "$layout_file" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp "$template" "$layout_file"
|
||||
content="$(cat "$template")"
|
||||
echo "${content/\{\{SESSION_NAME\}\}/$layout_name}" > "$layout_file"
|
||||
exec "$EDITOR" "$layout_file"
|
||||
|
||||
@@ -24,5 +24,6 @@ if [ -f "$layout_file" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp "$template" "$layout_file"
|
||||
content="$(cat "$template")"
|
||||
echo "${content/\{\{WINDOW_NAME\}\}/$layout_name}" > "$layout_file"
|
||||
exec "$EDITOR" "$layout_file"
|
||||
|
||||
127
runtime.sh
127
runtime.sh
@@ -1,126 +1,15 @@
|
||||
#
|
||||
# Load up runtime environment for session and window layout files.
|
||||
#
|
||||
|
||||
# Load tmuxifier environment.
|
||||
source "$TMUXIFIER/env.sh"
|
||||
|
||||
#
|
||||
# Defaults
|
||||
#
|
||||
|
||||
# Setup default variables.
|
||||
session_root="$HOME"
|
||||
|
||||
|
||||
#
|
||||
# Layout Helpers
|
||||
#
|
||||
# These functions are available exclusively within layout files, and enable
|
||||
# the layout files to function at all, but also provide useful short-hands to
|
||||
# otherwise more complex means.
|
||||
|
||||
# Load specified window layout.
|
||||
load_window() {
|
||||
local file="$TMUXIFIER_LAYOUT_PATH/$1.window.sh"
|
||||
if [ -f "$file" ]; then
|
||||
window="$1"
|
||||
source "$file"
|
||||
|
||||
# Reset `$window_root`.
|
||||
if [[ "$window_root" != "$session_root" ]]; then
|
||||
window_root "$session_root"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Load specified session layout.
|
||||
load_session() {
|
||||
local file="$TMUXIFIER_LAYOUT_PATH/$1.session.sh"
|
||||
if [ -f "$file" ]; then
|
||||
session="$1"
|
||||
source "$file"
|
||||
|
||||
# Reset `$session_root`.
|
||||
if [[ "$session_root" != "$HOME" ]]; then
|
||||
session_root="$HOME"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Customize session name. Default is based on the session layout filename.
|
||||
session_name() {
|
||||
session="$1"
|
||||
}
|
||||
|
||||
# Cusomize session root path. Default is `$HOME`.
|
||||
session_root() {
|
||||
local dir="$(__expand_path $@)"
|
||||
if [ -d "$dir" ]; then
|
||||
session_root="$dir"
|
||||
fi
|
||||
}
|
||||
|
||||
# Customize window name. Default is based on the window layout filename.
|
||||
window_name() {
|
||||
window="$1"
|
||||
}
|
||||
|
||||
# Customize window root path. Default is `$session_root`.
|
||||
window_root() {
|
||||
local dir="$(__expand_path $@)"
|
||||
if [ -d "$dir" ]; then
|
||||
cd "$dir"
|
||||
fi
|
||||
}
|
||||
|
||||
# Create a new session, returning 0 on success, 1 on failure.
|
||||
#
|
||||
# Example usage:
|
||||
#
|
||||
# if initialize_session; then
|
||||
# load_window "example"
|
||||
# fi
|
||||
#
|
||||
initialize_session() {
|
||||
# Ensure tmux server is running for has-session check.
|
||||
tmux start-server
|
||||
|
||||
# Check if the named session already exists.
|
||||
if ! tmux has-session -t "$session" 2>/dev/null; then
|
||||
# Create the new session.
|
||||
env TMUX= tmux new-session -d -s "$session"
|
||||
|
||||
# Set default-path for session
|
||||
if [ ! -z "$session_root" ] && [ -d "$session_root" ]; then
|
||||
tmux set-option -t "$session" default-path "$session_root" 1>/dev/null
|
||||
fi
|
||||
|
||||
# In order to ensure only specified windows are created, we move the
|
||||
# default window to position 99, and later remove it with the
|
||||
# `finalize_session` function.
|
||||
tmux move-window -s "$session:0" -t "$session:99"
|
||||
|
||||
# Ensure correct pane splitting.
|
||||
__go_to_session
|
||||
|
||||
# Session created, return ok exit status.
|
||||
return 0
|
||||
fi
|
||||
# Session already existed, return error exit status.
|
||||
return 1
|
||||
}
|
||||
|
||||
# Finalize session creation and then switch to it if needed.
|
||||
#
|
||||
# When the session is created, it leaves a unused window in position #99, this
|
||||
# is the default window which was created with the session, but it's also a
|
||||
# window that was not explicitly created. Hence we kill it.
|
||||
#
|
||||
# If the session was created, we've already been switched to it. If it was not
|
||||
# created, the session already exists, and we'll need to specifically switch
|
||||
# to it here.
|
||||
finalize_and_go_to_session() {
|
||||
tmux kill-window -t "$session:99" 2>/dev/null || true
|
||||
if [[ "$(tmuxifier-current-session)" != "$session" ]]; then
|
||||
__go_to_session
|
||||
fi
|
||||
}
|
||||
# Load layout helper functions.
|
||||
source "$TMUXIFIER/layout-helpers.sh"
|
||||
|
||||
|
||||
#
|
||||
@@ -131,7 +20,7 @@ finalize_and_go_to_session() {
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# $ __expand_path ~/Projects
|
||||
# $ __expand_path "~/Projects"
|
||||
# /Users/jimeh/Projects
|
||||
#
|
||||
__expand_path() {
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
# Set custom session name. Default is based on filename.
|
||||
# session_name "Example Session"
|
||||
# Set a custom session root path. Default is `$HOME`.
|
||||
# Must be called before `initialize_session`.
|
||||
#session_root "~/Projects/example"
|
||||
|
||||
# Set a custom session root. Default is `$HOME`.
|
||||
# session_root "~/Projects/example"
|
||||
# Create session with specified name if it does not already exist. If no
|
||||
# argument is given, session name will be based on layout file name.
|
||||
if initialize_session "{{SESSION_NAME}}"; then
|
||||
|
||||
# Create session if it does not already exist.
|
||||
if initialize_session; then
|
||||
# Create a new window inline within session layout definition.
|
||||
#new_window "misc"
|
||||
|
||||
# Load window layouts if session was created.
|
||||
# load_window "example"
|
||||
# Load a defined window layout.
|
||||
#load_window "example"
|
||||
|
||||
fi
|
||||
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
# Set custom window name. Default is based on filename.
|
||||
# window_name "Example Window"
|
||||
# Set window root path. Default is `$session_root`.
|
||||
# Must be called before `new_window`.
|
||||
#window_root "~/Projects/example"
|
||||
|
||||
# Set a window root path. Default is `$session_root`.
|
||||
# window_root "~/Projects/example"
|
||||
|
||||
# Create new window. Remove if you want to apply layout to current window.
|
||||
tmux new-window -t "$session" -n "$window"
|
||||
# Create new window. If no argument is given, window name will be based on
|
||||
# layout file name.
|
||||
new_window "{{WINDOW_NAME}}"
|
||||
|
||||
# Split window into panes.
|
||||
# tmux split-window -t "$session:$window.0" -h -p 20
|
||||
#tmux split-window -t "$session:$window.0" -h -p 20
|
||||
|
||||
# Set active pane.
|
||||
# tmux select-pane -t "$session:$window.0"
|
||||
#tmux select-pane -t "$session:$window.0"
|
||||
|
||||
Reference in New Issue
Block a user