Class: Hiiro::Tmux::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/tmux/window.rb

Constant Summary collapse

FORMAT =
'#{window_id}|#{window_index}|#{window_name}|#{window_active}|#{window_panes}|#{session_name}'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, index:, name:, active: false, panes: 1, session_name: nil) ⇒ Window

Returns a new instance of Window.



33
34
35
36
37
38
39
40
# File 'lib/hiiro/tmux/window.rb', line 33

def initialize(id:, index:, name:, active: false, panes: 1, session_name: nil)
  @id = id
  @index = index
  @name = name
  @active = active
  @panes = panes
  @session_name = session_name
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/hiiro/tmux/window.rb', line 6

def id
  @id
end

#indexObject (readonly)

Returns the value of attribute index.



6
7
8
# File 'lib/hiiro/tmux/window.rb', line 6

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/hiiro/tmux/window.rb', line 6

def name
  @name
end

#panesObject (readonly)

Returns the value of attribute panes.



6
7
8
# File 'lib/hiiro/tmux/window.rb', line 6

def panes
  @panes
end

#session_nameObject (readonly)

Returns the value of attribute session_name.



6
7
8
# File 'lib/hiiro/tmux/window.rb', line 6

def session_name
  @session_name
end

Class Method Details

.currentObject



26
27
28
29
30
31
# File 'lib/hiiro/tmux/window.rb', line 26

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
# File 'lib/hiiro/tmux/window.rb', line 8

def self.from_format_line(line)
  return nil if line.nil? || line.strip.empty?

  parts = line.strip.split('|', 6)
  return nil if parts.size < 5

  id, index, name, active, panes, session_name = parts

  new(
    id: id,
    index: index.to_i,
    name: name,
    active: active == '1',
    panes: panes.to_i,
    session_name: session_name
  )
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/hiiro/tmux/window.rb', line 42

def active?
  @active
end

#killObject



50
51
52
# File 'lib/hiiro/tmux/window.rb', line 50

def kill
  system('tmux', 'kill-window', '-t', target)
end


70
71
72
# File 'lib/hiiro/tmux/window.rb', line 70

def link_to(dest_session)
  system('tmux', 'link-window', '-s', target, '-t', dest_session)
end

#move_to(dest_target) ⇒ Object



66
67
68
# File 'lib/hiiro/tmux/window.rb', line 66

def move_to(dest_target)
  system('tmux', 'move-window', '-s', target, '-t', dest_target)
end

#rename(new_name) ⇒ Object



54
55
56
# File 'lib/hiiro/tmux/window.rb', line 54

def rename(new_name)
  system('tmux', 'rename-window', '-t', target, new_name)
end

#selectObject



58
59
60
# File 'lib/hiiro/tmux/window.rb', line 58

def select
  system('tmux', 'select-window', '-t', target)
end

#swap_with(other_target) ⇒ Object



62
63
64
# File 'lib/hiiro/tmux/window.rb', line 62

def swap_with(other_target)
  system('tmux', 'swap-window', '-s', target, '-t', other_target)
end

#targetObject



46
47
48
# File 'lib/hiiro/tmux/window.rb', line 46

def target
  "#{session_name}:#{index}"
end

#to_hObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/hiiro/tmux/window.rb', line 78

def to_h
  {
    id: id,
    index: index,
    name: name,
    active: active?,
    panes: panes,
    session_name: session_name
  }.compact
end

#to_sObject



89
90
91
# File 'lib/hiiro/tmux/window.rb', line 89

def to_s
  "#{session_name}:#{index} #{name} [#{panes} panes]#{active? ? ' *' : ''}"
end


74
75
76
# File 'lib/hiiro/tmux/window.rb', line 74

def unlink
  system('tmux', 'unlink-window', '-t', target)
end