file.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. use std::io::fs;
  2. use std::io;
  3. use colours::{Plain, Style, Black, Red, Green, Yellow, Blue, Purple, Cyan};
  4. use column::{Column, Permissions, FileName, FileSize, User, Group};
  5. use format::{formatBinaryBytes, formatDecimalBytes};
  6. use unix::{get_user_name, get_group_name};
  7. // Each file is definitely going to get `stat`ted at least once, if
  8. // only to determine what kind of file it is, so carry the `stat`
  9. // result around with the file for safe keeping.
  10. pub struct File<'a> {
  11. name: &'a str,
  12. path: &'a Path,
  13. stat: io::FileStat,
  14. }
  15. impl<'a> File<'a> {
  16. pub fn from_path(path: &'a Path) -> File<'a> {
  17. let filename: &str = path.filename_str().unwrap();
  18. // We have to use lstat here instad of file.stat(), as it
  19. // doesn't follow symbolic links. Otherwise, the stat() call
  20. // will fail if it encounters a link that's target is
  21. // non-existent.
  22. let stat: io::FileStat = match fs::lstat(path) {
  23. Ok(stat) => stat,
  24. Err(e) => fail!("Couldn't stat {}: {}", filename, e),
  25. };
  26. return File { path: path, stat: stat, name: filename };
  27. }
  28. pub fn is_dotfile(&self) -> bool {
  29. self.name.starts_with(".")
  30. }
  31. pub fn display(&self, column: &Column) -> ~str {
  32. match *column {
  33. Permissions => self.permissions(),
  34. FileName => self.file_colour().paint(self.name.to_owned()),
  35. FileSize(si) => self.file_size(si),
  36. User => get_user_name(self.stat.unstable.uid as i32).unwrap_or(self.stat.unstable.uid.to_str()),
  37. Group => get_group_name(self.stat.unstable.gid as u32).unwrap_or(self.stat.unstable.gid.to_str()),
  38. }
  39. }
  40. fn file_size(&self, si: bool) -> ~str {
  41. let sizeStr = if si {
  42. formatBinaryBytes(self.stat.size)
  43. } else {
  44. formatDecimalBytes(self.stat.size)
  45. };
  46. return if self.stat.kind == io::TypeDirectory {
  47. Green.normal()
  48. } else {
  49. Green.bold()
  50. }.paint(sizeStr);
  51. }
  52. fn type_char(&self) -> ~str {
  53. return match self.stat.kind {
  54. io::TypeFile => ".".to_owned(),
  55. io::TypeDirectory => Blue.paint("d"),
  56. io::TypeNamedPipe => Yellow.paint("|"),
  57. io::TypeBlockSpecial => Purple.paint("s"),
  58. io::TypeSymlink => Cyan.paint("l"),
  59. _ => "?".to_owned(),
  60. }
  61. }
  62. fn file_colour(&self) -> Style {
  63. if self.stat.kind == io::TypeDirectory {
  64. Blue.normal()
  65. } else if self.stat.perm.contains(io::UserExecute) {
  66. Green.normal()
  67. } else if self.name.ends_with("~") {
  68. Black.bold()
  69. } else {
  70. Plain
  71. }
  72. }
  73. fn permissions(&self) -> ~str {
  74. let bits = self.stat.perm;
  75. return format!("{}{}{}{}{}{}{}{}{}{}",
  76. self.type_char(),
  77. bit(bits, io::UserRead, "r", Yellow.bold()),
  78. bit(bits, io::UserWrite, "w", Red.bold()),
  79. bit(bits, io::UserExecute, "x", Green.bold().underline()),
  80. bit(bits, io::GroupRead, "r", Yellow.normal()),
  81. bit(bits, io::GroupWrite, "w", Red.normal()),
  82. bit(bits, io::GroupExecute, "x", Green.normal()),
  83. bit(bits, io::OtherRead, "r", Yellow.normal()),
  84. bit(bits, io::OtherWrite, "w", Red.normal()),
  85. bit(bits, io::OtherExecute, "x", Green.normal()),
  86. );
  87. }
  88. }
  89. fn bit(bits: io::FilePermission, bit: io::FilePermission, other: &'static str, style: Style) -> ~str {
  90. if bits.contains(bit) {
  91. style.paint(other.to_owned())
  92. } else {
  93. Black.bold().paint("-".to_owned())
  94. }
  95. }