Class: Hiiro::Tmux::Session
- Inherits:
-
Object
- Object
- Hiiro::Tmux::Session
- Defined in:
- lib/hiiro/tmux/session.rb
Constant Summary collapse
- FORMAT =
'#{session_id}|#{session_name}|#{session_windows}|#{session_attached}|#{session_created}|#{session_last_attached}|#{session_path}'
Instance Attribute Summary collapse
-
#created ⇒ Object
readonly
Returns the value of attribute created.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#last_attached ⇒ Object
readonly
Returns the value of attribute last_attached.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#windows ⇒ Object
readonly
Returns the value of attribute windows.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #attach ⇒ Object
- #attached? ⇒ Boolean
- #detach ⇒ Object
- #display ⇒ Object
-
#initialize(id:, name:, windows: 0, attached: false, created: 0, last_attached: 0, path: nil) ⇒ Session
constructor
A new instance of Session.
- #kill ⇒ Object
- #rename(new_name) ⇒ Object
- #select ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(id:, name:, windows: 0, attached: false, created: 0, last_attached: 0, path: nil) ⇒ Session
Returns a new instance of Session.
49 50 51 52 53 54 55 56 57 |
# File 'lib/hiiro/tmux/session.rb', line 49 def initialize(id:, name:, windows: 0, attached: false, created: 0, last_attached: 0, path: nil) @id = id @name = name @windows = windows @attached = attached @created = created @last_attached = last_attached @path = path end |
Instance Attribute Details
#created ⇒ Object (readonly)
Returns the value of attribute created.
6 7 8 |
# File 'lib/hiiro/tmux/session.rb', line 6 def created @created end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/hiiro/tmux/session.rb', line 6 def id @id end |
#last_attached ⇒ Object (readonly)
Returns the value of attribute last_attached.
6 7 8 |
# File 'lib/hiiro/tmux/session.rb', line 6 def last_attached @last_attached end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/hiiro/tmux/session.rb', line 6 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/hiiro/tmux/session.rb', line 6 def path @path end |
#windows ⇒ Object (readonly)
Returns the value of attribute windows.
6 7 8 |
# File 'lib/hiiro/tmux/session.rb', line 6 def windows @windows end |
Class Method Details
.all ⇒ Object
34 35 36 37 38 39 |
# File 'lib/hiiro/tmux/session.rb', line 34 def self.all output = `tmux list-sessions -F '#{FORMAT}' 2>/dev/null` return [] if output.nil? || output.empty? output.each_line.map { |line| from_format_line(line) }.compact end |
.client_map ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/hiiro/tmux/session.rb', line 41 def self.client_map output = `tmux list-clients -F '\#{client_tty}|\#{session_name}' 2>/dev/null` output.lines(chomp: true).each_with_object({}) do |line, map| tty, session_name = line.split('|', 2) map[session_name] ||= tty.delete_prefix('/dev/') end end |
.current ⇒ Object
27 28 29 30 31 32 |
# File 'lib/hiiro/tmux/session.rb', line 27 def self.current output = `tmux display-message -p '#{FORMAT}' 2>/dev/null`.strip return nil if output.empty? from_format_line(output) end |
.from_format_line(line) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/hiiro/tmux/session.rb', line 8 def self.from_format_line(line) return nil if line.nil? || line.strip.empty? parts = line.strip.split('|', 7) return nil if parts.size < 4 id, name, windows, attached, created, last_attached, path = parts new( id: id, name: name, windows: windows.to_i, attached: attached == '1', created: created.to_i, last_attached: last_attached.to_i, path: path ) end |
Instance Method Details
#==(other) ⇒ Object
59 60 61 |
# File 'lib/hiiro/tmux/session.rb', line 59 def ==(other) other.is_a?(Session) && name == other.name end |
#attach ⇒ Object
83 84 85 |
# File 'lib/hiiro/tmux/session.rb', line 83 def attach system('tmux', 'attach-session', '-t', "=#{name}") end |
#attached? ⇒ Boolean
63 64 65 |
# File 'lib/hiiro/tmux/session.rb', line 63 def attached? @attached end |
#detach ⇒ Object
87 88 89 |
# File 'lib/hiiro/tmux/session.rb', line 87 def detach system('tmux', 'detach-client', '-s', name) end |
#display ⇒ Object
107 108 109 |
# File 'lib/hiiro/tmux/session.rb', line 107 def display "#{name} (#{windows} windows#{attached? ? ', attached' : ''})" end |
#kill ⇒ Object
67 68 69 |
# File 'lib/hiiro/tmux/session.rb', line 67 def kill system('tmux', 'kill-session', '-t', name) end |
#rename(new_name) ⇒ Object
71 72 73 |
# File 'lib/hiiro/tmux/session.rb', line 71 def rename(new_name) system('tmux', 'rename-session', '-t', name, new_name) end |
#select ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/hiiro/tmux/session.rb', line 75 def select if ENV['TMUX'] system('tmux', 'switch-client', '-t', "=#{name}") else system('tmux', 'attach-session', '-t', "=#{name}") end end |
#to_h ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/hiiro/tmux/session.rb', line 91 def to_h { id: id, name: name, windows: windows, attached: attached?, created: created, last_attached: last_attached, path: path }.compact end |
#to_s ⇒ Object
103 104 105 |
# File 'lib/hiiro/tmux/session.rb', line 103 def to_s name end |