Class: RubyJard::Console

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

Overview

Wrapper for utilities to control screen

Class Method Summary collapse

Class Method Details

.attachable?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
# File 'lib/ruby_jard/console.rb', line 11

def attachable?
  return false unless output.tty?

  width, height = screen_size(output)
  width != 0 && height != 0
end

.cached_tputObject



155
156
157
# File 'lib/ruby_jard/console.rb', line 155

def cached_tput
  @cached_tput ||= {}
end

.clear_screen(output) ⇒ Object



69
70
71
72
73
# File 'lib/ruby_jard/console.rb', line 69

def clear_screen(output)
  return unless output.tty?

  output.print "\e[3J"
end

.clear_screen_to_end(output) ⇒ Object



75
76
77
78
79
# File 'lib/ruby_jard/console.rb', line 75

def clear_screen_to_end(output)
  return unless output.tty?

  output.print "\e[0J"
end

.cooked!(output) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/ruby_jard/console.rb', line 122

def cooked!(output)
  return unless output.tty?

  begin
    output.cooked!
  rescue StandardError
    # If stty not found, or raise error, nothing I can do
    stty('-raw')
  end
end

.disable_cursor!(output) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/ruby_jard/console.rb', line 81

def disable_cursor!(output)
  return unless output.tty?

  output.print tput('civis')
rescue RubyJard::Error
  # If tput not found, fallback to hard-coded sequence.
  output.print "\e[?25l"
end

.disable_echo!(output) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/ruby_jard/console.rb', line 133

def disable_echo!(output)
  return unless output.tty?

  begin
    output.echo = false
  rescue StandardError
    # If stty not found, or raise error, nothing I can do
    stty('-echo')
  end
end

.enable_cursor!(output) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/ruby_jard/console.rb', line 90

def enable_cursor!(output)
  return unless output.tty?

  output.print tput('cnorm')
rescue RubyJard::Error
  # If tput not found, fallback to hard-coded sequence.
  output.print "\e[?12l\e[?25h"
end

.enable_echo!(output) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# File 'lib/ruby_jard/console.rb', line 144

def enable_echo!(output)
  return unless output.tty?

  begin
    output.echo = true
  rescue StandardError
    # If stty not found, or raise error, nothing I can do
    stty('echo')
  end
end

.getch(input, timeout) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ruby_jard/console.rb', line 99

def getch(input, timeout)
  input.read_nonblock(255)
rescue IO::WaitReadable
  io = IO.select([input], nil, nil, timeout)
  if io.nil?
    nil
  else
    retry
  end
rescue IO::WaitWritable
  nil
end

.inputObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_jard/console.rb', line 37

def input
  return @input if defined?(@input)

  @input =
    if STDIN.tty?
      STDIN
    else
      begin
        File.open('/dev/tty', 'r+')
      rescue StandardError
        STDIN # Give up.
      end
    end
end

.move_to(output, x, y) ⇒ Object



52
53
54
55
56
# File 'lib/ruby_jard/console.rb', line 52

def move_to(output, x, y)
  return unless output.tty?

  output.print format("\e[%<row>d;%<col>dH", row: y + 1, col: x + 1)
end

.outputObject



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

def output
  return @output if defined?(@output)

  @output =
    if STDOUT.tty?
      STDOUT
    else
      begin
        File.open('/dev/tty', 'w+')
      rescue StandardError
        STDOUT # Give up now.
      end
    end
end

.raw!(output) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/ruby_jard/console.rb', line 112

def raw!(output)
  return unless output.tty?

  begin
    output.raw!
  rescue StandardError
    stty('raw')
  end
end

.redirected?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/ruby_jard/console.rb', line 18

def redirected?
  output != $stdout
end

.screen_size(output) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_jard/console.rb', line 58

def screen_size(output)
  return [0, 0] unless output.tty?

  if output.respond_to?(:winsize)
    height, width = output.winsize
    [width, height]
  else
    [TTY::Screen.width, TTY::Screen.height]
  end
end

.stty(*args) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ruby_jard/console.rb', line 173

def stty(*args)
  command = "stty #{args.join(' ')}"
  output = `#{command}`
  if $CHILD_STATUS.success?
    output
  else
    raise RubyJard::Error, "Fail to call `#{command}`: #{$CHILD_STATUS}"
  end
rescue StandardError => e
  raise RubyJard::Error, "Fail to call `#{command}`. Error: #{e}"
end

.tput(*args) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ruby_jard/console.rb', line 159

def tput(*args)
  command = "tput #{args.join(' ')}"
  return cached_tput[command] unless cached_tput[command].nil?

  output = `#{command}`
  if $CHILD_STATUS.success?
    cached_tput[command] = output
  else
    raise RubyJard::Error, "Fail to call `#{command}`: #{$CHILD_STATUS}"
  end
rescue StandardError => e
  raise RubyJard::Error, "Fail to call `#{command}`. Error: #{e}"
end