Class: TermInfo

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term = ENV['TERM'], io = STDERR) ⇒ TermInfo

Returns a new instance of TermInfo.



54
55
56
57
58
# File 'lib/terminfo.rb', line 54

def initialize(term=ENV['TERM'], io=STDERR)
  setupterm(term, io.fileno)
  @term = term
  @io = io
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



59
60
61
# File 'lib/terminfo.rb', line 59

def io
  @io
end

Class Method Details

.control(*args) ⇒ Object



44
# File 'lib/terminfo.rb', line 44

def TermInfo.control(*args) default_object.control(*args) end

.control_string(*args) ⇒ Object



43
# File 'lib/terminfo.rb', line 43

def TermInfo.control_string(*args) default_object.control_string(*args) end

.default_objectObject



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

def TermInfo.default_object
  unless defined? @default_terminfo
    io = open("/dev/tty", "r+")
    io.sync = true
    @default_terminfo = TermInfo.new(ENV['TERM'], io)
  end
  @default_terminfo
end

.flush(&block) ⇒ Object



46
# File 'lib/terminfo.rb', line 46

def TermInfo.flush(&block) default_object.flush(&block) end

.ioObject



52
# File 'lib/terminfo.rb', line 52

def TermInfo.io() default_object.io() end

.screen_columnsObject



50
# File 'lib/terminfo.rb', line 50

def TermInfo.screen_columns() default_object.screen_columns() end

.screen_heightObject



49
# File 'lib/terminfo.rb', line 49

def TermInfo.screen_height() default_object.screen_height() end

.screen_linesObject



48
# File 'lib/terminfo.rb', line 48

def TermInfo.screen_lines() default_object.screen_lines() end

.screen_sizeObject



47
# File 'lib/terminfo.rb', line 47

def TermInfo.screen_size() default_object.screen_size() end

.screen_widthObject



51
# File 'lib/terminfo.rb', line 51

def TermInfo.screen_width() default_object.screen_width() end

.write(str) ⇒ Object



45
# File 'lib/terminfo.rb', line 45

def TermInfo.write(str) default_object.write(str) end

Instance Method Details

#control(*args) ⇒ Object

TermInfo#control controls a terminal.

It prints the result of control_string to io specified at initialization.



86
87
88
89
# File 'lib/terminfo.rb', line 86

def control(*args)
  @io.write(self.control_string(*args))
  nil
end

#control_string(*args) ⇒ Object

TermInfo#control_string return a string to control terminal.

TermInfo#control_string([afflines,] capname, p1, p2, ...)

capname is a terminfo string capability such as “cuu”, “el”.

p1, p2, … are parameters for the capability.

afflines is a number of lines affected. (used for determining padding length)

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
# File 'lib/terminfo.rb', line 74

def control_string(*args)
  afflines = 1
  raise ArgumentError, "capname requried" if args.empty?
  afflines = args.shift.to_i if args.first.respond_to?(:to_int)
  raise ArgumentError, "capname not given" if !args.first.respond_to?(:to_str)
  capname = args.shift.to_str 
  self.tputs(self.tparm(self.tigetstr(capname), *args), afflines)
end

#flushObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/terminfo.rb', line 95

def flush
  oldlevel = nil
  if block_given?
    oldlevel = Thread.current[:TermInfo_Flush_level]
    oldsync = @io.sync
    begin
      Thread.current[:TermInfo_Flush_level] = (oldlevel || 0) + 1
      @io.sync = false
      yield
    ensure
      Thread.current[:TermInfo_Flush_level] = oldlevel
      @io.sync = oldsync
    end
  end
  @io.flush if oldlevel == nil
  nil
end

#inspectObject



61
62
63
# File 'lib/terminfo.rb', line 61

def inspect
  "\#<#{self.class}:#{@term}>"
end

#screen_columnsObject Also known as: screen_width

returns terminal screen width.



136
137
138
# File 'lib/terminfo.rb', line 136

def screen_columns
  self.screen_size[1]
end

#screen_linesObject Also known as: screen_height

returns terminal screen height.



130
131
132
# File 'lib/terminfo.rb', line 130

def screen_lines
  self.screen_size[0]
end

#screen_sizeObject

returns terminal screen size in a two element array: [lines, columns].



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/terminfo.rb', line 114

def screen_size
  begin
    size = TermInfo.tiocgwinsz(@io)
  rescue NotImplementedError
    size = [0,0]
  end
  if size[0] == 0
    size[0] = ENV.include?('LINES') ? ENV['LINES'].to_i : self.tigetnum("lines")
  end
  if size[1] == 0
    size[1] = ENV.include?('COLUMNS') ? ENV['COLUMNS'].to_i : self.tigetnum("cols")
  end
  size
end

#write(str) ⇒ Object



91
92
93
# File 'lib/terminfo.rb', line 91

def write(str)
  @io.write(str)
end