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



10
11
12
# File 'lib/terminal-size.rb', line 10

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



7
# File 'lib/terminal-size.rb', line 7

def size!; size or _height_width_hash_from 25, 80 end

.size_via_low_level_ioctlObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/terminal-size.rb', line 19

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 unless $stdout.ioctl(code, buf).zero?
  return if IOCTL_INPUT_BUF == buf
  got = buf.unpack('S4')[0..1]
  _height_width_hash_from *got
rescue
  nil
end

.size_via_sttyObject



42
43
44
45
46
47
# File 'lib/terminal-size.rb', line 42

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

.tiocgwinsz_value_for(platform) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/terminal-size.rb', line 33

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



14
15
16
# File 'lib/terminal-size.rb', line 14

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