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

bars[RW]

The length of the progress bar.

@return [Integer]

block_fg[RW]

The color of the optional block return value.

@return [Integer]

cancel_fg[RW]

The color of the time position info on an interrupted task.

@return [Integer]

empty_chr[RW]

The string to be used to draw the remaining part of the bar.

@return [String]

empty_fg[RW]

The color of the remaining part of the bar.

@return [Integer]

end_fg[RW]

The color of the beginning and end of a section.

@return [Integer]

finish_fg[RW]

The color of the time position info on a completed task.

@return [Integer]

full_chr[RW]

The string to be used to draw the filled part of the bar.

@return [String]

full_fg[RW]

The color of the filled part of the bar.

@return [Integer]

head_chr[RW]

The string to be used as the start of a section.

@return [String]

tail_chr[RW]

The string to be used as the end of a section.

@return [String]

time_fg[RW]

The color of the current time position info.

@return [Integer]

Public Class Methods

from(object) click to toggle source

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
new(theme_hash = DEFAULT_THEME) click to toggle source

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

bar(position, time_string = '00:00:00.00', option_hash = {}) click to toggle source

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(string, color) click to toggle source

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
empty(position) click to toggle source

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
full(position) click to toggle source

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
head() click to toggle source

Returns a colorized section start element.

@return [String]

# File lib/ffmpeg_progress/theme.rb, line 157
def head
  colorize(@head_chr, @end_fg)
end
tail() click to toggle source

Returns a colorized section end element.

@return [String]

# File lib/ffmpeg_progress/theme.rb, line 164
def tail
  colorize(@tail_chr, @end_fg)
end
time(time_string, option_hash = {}) click to toggle source

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