Class: TTYtest::Tmux::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/ttytest/tmux/driver.rb

Overview

tmux driver

Defined Under Namespace

Classes: TmuxError

Constant Summary collapse

COMMAND =
'tmux'
SOCKET_NAME =
'ttytest'
REQUIRED_TMUX_VERSION =
'1.8'
DEFAULT_CONFING_FILE_PATH =
File.expand_path('tmux.conf', __dir__)
SLEEP_INFINITY =
'read x < /dev/fd/1'

Instance Method Summary collapse

Constructor Details

#initialize(debug: false, command: COMMAND, socket_name: SOCKET_NAME, config_file_path: DEFAULT_CONFING_FILE_PATH) ⇒ Driver

Returns a new instance of Driver.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ttytest/tmux/driver.rb', line 22

def initialize(
  debug: false,
  command: COMMAND,
  socket_name: SOCKET_NAME,
  config_file_path: DEFAULT_CONFING_FILE_PATH
)
  @debug = debug
  @tmux_cmd = command
  @socket_name = socket_name
  @config_file_path = config_file_path
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/ttytest/tmux/driver.rb', line 70

def available?
  return false unless tmux_version

  @available ||= (Gem::Version.new(tmux_version) >= Gem::Version.new(REQUIRED_TMUX_VERSION))
end

#new_default_sh_terminalObject



43
44
45
# File 'lib/ttytest/tmux/driver.rb', line 43

def new_default_sh_terminal
  new_terminal(%(PS1='$ ' /bin/sh), width: 80, height: 24)
end

#new_sh_terminal(width: 80, height: 24) ⇒ Object



47
48
49
# File 'lib/ttytest/tmux/driver.rb', line 47

def new_sh_terminal(width: 80, height: 24)
  new_terminal(%(PS1='$ ' /bin/sh), width: width, height: height)
end

#new_terminal(cmd, width: 80, height: 24) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/ttytest/tmux/driver.rb', line 34

def new_terminal(cmd, width: 80, height: 24)
  cmd = "#{cmd}\n#{SLEEP_INFINITY}"

  session_name = "ttytest-#{SecureRandom.uuid}"
  tmux(*%W[-f #{@config_file_path} new-session -s #{session_name} -d -x #{width} -y #{height} #{cmd}])
  session = Session.new(self, session_name)
  Terminal.new(session)
end

#tmux(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ttytest/tmux/driver.rb', line 52

def tmux(*args)
  ensure_available
  puts "tmux(#{args.inspect[1...-1]})" if debug?

  cmd = [@tmux_cmd, '-L', SOCKET_NAME, *args]
  output = nil
  status = nil
  IO.popen(cmd, err: %i[child out]) do |io|
    output = io.read
    io.close
    status = $CHILD_STATUS
  end

  raise TmuxError, "tmux(#{args.inspect[1...-1]}) failed\n#{output}" unless status&.success?

  output
end