Class: Terminal

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

Defined Under Namespace

Classes: Size

Constant Summary collapse

IOCTL_INPUT_BUF =
"\x00" * 8

Class Method Summary collapse

Class Method Details

.resize(direction, magnitude) ⇒ Object

These are experimental



13
14
15
# File 'lib/terminal_size.rb', line 13

def resize(direction, magnitude)
  tmux 'resize-pane', "-#{direction}", magnitude
end

.sizeObject



4
5
6
# File 'lib/terminal_size.rb', line 4

def size
  size_via_low_level_ioctl or size_via_stty or nil
end

.size!Object



8
9
10
# File 'lib/terminal_size.rb', line 8

def size!
  size or _height_width_hash_from 25, 80
end

.size_via_low_level_ioctlObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/terminal_size.rb', line 22

def size_via_low_level_ioctl
  # Thanks to runpaint for the general approach to this
  return unless $stdin.respond_to? :ioctl

  code = tiocgwinsz_value_for RUBY_PLATFORM
  return unless code

  buf = IOCTL_INPUT_BUF.dup
  return if $stdout.ioctl(code, buf) != 0
  return if buf == IOCTL_INPUT_BUF

  got = buf.unpack('S4')[0..1]
  _height_width_hash_from(*got)
rescue StandardError
  nil
end

.size_via_sttyObject



48
49
50
51
52
53
# File 'lib/terminal_size.rb', line 48

def size_via_stty
  ints = `stty size`.scan(/\d+/).map(&:to_i)
  _height_width_hash_from(*ints)
rescue StandardError
  nil
end

.tiocgwinsz_value_for(platform) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/terminal_size.rb', line 39

def tiocgwinsz_value_for(platform)
  # This is as reported by <sys/ioctl.h>
  # Hard-coding because it seems like overkll to acutally involve C for this.
  {
    /linux/ => 0x5413,
    /darwin/ => 0x40087468 # thanks to [email protected] for the lookup!
  }.find { |k, _v| platform[k] }
end

.tmux(*cmd) ⇒ Object



17
18
19
# File 'lib/terminal_size.rb', line 17

def tmux *cmd
  system 'tmux', *cmd.map(&:to_s)
end