I don't have the time right now to submit a pull request because this probably no longer works in linux so I would need to add some OS detection and switches. Here is a version that runs in OSX 10.9.2 with ruby 1.9.2-p180.
Issues resolved:
- Home directory expansion was not happening, causing the created folder to be in the current directory and literally be named with a tilde.
- Directory creation was sort of broken, so changed
Dir in one place to FileUtils.
- Renamed
.sessions to .tmux-sessions because it was too generic.
- Most recent
tmux version has slightly different argument names for its commands.
list-sessions command fixed
list-panes command fixed
ps command in OSX has different arguments, the current ones caused error.
- The restore script was misformatted and wouldn't execute.
- The restore script didn't have execute permissions.
#!/usr/bin/env ruby
require 'fileutils.rb'
# Start - Configuration Variables
sessionDir = ENV['HOME']+"/.tmux-sessions"
maxStoredSessions = 5
filesToRoll = 3
# End - Configuration Variables
FileUtils::makedirs(sessionDir) unless File.exists?(sessionDir)
files = []
Dir.entries(sessionDir).each do |e|
if e !~ /^\./
files << e
end
end
files.sort! unless files.length == 0
if files.length > maxStoredSessions
0.upto(filesToRoll - 1) do |index|
File.delete( sessionDir+ "/" + files[index] )
end
puts "Rotated stored sessions"
end
#%x[rm #{sessionDir}/*-restore]
sessions = %x[tmux list-sessions -F "\#{session_name}"].split("\n")
sessions.each do |sessionName|
rawPaneList = %x[tmux list-panes -t #{sessionName} -s -F "\#{window_index} \#{pane_index} \#{window_width} \#{window_height} \#{pane_width} \#{pane_height} \#{window_name} \#{pane_current_path} \#{pane_pid}"].split("\n")
panes = []
rawPaneList.each do |pane_line|
temp_pane = pane_line.split(" ")
panes.push({
windowIndex: Integer(temp_pane[0]),
paneIndex: Integer(temp_pane[1]),
windowWidth: Integer(temp_pane[2]),
windowHeight: Integer(temp_pane[3]),
paneWidth: Integer(temp_pane[4]),
paneHeight: Integer(temp_pane[5]),
window_name: temp_pane[6],
cwd: temp_pane[7],
pid: temp_pane[8]
})
end
sessionScript = ""
panes.each do |pane|
pane[:cmd] = %x[ps -o command -p #{pane[:pid]} | awk 'NR>1'].delete("\n")
pane[:cmd] = %x[ps -o command #{pane[:pid]} | awk 'NR>1'].delete("\n").gsub(/^-/,"") unless pane[:cmd] != ""
sessionScript += "tmux new-window -t $SESSION -a -n #{pane[:window_name]} \"cd #{pane[:cwd]} && #{pane[:cmd]}\"\n"
if pane[:paneIndex] > 0
if pane[:paneWidth] < pane[:windowWidth]
sessionScript += "tmux join-pane -h -l #{pane[:paneWidth]} -s $SESSION:#{pane[:windowIndex] +1}.0 -t $SESSION:#{pane[:windowIndex]}\n"
else
sessionScript += "tmux join-pane -v -l #{pane[:paneHeight]} -s $SESSION:#{pane[:windowIndex] +1}.0 -t $SESSION:#{pane[:windowIndex]}\n"
end
end
end
restore_file = sessionDir + "/" + sessionName + "-restore"
File.open(restore_file, "w") {
|f| f.write(%Q[#!/usr/bin/env bash
SESSION=#{sessionName}
if [ -z $TMUX ]; then
# if session already exists, attach
tmux has-session -t $SESSION
if [ $? -eq 0 ]; then
echo \"Session $SESSION already exists. Attaching...\"
tmux attach -t $SESSION
exit 0;
fi
# make new session
tmux new-session -d -s $SESSION
#{sessionScript}
# attach to new session
tmux select-window -t $SESSION:1
tmux attach-session -t $SESSION
fi
])
# make script executable
f.chmod( 0744 )
}
end
I don't have the time right now to submit a pull request because this probably no longer works in linux so I would need to add some OS detection and switches. Here is a version that runs in OSX 10.9.2 with ruby 1.9.2-p180.
Issues resolved:
Dirin one place toFileUtils..sessionsto.tmux-sessionsbecause it was too generic.tmuxversion has slightly different argument names for its commands.list-sessionscommand fixedlist-panescommand fixedpscommand in OSX has different arguments, the current ones caused error.