|
| 1 | +extern crate pretty_bytes; |
| 2 | +use ansi_term::Style; |
| 3 | +use chrono::{DateTime, Utc}; |
| 4 | +use pretty_bytes::converter::convert; |
| 5 | +use std::os::unix::fs::MetadataExt; |
| 6 | +use std::{fs}; |
| 7 | +use termion::color; |
| 8 | +use users::{get_current_uid, get_user_by_uid}; |
| 9 | + |
| 10 | +pub fn single(e: &std::path::PathBuf, size_count: usize) -> Result<(), Box<dyn std::error::Error>> { |
| 11 | + print!("{}", Style::new().underline().paint("permissions")); |
| 12 | + for _ in 0..2 { |
| 13 | + print!("{}", Style::new().underline().paint(" ")) |
| 14 | + } |
| 15 | + print!(" {}", Style::new().underline().paint("size")); |
| 16 | + for _ in 0..(size_count - 4) { |
| 17 | + print!("{}", Style::new().underline().paint(" ")) |
| 18 | + } |
| 19 | + |
| 20 | + print!(" {}", Style::new().underline().paint("modified")); |
| 21 | + |
| 22 | + for _ in 0..11 { |
| 23 | + print!("{}", Style::new().underline().paint(" ")) |
| 24 | + } |
| 25 | + |
| 26 | + print!(" {}", Style::new().underline().paint("user")); |
| 27 | + |
| 28 | + for _ in 0..(get_user_by_uid(get_current_uid()) |
| 29 | + .unwrap() |
| 30 | + .name() |
| 31 | + .to_str() |
| 32 | + .unwrap() |
| 33 | + .len() |
| 34 | + - 4) |
| 35 | + { |
| 36 | + print!("{}", Style::new().underline().paint(" ")) |
| 37 | + } |
| 38 | + |
| 39 | + print!(" {}", Style::new().underline().paint("name")); |
| 40 | + |
| 41 | + print!("\n"); |
| 42 | + |
| 43 | + let meta = fs::metadata(&e)?; |
| 44 | + let mode = meta.mode(); |
| 45 | + let user_has_write_access = mode & 0o200; |
| 46 | + let user_has_read_write_access = mode & 0o600; |
| 47 | + let group_has_read_access = mode & 0o040; |
| 48 | + let others_have_exec_access = mode & 0o001; |
| 49 | + let mut mode_count = 0; |
| 50 | + if user_has_write_access == 128 { |
| 51 | + print!("{}", color::Fg(color::Red)); |
| 52 | + print!("w"); |
| 53 | + print!("{}", color::Fg(color::White)); |
| 54 | + print!("-"); |
| 55 | + mode_count += 2; |
| 56 | + } |
| 57 | + if user_has_read_write_access == 384 { |
| 58 | + print!("{}", color::Fg(color::LightYellow)); |
| 59 | + print!("r"); |
| 60 | + print!("{}", color::Fg(color::LightRed)); |
| 61 | + print!("w"); |
| 62 | + print!("{}", color::Fg(color::White)); |
| 63 | + print!("-"); |
| 64 | + mode_count += 3; |
| 65 | + } |
| 66 | + if group_has_read_access == 32 { |
| 67 | + print!("{}", color::Fg(color::Green)); |
| 68 | + print!("x"); |
| 69 | + print!("{}", color::Fg(color::LightYellow)); |
| 70 | + print!("a"); |
| 71 | + print!("{}", color::Fg(color::White)); |
| 72 | + print!("-"); |
| 73 | + mode_count += 3; |
| 74 | + } |
| 75 | + if others_have_exec_access == 1 { |
| 76 | + print!("{}", color::Fg(color::Yellow)); |
| 77 | + print!("xw"); |
| 78 | + print!("{}", color::Fg(color::White)); |
| 79 | + print!("-"); |
| 80 | + mode_count += 3; |
| 81 | + } |
| 82 | + print!("{}", color::Fg(color::White)); |
| 83 | + print!("-@"); |
| 84 | + mode_count += 2; |
| 85 | + for _ in 0..(13 - mode_count) { |
| 86 | + print!(" ") |
| 87 | + } |
| 88 | + |
| 89 | + for _ in 0..(size_count - convert(fs::metadata(&e)?.size() as f64).len()) { |
| 90 | + print!(" ") |
| 91 | + } |
| 92 | + print!("{}", color::Fg(color::Green)); |
| 93 | + print!( |
| 94 | + " {}", |
| 95 | + Style::new() |
| 96 | + .bold() |
| 97 | + .paint(convert(fs::metadata(&e)?.size() as f64)) |
| 98 | + ); |
| 99 | + |
| 100 | + if let Ok(time) = e.metadata()?.modified() { |
| 101 | + print!("{}", color::Fg(color::LightRed)); |
| 102 | + let datetime: DateTime<Utc> = time.into(); |
| 103 | + print!(" {} ", datetime.format("%d-%m-%Y")); |
| 104 | + print!("{}", datetime.format("%T")) |
| 105 | + } |
| 106 | + |
| 107 | + print!("{}", color::Fg(color::Yellow)); |
| 108 | + print!( |
| 109 | + " {} ", |
| 110 | + Style::new().bold().paint( |
| 111 | + get_user_by_uid(get_current_uid()) |
| 112 | + .unwrap() |
| 113 | + .name() |
| 114 | + .to_str() |
| 115 | + .unwrap() |
| 116 | + ) |
| 117 | + ); |
| 118 | + |
| 119 | + print!("{}", color::Fg(color::White)); |
| 120 | + if e.metadata()?.is_dir() { |
| 121 | + print!("{}", color::Fg(color::LightBlue)); |
| 122 | + println!("{}/", &e.file_name().unwrap().to_str().unwrap()); |
| 123 | + } else { |
| 124 | + print!("{}", color::Fg(color::LightGreen)); |
| 125 | + println!( |
| 126 | + "{}", |
| 127 | + Style::new() |
| 128 | + .bold() |
| 129 | + .paint(e.file_name().unwrap().to_str().unwrap()) |
| 130 | + ); |
| 131 | + } |
| 132 | + Ok(()) |
| 133 | +} |
0 commit comments