Class: Tb::Pager
- Inherits:
-
Object
- Object
- Tb::Pager
- Defined in:
- lib/tb/pager.rb
Constant Summary collapse
- DEFAULT_LINES =
24
- DEFAULT_COLUMNS =
80
Class Method Summary collapse
Instance Method Summary collapse
- #<<(obj) ⇒ Object
- #close ⇒ Object
- #expand_tab(str, tabstop = 8) ⇒ Object
- #flush ⇒ Object
-
#initialize ⇒ Pager
constructor
A new instance of Pager.
- #print(*args) ⇒ Object
- #printf(format, *args) ⇒ Object
- #putc(ch) ⇒ Object
- #puts(*objs) ⇒ Object
- #single_screen?(str) ⇒ Boolean
- #winsize ⇒ Object
- #write(str) ⇒ Object
- #write_nonblock(str) ⇒ Object
Constructor Details
#initialize ⇒ Pager
Returns a new instance of Pager.
16 17 18 19 20 21 22 23 24 |
# File 'lib/tb/pager.rb', line 16 def initialize if $stdout.tty? @io = nil @buf = '' else @io = $stdout @buf = nil end end |
Class Method Details
.open ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/tb/pager.rb', line 7 def self.open pager = self.new begin yield pager ensure pager.close end end |
Instance Method Details
#<<(obj) ⇒ Object
26 27 28 29 |
# File 'lib/tb/pager.rb', line 26 def <<(obj) write obj.to_s self end |
#close ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/tb/pager.rb', line 123 def close if !@io $stdout.print @buf else # don't need to ouput @buf because @buf is nil. @io.close if @io != $stdout end nil end |
#expand_tab(str, tabstop = 8) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tb/pager.rb', line 69 def (str, tabstop=8) col = 0 str.gsub(/(\t+)|[^\t]+/) { if $1 ' ' * (($1.length * tabstop) - (col + 1) % tabstop) else $& end } end |
#flush ⇒ Object
118 119 120 121 |
# File 'lib/tb/pager.rb', line 118 def flush @io.flush if @io self end |
#print(*args) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/tb/pager.rb', line 31 def print(*args) s = '' args.map {|a| s << a.to_s } write s nil end |
#printf(format, *args) ⇒ Object
38 39 40 41 |
# File 'lib/tb/pager.rb', line 38 def printf(format, *args) write sprintf(format, *args) nil end |
#putc(ch) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/tb/pager.rb', line 43 def putc(ch) if Integer === ch write [ch].pack("C") else write ch.to_s end ch end |
#puts(*objs) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tb/pager.rb', line 52 def puts(*objs) if objs.empty? write "\n" else objs.each {|o| o = o.to_s write o write "\n" if /\n\z/ !~ o } end nil end |
#single_screen?(str) ⇒ Boolean
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/tb/pager.rb', line 91 def single_screen?(str) lines, columns = winsize n = 0 str.each_line {|line| line = (line).chomp cols = line.length # xxx: 1 column/character assumed. cols = 1 if cols == 0 m = (cols + columns - 1) / columns # termcap am capability is assumed. n += m } n <= lines-1 end |
#winsize ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/tb/pager.rb', line 83 def winsize if $stdout.respond_to? :winsize lines, columns = $stdout.winsize return [lines, columns] if lines != 0 && columns != 0 end [DEFAULT_LINES, DEFAULT_COLUMNS] end |
#write(str) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/tb/pager.rb', line 104 def write(str) str = str.to_s if !@io @buf << str if !single_screen?(@buf) @io = IO.popen(ENV['PAGER'] || 'more', 'w') @io << @buf @buf = nil end else @io << str end end |
#write_nonblock(str) ⇒ Object
65 66 67 |
# File 'lib/tb/pager.rb', line 65 def write_nonblock(str) write str.to_s end |