class DuPretty::Prettier::DiskUsage

Attributes

kbyte[RW]
path[RW]
raw[RW]

Public Class Methods

new(raw, root, total: nil) click to toggle source
# File lib/du_pretty/prettier.rb, line 49
def initialize(raw, root, total: nil)
  @raw = raw
  @root = root
  @kbyte = raw.split("\t").compact[0].to_i
  @path = raw.split("\t").compact[1]
  @total = total
end

Public Instance Methods

pretty() click to toggle source
# File lib/du_pretty/prettier.rb, line 57
def pretty
  pretty_byte + "\t." + relative_path
end
tree_format() click to toggle source
# File lib/du_pretty/prettier.rb, line 61
def tree_format
  pretty_path + ' ' + pretty_byte
end

Private Instance Methods

basename() click to toggle source
# File lib/du_pretty/prettier.rb, line 67
def basename
  Pathname.new(@path).basename.to_s
end
depth() click to toggle source
# File lib/du_pretty/prettier.rb, line 71
def depth
  Pathname.new(relative_path).each_filename.to_a.size - 1
end
percentage() click to toggle source
# File lib/du_pretty/prettier.rb, line 108
def percentage
  percentage = @kbyte * 1.0 / @total * 100
  "#{percentage.round}%"
end
pretty_byte() click to toggle source
# File lib/du_pretty/prettier.rb, line 75
def pretty_byte
  mb = @kbyte * 1.0 / 1024
  gb = mb / 1024
  byte_format = ->(x) { x.to_i >= 10 ? x.to_i : x.round(1) }
  f = ->(value, unit, color) { "#{byte_format.call(value)}#{unit} (#{percentage})".send(color) }
  if gb >= 1.0
    if gb > 10
      f.call(gb, 'GB', :red)
    else
      f.call(gb, 'GB', :light_red)
    end
  elsif mb >= 1.0
    if mb > 500
      f.call(mb, 'MB', :yellow)
    else
      f.call(mb, 'MB', :light_yellow)
    end
  else
    if @kbyte > 500
      f.call(@kbyte, 'KB', :green)
    else
      f.call(@kbyte, 'KB', :light_green)
    end
  end
end
pretty_path() click to toggle source
# File lib/du_pretty/prettier.rb, line 101
def pretty_path
  return '.' if @path == @root
  spaces = depth.positive? ? '  ' * depth : ''
  tree_symbol = '└── '
  spaces + tree_symbol + basename
end
relative_path() click to toggle source
# File lib/du_pretty/prettier.rb, line 113
def relative_path
  @path.sub(/^#{@root}/, '')
end