Sha256: 5626439982e20b3e1df64788eadbe57d183171a2928a506d5c38dc363cf1880f

Contents?: true

Size: 1.83 KB

Versions: 19

Compression:

Stored size: 1.83 KB

Contents

class Rfix::Box < Struct.new(:out, :err, :status, :args, :quiet)
  include Rfix::Log

  def render(color: :reset, debug: false)
    return if Rfix.debug? && !debug

    @render ||= box(title, color: color) do
      margin do
        prt cmd_lines
      end

      div("{{info:STDOUT}}") do
        prt lines_or_none(stdout)
      end

      div("{{error:STDERR}}") do
        prt lines_or_none(stderr)
      end
    end
  end

  def lines_or_none(lines)
    if lines.join.chomp.empty?
      return "{{italic:<none>}}"
    end

    lines.map(&method(:strip)).join("\n")
  end

  def title
    "#{quiet_icon}#{icon} #{bin} (#{exit_status}) @ {{italic:#{pwd}}}"
  end

  def quiet_icon
    return "" unless quiet

    "{{warning:[silent]}} "
  end

  def stdout
    @stdout ||= dumpable(out.lines.map(&:chomp))
  end

  def stderr
    @stderr ||= dumpable(err.lines.map(&:chomp))
  end

  def dumpable(lines)
    box = self
    lines.tap do
      lines.define_singleton_method(:dump!) do
        tap do
          box.render
        end
      end
    end
  end

  def icon
    success? ? "{{v}}" : "{{x}}"
  end

  def success?
    status.success?
  end

  def bin
    clean_args.first
  end

  def tail
    clean_args[1..-1]
  end

  def exit_status
    status.exitstatus
  end

  def pwd
    if Dir.getwd == Dir.pwd
      return "{{italic:#{Dir.getwd}}}"
    end

    Dir.getwd.sub(Dir.pwd, "")
  end

  private

  def longest_arg
    @longest_arg ||= clean_args.reduce(0) do |acc, arg|
      acc < arg.length ? arg.length : acc
    end
  end

  def clean_args
    @clean_args ||= args.map(&method(:strip))
  end

  def rest
    tail.each_slice(2).map do |args|
      args.map do |arg|
        arg.ljust(longest_arg + 5)
      end.join(" ")
    end.map do |tail|
      "  {{italic:#{tail}}}"
    end.join("\n")
  end

  def cmd_lines
    "{{command:$ #{bin} \n#{rest}}}"
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
rfix-1.4.1 lib/rfix/box.rb
rfix-1.4.0.pre.201 lib/rfix/box.rb
rfix-1.4.0 lib/rfix/box.rb
rfix-1.3.0.pre.199 lib/rfix/box.rb
rfix-1.2.6.pre.198 lib/rfix/box.rb
rfix-1.2.6 lib/rfix/box.rb
rfix-1.2.2.pre.174 lib/rfix/box.rb
rfix-1.2.5 lib/rfix/box.rb
rfix-1.2.4 lib/rfix/box.rb
rfix-1.2.3.pre lib/rfix/box.rb
rfix-1.2.2.pre lib/rfix/box.rb
rfix-1.2.2.pre.172 lib/rfix/box.rb
rfix-1.2.2 lib/rfix/box.rb
rfix-1.1.0.pre.150 lib/rfix/box.rb
rfix-1.2.0 lib/rfix/box.rb
rfix-1.1.4.pre lib/rfix/box.rb
rfix-1.1.1.pre lib/rfix/box.rb
rfix-1.1.0.pre.149 lib/rfix/box.rb
rfix-1.1.0.pre.147 lib/rfix/box.rb