Class: Mxup::PaneResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/mxup/pane_resolver.rb

Overview

Answers the question “where is the logical window <foo> actually living in tmux right now?” given the config + active layout + live tmux state.

Separating this from Runner means Reconciler, LayoutManager, ExecRunner, StatusView and Target can all share a single source of truth.

Instance Method Summary collapse

Constructor Details

#initialize(config, session: config.session, layout_override: nil) ⇒ PaneResolver

Returns a new instance of PaneResolver.



10
11
12
13
14
# File 'lib/mxup/pane_resolver.rb', line 10

def initialize(config, session: config.session, layout_override: nil)
  @config          = config
  @session         = session
  @layout_override = layout_override
end

Instance Method Details

#active_layoutObject

The layout the caller asked for, or the one persisted in the session’s tmux environment, or the config’s default. May be nil if no layouts at all.



18
19
20
21
# File 'lib/mxup/pane_resolver.rb', line 18

def active_layout
  return @layout_override if @layout_override
  Tmux.has_session?(@session) ? stored_layout : @config.default_layout
end

#pane_for(target) ⇒ Object

Look up the pane metadata hash for an already-resolved tmux target.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mxup/pane_resolver.rb', line 54

def pane_for(target)
  if target =~ /\A(.+)\.(\d+)\z/
    win_name = Regexp.last_match(1)
    pane_idx = Regexp.last_match(2).to_i
    Tmux.list_panes(@session)
        .find { |p| p[:name] == win_name && p[:pane_index] == pane_idx }
  else
    Tmux.list_panes(@session)
        .find { |p| p[:name] == target && p[:pane_index] == 0 }
  end
end

#pane_target(window_name, layout = active_layout) ⇒ Object

Resolve a logical window name to a tmux target string (either “window” for a standalone, or “group.pane_index” for a grouped window). Returns nil if the window isn’t currently in the session.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mxup/pane_resolver.rb', line 37

def pane_target(window_name, layout = active_layout)
  result = @config.find_group_for_window(layout, window_name)

  if result
    group, cfg_idx = result
    return nil unless window_exists?(group.name)

    pane = Tmux.list_panes(@session)
               .find { |p| p[:name] == group.name && p[:title] == window_name }
    actual_idx = pane ? pane[:pane_index] : cfg_idx
    Tmux.pane_target(group.name, actual_idx)
  else
    window_exists?(window_name) ? window_name : nil
  end
end

#stored_layoutObject

Same as #active_layout but ignores any –layout override — used when we need to know what’s really in the session right now (e.g. to decide whether a layout switch is needed).



26
27
28
29
30
31
32
# File 'lib/mxup/pane_resolver.rb', line 26

def stored_layout
  if Tmux.has_session?(@session)
    stored = Tmux.show_environment(@session, 'MXUP_LAYOUT')
    return stored if stored && @config.layout_names.include?(stored)
  end
  @config.default_layout
end

#window_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/mxup/pane_resolver.rb', line 66

def window_exists?(name)
  Tmux.list_windows(@session).any? { |w| w[:name] == name }
end