Sha256: 080288701f43ec30c96a716a28805ef2a5a319f01c9a29d68bb0fcfd3be3b9d7

Contents?: true

Size: 973 Bytes

Versions: 3

Compression:

Stored size: 973 Bytes

Contents

require 'fileutils'
require 'tmpdir'

module Pair
  class Session
    class Tmux
      attr_accessor :session_name
      private       :session_name=

      def initialize(session_name)
        self.session_name = session_name
      end

      def start
        args = %W[-S #{socket_path} new-session -d]
        system tmux, *args
      end

      def attach(read_only = false)
        system(*attach_command(read_only))
      end

      def stop
        `lsof -t #{socket_path}/ 2>/dev/null | xargs kill -9`
        FileUtils.rm_f(socket_path)
      end

      def attach_command(read_only = false)
        args = %W[#{tmux} -S #{socket_path} attach]
        args << " -r" if read_only
        args
      end

      private

      def tmux
        @tmux ||= `which tmux`.chomp
      end

      def socket_path
        File.join(Dir.tmpdir, socket_name)
      end

      def socket_name
        "pair-#{session_name.gsub(/[^\w_-]+/,'')}.tmux"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pair-0.0.4 lib/pair/session/tmux.rb
pair-0.0.3 lib/pair/session/tmux.rb
pair-0.0.2 lib/pair/session/tmux.rb