lib/cli/ui.rb in cli-ui-1.3.0 vs lib/cli/ui.rb in cli-ui-1.4.0
- old
+ new
@@ -1,12 +1,13 @@
module CLI
module UI
autoload :ANSI, 'cli/ui/ansi'
autoload :Glyph, 'cli/ui/glyph'
autoload :Color, 'cli/ui/color'
- autoload :Box, 'cli/ui/box'
autoload :Frame, 'cli/ui/frame'
+ autoload :OS, 'cli/ui/os'
+ autoload :Printer, 'cli/ui/printer'
autoload :Progress, 'cli/ui/progress'
autoload :Prompt, 'cli/ui/prompt'
autoload :Terminal, 'cli/ui/terminal'
autoload :Truncater, 'cli/ui/truncater'
autoload :Formatter, 'cli/ui/formatter'
@@ -26,47 +27,62 @@
def self.glyph(handle)
CLI::UI::Glyph.lookup(handle)
end
# Color resolution using +CLI::UI::Color.lookup+
- # Will lookup using +Color.lookup+ if a symbol, otherwise we assume it is a valid color and return it
+ # Will lookup using +Color.lookup+ unless it's already a CLI::UI::Color (or nil)
#
# ==== Attributes
#
# * +input+ - color to resolve
#
def self.resolve_color(input)
case input
- when Symbol
- CLI::UI::Color.lookup(input)
+ when CLI::UI::Color, nil
+ input
else
+ CLI::UI::Color.lookup(input)
+ end
+ end
+
+ # Frame style resolution using +CLI::UI::Frame::FrameStyle.lookup+.
+ # Will lookup using +FrameStyle.lookup+ unless it's already a CLI::UI::Frame::FrameStyle(or nil)
+ #
+ # ==== Attributes
+ #
+ # * +input+ - frame style to resolve
+ def self.resolve_style(input)
+ case input
+ when CLI::UI::Frame::FrameStyle, nil
input
+ else
+ CLI::UI::Frame::FrameStyle.lookup(input)
end
end
- # Conviencence Method for +CLI::UI::Prompt.confirm+
+ # Convenience Method for +CLI::UI::Prompt.confirm+
#
# ==== Attributes
#
# * +question+ - question to confirm
#
def self.confirm(question, **kwargs)
CLI::UI::Prompt.confirm(question, **kwargs)
end
- # Conviencence Method for +CLI::UI::Prompt.ask+
+ # Convenience Method for +CLI::UI::Prompt.ask+
#
# ==== Attributes
#
# * +question+ - question to ask
- # * +kwargs+ - arugments for +Prompt.ask+
+ # * +kwargs+ - arguments for +Prompt.ask+
#
def self.ask(question, **kwargs)
CLI::UI::Prompt.ask(question, **kwargs)
end
- # Conviencence Method to resolve text using +CLI::UI::Formatter.format+
+ # Convenience Method to resolve text using +CLI::UI::Formatter.format+
# Check +CLI::UI::Formatter::SGR_MAP+ for available formatting options
#
# ==== Attributes
#
# * +input+ - input to format
@@ -74,14 +90,14 @@
#
def self.resolve_text(input, truncate_to: nil)
return input if input.nil?
formatted = CLI::UI::Formatter.new(input).format
return formatted unless truncate_to
- return CLI::UI::Truncater.call(formatted, truncate_to)
+ CLI::UI::Truncater.call(formatted, truncate_to)
end
- # Conviencence Method to format text using +CLI::UI::Formatter.format+
+ # Convenience Method to format text using +CLI::UI::Formatter.format+
# Check +CLI::UI::Formatter::SGR_MAP+ for available formatting options
#
# https://user-images.githubusercontent.com/3074765/33799827-6d0721a2-dd01-11e7-9ab5-c3d455264afe.png
# https://user-images.githubusercontent.com/3074765/33799847-9ec03fd0-dd01-11e7-93f7-5f5cc540e61e.png
#
@@ -95,33 +111,44 @@
#
def self.fmt(input, enable_color: enable_color?)
CLI::UI::Formatter.new(input).format(enable_color: enable_color)
end
- # Conviencence Method for +CLI::UI::Frame.open+
+ # Convenience Method for +CLI::UI::Printer.puts+
#
# ==== Attributes
#
+ # * +msg+ - Message to print
+ # * +kwargs+ - keyword arguments for +Printer.puts+
+ #
+ def self.puts(msg, **kwargs)
+ CLI::UI::Printer.puts(msg, **kwargs)
+ end
+
+ # Convenience Method for +CLI::UI::Frame.open+
+ #
+ # ==== Attributes
+ #
# * +args+ - arguments for +Frame.open+
# * +block+ - block for +Frame.open+
#
- def self.frame(*args, &block)
- CLI::UI::Frame.open(*args, &block)
+ def self.frame(*args, **kwargs, &block)
+ CLI::UI::Frame.open(*args, **kwargs, &block)
end
- # Conviencence Method for +CLI::UI::Spinner.spin+
+ # Convenience Method for +CLI::UI::Spinner.spin+
#
# ==== Attributes
#
# * +args+ - arguments for +Spinner.open+
# * +block+ - block for +Spinner.open+
#
- def self.spinner(*args, &block)
- CLI::UI::Spinner.spin(*args, &block)
+ def self.spinner(*args, **kwargs, &block)
+ CLI::UI::Spinner.spin(*args, **kwargs, &block)
end
- # Conviencence Method to override frame color using +CLI::UI::Frame.with_frame_color+
+ # Convenience Method to override frame color using +CLI::UI::Frame.with_frame_color+
#
# ==== Attributes
#
# * +color+ - color to override to
# * +block+ - block for +Frame.with_frame_color_override+
@@ -141,11 +168,11 @@
raise "multiple logs not allowed"
end
CLI::UI::StdoutRouter.duplicate_output_to = File.open(path, 'w')
yield
ensure
- if file_descriptor = CLI::UI::StdoutRouter.duplicate_output_to
+ if (file_descriptor = CLI::UI::StdoutRouter.duplicate_output_to)
file_descriptor.close
CLI::UI::StdoutRouter.duplicate_output_to = nil
end
end
@@ -180,9 +207,22 @@
def self.enable_color=(bool)
@enable_color = !!bool
end
self.enable_color = $stdout.tty?
+
+ # Set the default frame style.
+ # Convenience method for setting the default frame style with +CLI::UI::Frame.frame_style=+
+ #
+ # Raises ArgumentError if +frame_style+ is not valid
+ #
+ # ==== Attributes
+ #
+ # * +symbol+ - the default frame style to use for frames
+ #
+ def self.frame_style=(frame_style)
+ Frame.frame_style = frame_style.to_sym
+ end
end
end
require 'cli/ui/stdout_router'