Class: Mxup::Runner

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

Overview

Public programmatic API. Composes the focused helpers — nothing here should contain real business logic, only wiring and delegation.

Constant Summary collapse

DEFAULT_INTERRUPT_DELAY =

Delay (seconds) between the two Ctrl-C presses during a restart, and between Ctrl-C and sending the new command. In production 1s is enough to let the pane's shell redraw its prompt; tests shorten this to avoid paying multi-second waits for a purely cosmetic settle.

1.0

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, dry_run: false, layout: nil) ⇒ Runner

Returns a new instance of Runner.



24
25
26
27
28
29
# File 'lib/mxup/runner.rb', line 24

def initialize(config, dry_run: false, layout: nil)
  @config  = config
  @session = config.session
  @dry_run = dry_run
  @layout  = layout
end

Class Attribute Details

.interrupt_delayObject



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

def interrupt_delay
  @interrupt_delay || DEFAULT_INTERRUPT_DELAY
end

Instance Method Details

#downObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mxup/runner.rb', line 45

def down
  unless Tmux.has_session?(@session)
    $stdout.puts "Session #{@session} is not running."
    return
  end

  if @dry_run
    $stdout.puts "[dry-run] Would gracefully stop session #{@session}"
    return
  end

  stop_watcher
  graceful_stop.call
  Tmux.kill_session(@session)
  launcher.cleanup
  $stdout.puts "Session #{@session} killed."
end

#exec(target_spec, command, lines: 50, timeout: nil, force: false, quiet: false) ⇒ Object



101
102
103
104
105
# File 'lib/mxup/runner.rb', line 101

def exec(target_spec, command, lines: 50, timeout: nil, force: false, quiet: false)
  exec_runner.run(target_spec, command,
                  lines: lines, timeout: timeout,
                  force: force, quiet: quiet)
end

#restart(window_names) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mxup/runner.rb', line 63

def restart(window_names)
  abort "Session #{@session} is not running." unless Tmux.has_session?(@session)
  launcher.write_all unless @dry_run

  targets = resolve_restart_targets(window_names)

  targets.each do |win|
    target_ref = resolver.pane_target(win.name)
    if target_ref
      restart_existing(win, target_ref)
    else
      create_missing(win)
    end
  end
end

#run_command(window_name, command_name) ⇒ Object

Run a window's named command (mxup <config> <window>:<name>) in a copy of that window's environment, streaming its output to the calling shell and exiting with its return code. Does not touch the live tmux pane, so it works whether or not the session is up.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mxup/runner.rb', line 111

def run_command(window_name, command_name)
  win = @config.window_by_name(window_name) ||
        abort("Window '#{window_name}' not found in config.")

  command = win.commands[command_name] || abort(unknown_command_message(win, command_name))

  script = launcher.build_command_script(win, command)

  if @dry_run
    $stdout.puts "[dry-run] Would run #{window_name}:#{command_name} in #{win.root}:"
    $stdout.puts command
    return
  end

  exit(run_script(script))
end

#show_layoutsObject



83
84
85
86
87
# File 'lib/mxup/runner.rb', line 83

def show_layouts
  layout_manager.show(
    active: Tmux.has_session?(@session) ? resolver.stored_layout : nil
  )
end

#status(lines:) ⇒ Object



41
42
43
# File 'lib/mxup/runner.rb', line 41

def status(lines:)
  status_view.render(lines: lines)
end

#switch_layout(target_layout) ⇒ Object



79
80
81
# File 'lib/mxup/runner.rb', line 79

def switch_layout(target_layout)
  layout_manager.switch(target_layout)
end

#target(window_names) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mxup/runner.rb', line 89

def target(window_names)
  abort "Session #{@session} is not running." unless Tmux.has_session?(@session)

  requested = Array(window_names).flat_map { |n| n.split(',') }.reject(&:empty?)

  if requested.empty?
    print_all_targets
  else
    print_requested_targets(requested)
  end
end

#upObject

--- primary commands --------------------------------------------------



33
34
35
36
37
38
39
# File 'lib/mxup/runner.rb', line 33

def up
  handle_profile_switch
  require_env!
  reconciler.up
  ensure_watcher unless @dry_run
  $stdout.puts "Attach with: tmux attach -t #{@session}" unless @dry_run
end