Git Product home page Git Product logo

tmux-persistence's People

Contributors

geebee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

alde

tmux-persistence's Issues

OSX Compatibility

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.