Repository navigation
Expand file tree
/
Copy pathui.rb
More file actions
108 lines (90 loc) · 2.85 KB
/
Copy pathui.rb
File metadata and controls
108 lines (90 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require 'thor/shell/basic'
require 'thor/shell/color'
## Concepts borrowed from Vagrant UI class.
module GitlabCli
module UI
class Interface
#[:ask, :warn, :error, :info, :success].each do |method|
# define_method(method) do |message, *opts|
# # Log normal console messages
# @logger.info { "#{method}: #{message}" }
# end
#end
end
class Basic < Interface
def initialize
#super
@shell = Thor::Shell::Basic.new
end
# Use some light meta-programming to create the various methods to
# output text to the UI. These all delegate the real functionality
# to `say`.
[:info, :warn, :error, :success].each do |method|
class_eval <<-CODE
def #{method}(message)
#super(message)
@shell.say(message)
end
CODE
end
def ask(message, opts=nil)
#super(message)
# We can't ask questions when the output isn't a TTY.
#raise Errors::UIExpectsTTY if !$stdin.tty? && !Vagrant::Util::Platform.cygwin?
# Setup the options so that the new line is suppressed
#opts ||= {}
#opts[:new_line] = false if !opts.has_key?(:new_line)
#opts[:prefix] = false if !opts.has_key?(:prefix)
# Output the data
#@shell.ask(:info, message, opts)
# Get the results and chomp off the newline. We do a logical OR
# here because `gets` can return a nil, for example in the case
# that ctrl-D is pressed on the input.
@shell.ask message
end
def yes?(message, color=nil)
@shell.yes? message, color
end
def handle_error(e, verbose=false)
error e.message
if verbose
e.backtrace.each do |line|
error line, :white
end
end
end
end
class Color < Basic
def initialize
#super
@shell = Thor::Shell::Color.new
end
# Terminal colors
COLORS = {
:clear => "\e[0m",
:red => "\e[31m",
:green => "\e[32m",
:yellow => "\e[33m"
}
# Mapping between type of message and the color to output
COLOR_MAP = {
:info => nil,
:warn => :yellow,
:error => :red,
:success => :green
}
# Use some light meta-programming to create the various methods to
# output text to the UI. These all delegate the real functionality
# to `say`.
[:info, :warn, :error, :success].each do |method|
class_eval <<-CODE
def #{method}(message,color=nil,force_new_line = (message.to_s !~ /( |\t)\Z/))
say_color = color.nil? ? COLOR_MAP[:#{method}] : color
#super(message)
@shell.say(message,say_color,force_new_line)
end
CODE
end
end
end
end