class FFmpegProgress::Theme
This is the class that handles progress bar themes and converting them into visual elements. {FFmpegProgress} expects a 256-color capable terminal.
Constants
- DEFAULT_THEME
The default theme.
Attributes
The length of the progress bar.
@return [Integer]
The color of the optional block return value.
@return [Integer]
The color of the time position info on an interrupted task.
@return [Integer]
The string to be used to draw the remaining part of the bar.
@return [String]
The color of the remaining part of the bar.
@return [Integer]
The color of the beginning and end of a section.
@return [Integer]
The color of the time position info on a completed task.
@return [Integer]
The string to be used to draw the filled part of the bar.
@return [String]
The color of the filled part of the bar.
@return [Integer]
The string to be used as the start of a section.
@return [String]
The string to be used as the end of a section.
@return [String]
The color of the current time position info.
@return [Integer]
Public Class Methods
Generates a new instance of Theme
. Returns the argument if the argument is a Theme
.
@param [Hash, Theme] object @return [Theme]
# File lib/ffmpeg_progress/theme.rb, line 77 def self.from(object) return object if object.is_a? Theme return new(object) if object.is_a? Hash fail 'Argument must be either Theme or Hash.' end
Generates a new instance of Theme
from a theme hash.
@param [Hash] theme_hash @return [Theme]
# File lib/ffmpeg_progress/theme.rb, line 87 def initialize(theme_hash = DEFAULT_THEME) DEFAULT_THEME.merge(theme_hash).each_pair do |key, value| method("#{key}=").call(value) if DEFAULT_THEME.key? key end end
Public Instance Methods
Returns a progress bar given a fractional position, a time string, and an optional block output string.
@param [Float] position @param [String] time_string @param [Hash] option_hash @option option_hash [String] :block_output the output from the optional
block passed to {FFmpeg#run}, if any.
@option option_hash [Boolean] :interrupted has the task been canceled? @option option_hash [Boolean] :finished has the task finished? @return [String]
# File lib/ffmpeg_progress/theme.rb, line 104 def bar(position, time_string = '00:00:00.00', option_hash = {}) output = "#{head}#{full(position)}#{empty(position)}#{tail}" \ "#{head}#{time(time_string, option_hash)}#{tail}" if option_hash.fetch :block_output, false output << "#{head}#{colorize(option_hash[:block_output], @block_fg)}" \ "#{tail}" end output end
Private Instance Methods
Colorize a string for the terminal (256-color mode).
@param [String] string @param [Integer] color @return [String]
# File lib/ffmpeg_progress/theme.rb, line 173 def colorize(string, color) "\x1b[38;5;#{color}m#{string}\x1b[0m" end
Returns the empty part of the progress bar given a fractional position.
@param [Float] position @return [String]
# File lib/ffmpeg_progress/theme.rb, line 150 def empty(position) colorize(@empty_chr * (@bars - (position * @bars).round), @empty_fg) end
Returns the filled part of the progress bar given a fractional position.
@param [Float] position @return [String]
# File lib/ffmpeg_progress/theme.rb, line 142 def full(position) colorize(@full_chr * (position * @bars).round, @full_fg) end
Returns a colorized section start element.
@return [String]
# File lib/ffmpeg_progress/theme.rb, line 157 def head colorize(@head_chr, @end_fg) end
Returns a colorized section end element.
@return [String]
# File lib/ffmpeg_progress/theme.rb, line 164 def tail colorize(@tail_chr, @end_fg) end
Returns the contents of the time section.
@param [String] time_string @param [Hash] option_hash @option option_hash [Boolean] :interrupted has the task been canceled? @option option_hash [Boolean] :finished has the task finished? @return [String]
# File lib/ffmpeg_progress/theme.rb, line 125 def time(time_string, option_hash = {}) fg_color = if option_hash.fetch :interrupted, false @cancel_fg elsif option_hash.fetch :finished, false @finish_fg else @time_fg end colorize(time_string, fg_color) end