class Rfd::Item

Attributes

dir[R]
index[RW]
name[R]
stat[R]

Public Class Methods

new(path: nil, dir: nil, name: nil, stat: nil, window_width: nil) click to toggle source
# File lib/rfd/item.rb, line 8
def initialize(path: nil, dir: nil, name: nil, stat: nil, window_width: nil)
  @path, @dir, @name, @stat, @window_width, @marked = path, dir || File.dirname(path), name || File.basename(path), stat, window_width, false
  @stat = File.lstat self.path unless stat
end

Public Instance Methods

<=>(o) click to toggle source
# File lib/rfd/item.rb, line 202
def <=>(o)
  if directory? && !o.directory?
    1
  elsif !directory? && o.directory?
    -1
  else
    name <=> o.name
  end
end
atime() click to toggle source
# File lib/rfd/item.rb, line 70
def atime
  stat.atime.strftime('%Y-%m-%d %H:%M:%S')
end
basename() click to toggle source
# File lib/rfd/item.rb, line 17
def basename
  @basename ||= File.basename name, extname
end
color() click to toggle source
# File lib/rfd/item.rb, line 48
def color
  if symlink?
    Curses::COLOR_MAGENTA
  elsif hidden?
    Curses::COLOR_GREEN
  elsif directory?
    Curses::COLOR_CYAN
  elsif executable?
    Curses::COLOR_RED
  else
    Curses::COLOR_WHITE
  end
end
ctime() click to toggle source
# File lib/rfd/item.rb, line 74
def ctime
  stat.ctime.strftime('%Y-%m-%d %H:%M:%S')
end
current_mark() click to toggle source
# File lib/rfd/item.rb, line 169
def current_mark
  marked? ? '*' : ' '
end
directory?() click to toggle source
# File lib/rfd/item.rb, line 102
def directory?
  @directory ||= if symlink?
    begin
      File.stat(path).directory?
    rescue Errno::ENOENT
      false
    end
  else
    stat.directory?
  end
end
display_name() click to toggle source
# File lib/rfd/item.rb, line 35
def display_name
  @display_name ||= begin
    n = full_display_name
    if mb_size(n) <= @window_width - 15
      n
    elsif symlink?
      mb_left n, @window_width - 16
    else
      "#{mb_left(basename, @window_width - 16 - extname.size)}…#{extname}"
    end
  end
end
executable?() click to toggle source
# File lib/rfd/item.rb, line 122
def executable?
  stat.executable?
end
extname() click to toggle source
# File lib/rfd/item.rb, line 21
def extname
  @extname ||= File.extname name
end
full_display_name() click to toggle source
# File lib/rfd/item.rb, line 29
def full_display_name
  n = @name.dup
  n << " -> #{target}" if symlink?
  n
end
gz?() click to toggle source
# File lib/rfd/item.rb, line 138
def gz?
  @gz_ ||= begin
    if directory?
      false
    else
      File.binread(realpath, 2).unpack('n').first == 0x1f8b
    end
  rescue
    false
  end
end
hidden?() click to toggle source
# File lib/rfd/item.rb, line 118
def hidden?
  name.start_with?('.') && (name != '.') && (name != '..')
end
join(*ary) click to toggle source
# File lib/rfd/item.rb, line 25
def join(*ary)
  File.join path, ary
end
marked?() click to toggle source
# File lib/rfd/item.rb, line 165
def marked?
  @marked
end
mb_char_size(c) click to toggle source
# File lib/rfd/item.rb, line 182
def mb_char_size(c)
  c == '…' ? 1 : c.bytesize == 1 ? 1 : 2
end
mb_left(str, size) click to toggle source
# File lib/rfd/item.rb, line 173
def mb_left(str, size)
  len = 0
  index = str.each_char.with_index do |c, i|
    break i if len + mb_char_size(c) > size
    len += mb_size c
  end
  str[0, index]
end
mb_ljust(str, size) click to toggle source
# File lib/rfd/item.rb, line 190
def mb_ljust(str, size)
  "#{str}#{' ' * [0, size - mb_size(str)].max}"
end
mb_size(str) click to toggle source
# File lib/rfd/item.rb, line 186
def mb_size(str)
  str.each_char.inject(0) {|l, c| l += mb_char_size(c)}
end
mode() click to toggle source
# File lib/rfd/item.rb, line 82
def mode
  @mode ||= begin
    m = stat.mode
    ft = directory? ? 'd' : symlink? ? 'l' : '-'
    ret = [(m & 0700) / 64, (m & 070) / 8, m & 07].inject(ft) do |str, s|
      str += "#{s & 4 == 4 ? 'r' : '-'}#{s & 2 == 2 ? 'w' : '-'}#{s & 1 == 1 ? 'x' : '-'}"
    end
    if m & 04000 != 0
      ret[3] = directory? ? 's' : 'S'
    end
    if m & 02000 != 0
      ret[6] = directory? ? 's' : 'S'
    end
    if m & 01000 == 512
      ret[-1] = directory? ? 't' : 'T'
    end
    ret
  end
end
mtime() click to toggle source
# File lib/rfd/item.rb, line 78
def mtime
  stat.mtime.strftime('%Y-%m-%d %H:%M:%S')
end
path() click to toggle source
# File lib/rfd/item.rb, line 13
def path
  @path ||= File.join @dir, @name
end
realpath() click to toggle source
# File lib/rfd/item.rb, line 154
def realpath
  @realpath ||= File.realpath path
end
size() click to toggle source
# File lib/rfd/item.rb, line 62
def size
  directory? ? 0 : stat.size
end
size_or_dir() click to toggle source
# File lib/rfd/item.rb, line 66
def size_or_dir
  directory? ? '<DIR>' : size.to_s
end
target() click to toggle source
# File lib/rfd/item.rb, line 150
def target
  File.readlink path if symlink?
end
to_s() click to toggle source
# File lib/rfd/item.rb, line 194
def to_s
  "#{current_mark}#{mb_ljust(display_name, @window_width - 15)}#{size_or_dir.rjust(13)}"
end
to_str() click to toggle source
# File lib/rfd/item.rb, line 198
def to_str
  path
end
toggle_mark() click to toggle source
# File lib/rfd/item.rb, line 158
def toggle_mark
  unless %w(. ..).include? name
    @marked = !@marked
    true
  end
end
zip?() click to toggle source
# File lib/rfd/item.rb, line 126
def zip?
  @zip_ ||= begin
    if directory?
      false
    else
      File.binread(realpath, 4).unpack('V').first == 0x04034b50
    end
  rescue
    false
  end
end