Module: Choosy::Terminal

Included in:
Printing::BasePrinter
Defined in:
lib/choosy/terminal.rb

Constant Summary collapse

DEFAULT_LINE_COUNT =
25
DEFAULT_COLUMN_COUNT =
80

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columnsObject



12
13
14
# File 'lib/choosy/terminal.rb', line 12

def columns
  @columns ||= find_terminal_size('COLUMNS', 'cols', 1) || DEFAULT_COLUMN_COUNT
end

#linesObject



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

def lines
  @lines ||= find_terminal_size('LINES', 'lines', 0) || DEFAULT_LINE_COUNT
end

Class Method Details

.die(message) ⇒ Object



110
111
112
# File 'lib/choosy/terminal.rb', line 110

def self.die(message)
  raise Choosy::ClientExecutionError.new(message)
end

Instance Method Details

#colorObject



16
17
18
# File 'lib/choosy/terminal.rb', line 16

def color
  @color ||= Choosy::Printing::Color.new
end

#command_exists?(command) ⇒ Boolean

directly from hirb

Returns:

  • (Boolean)


87
88
89
# File 'lib/choosy/terminal.rb', line 87

def command_exists?(command)
  ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
end

#die(message) ⇒ Object



106
107
108
# File 'lib/choosy/terminal.rb', line 106

def die(message)
  raise Choosy::ClientExecutionError.new(message)
end

#page(contents, pipe_command = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/choosy/terminal.rb', line 43

def page(contents, pipe_command=nil)
  if pager?
    if pipe_command
      pipe_out("#{pipe_command} | #{pager}", contents)
    else
      pipe_out(pager, contents)
    end
  else
    pipe_out(pipe_command, contents)
  end
end

#pagerObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/choosy/terminal.rb', line 24

def pager
  @pager ||= nil
  return @pager if @pager

  @pager ||= ENV['PAGER'] || 
              ENV['MANPAGER'] ||
              if command_exists?('less')
                'less -R'
              elsif command_exists?('more')
                'more'
              else
                ''
              end
  if @pager !~ /less -R/
    color.disable! 
  end
  @pager
end

#pager?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/choosy/terminal.rb', line 20

def pager?
  !pager.empty?
end

#pipe_in(command = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/choosy/terminal.rb', line 66

def pipe_in(command = nil, &block)
  raise ArgumentError.new("Requires a block") unless block_given?

  if command
    IO.popen(command, 'r') do |f|
      f.each_line do |line|
        yield line
      end
    end
    $?
  elsif stdin?
    STDIN.each_line do |line|
      yield line
    end
    0
  else
    1
  end
end

#pipe_out(command, contents = nil, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/choosy/terminal.rb', line 55

def pipe_out(command, contents = nil, &block)
  puts contents if command.nil? && contents

  IO.popen(command, 'w') do |f|
    f.puts contents if contents
    if block_given?
      yield f
    end
  end
end

#stdin?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
# File 'lib/choosy/terminal.rb', line 91

def stdin?
  begin
    require 'fcntl'
    STDIN.fcntl(Fcntl::F_GETFL, 0) == 0 && !$stdin.tty?
  rescue
    $stdin.stat.size != 0
  end
end

#unformatted(line = '') ⇒ Object



100
101
102
103
104
# File 'lib/choosy/terminal.rb', line 100

def unformatted(line='')
  line = line.gsub(/\e\[\d+m/, '')
  line.gsub!(/\\f[IPB]/, '')
  line
end