# File lib/powerbar.rb, line 33
  def initialize(opts={})
    @@exit_hooked = false
    @state = Hashie::Mash.new( {
      :time_last_show => Time.at(0),    # <- don't mess with us
      :time_last_update => Time.at(0),  # <- unless you know
      :time_start => nil,               # <- what you're doing!
      :time_now => nil,                 # <-
      :msg => 'PowerBar!',
      :done => 0,
      :total => :unknown,
      :settings => {
        :rate_sample_max_interval => 10,  # See PowerBar::Rate
        :rate_sample_window => 6,         # See PowerBar::Rate
        :force_mode => nil, # set to :tty or :notty to force either mode
        :kilo => 1024, # Change this to 1000 when measuring network traffic or such.
        :tty => {      # <== Settings when stdout is a tty
          :finite => { # <== Settings for a finite progress bar (when total != :unknown)
            # The :output Proc is called to draw on the screen --------------------.
            :output => Proc.new{ |s| $stderr.print s[0..terminal_width()-1] }, # <-'
            :interval => 0.1,  # Minimum interval between screen refreshes (in seconds)
            :show_eta => true, # Set to false if you want to hide the ETA without changing the template
            :template => { # <== template for a finite progress bar on a tty
              :pre  => "\e[1G\e[?25l",  # printed before the progress-bar
              #
              # :main is the progressbar template
              #
              # The following tokens are available:
              #   msg, bar, rate, percent, elapsed, eta, done, total
              #
              # Tokens may be used like so:
              #    ${<foo>}
              # OR:
              #    ${surrounding <foo> text}
              #
              # The surrounding text is only rendered when <foo>
              # evaluates to something other than nil.
              :main => '${<msg>}: ${[<bar>] }${<rate>/s }${<percent>% }${<elapsed>}${, ETA: <eta>}',
              :post => '',             # printed after the progressbar
              :wipe => "\e[0m\e[1G\e[K", # printed when 'wipe' is called
              :close => "\e[?25h\n",   # printed when 'close' is called
              :exit => "\e[?25h",      # printed if the process exits unexpectedly
              :barchar => RUBY18 ? '#' : "\u2588", # fill-char for the progress-bar
              :padchar => RUBY18 ? '.' : "\u2022"  # padding-char for the progress-bar
            },
          },
          :infinite => { # <== Settings for an infinite progress "bar" (when total is :unknown)
            :output => Proc.new{ |s| $stderr.print s[0..terminal_width()-1] },
            :interval => 0.1,
            :show_eta => false,
            :template => {
              :pre  => "\e[1G\e[?25l",
              :main => "${<msg>}: ${<done> }${<rate>/s }${<elapsed>}",
              :post => "\e[K",
              :wipe => "\e[0m\e[1G\e[K",
              :close => "\e[?25h\n",
              :exit => "\e[?25h",
              :barchar => RUBY18 ? '#' : "\u2588",
              :padchar => RUBY18 ? '.' : "\u2022"
            },
          }
        },
        :notty => { # <== Settings when stdout is not a tty
          :finite => {
            # You may want to hook in your favorite Logger-Library here. ---.
            :output => Proc.new{ |s| $stderr.print s },  # <----------------'
            :interval => 1,
            :show_eta => true,
            :line_width => 78, # Maximum output line width
            :template => {
              :pre  => '',
              :main => "${<msg>}: ${<done>}/${<total>}, ${<percent>%}${, <rate>/s}${, elapsed: <elapsed>}${, ETA: <eta>}\n",
              :post => '',
              :wipe => '',
              :close => nil,
              :exit => nil,
              :barchar => "#",
              :padchar => "."
            },
          },
          :infinite => {
            :output => Proc.new{ |s| $stderr.print s },
            :interval => 1,
            :show_eta => false,
            :line_width => 78,
            :template => {
              :pre  => "",
              :main => "${<msg>}: ${<done> }${<rate>/s }${<elapsed>}\n",
              :post => "",
              :wipe => "",
              :close => nil,
              :exit => nil,
              :barchar => "#",
              :padchar => "."
            },
          }
        }
      }
    }.merge(opts) )
  end