table.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. use std::cmp::max;
  2. use std::env;
  3. use std::ops::Deref;
  4. use std::sync::{Mutex, MutexGuard};
  5. use datetime::TimeZone;
  6. use zoneinfo_compiled::{CompiledData, Result as TZResult};
  7. use lazy_static::lazy_static;
  8. use log::*;
  9. use users::UsersCache;
  10. use crate::fs::{File, fields as f};
  11. use crate::fs::feature::git::GitCache;
  12. use crate::output::cell::TextCell;
  13. use crate::output::render::TimeRender;
  14. use crate::output::time::TimeFormat;
  15. use crate::theme::Theme;
  16. /// Options for displaying a table.
  17. #[derive(PartialEq, Debug)]
  18. pub struct Options {
  19. pub size_format: SizeFormat,
  20. pub time_format: TimeFormat,
  21. pub columns: Columns,
  22. }
  23. /// Extra columns to display in the table.
  24. #[derive(PartialEq, Debug, Copy, Clone)]
  25. pub struct Columns {
  26. /// At least one of these timestamps will be shown.
  27. pub time_types: TimeTypes,
  28. // The rest are just on/off
  29. pub inode: bool,
  30. pub links: bool,
  31. pub blocks: bool,
  32. pub group: bool,
  33. pub git: bool,
  34. pub octal: bool,
  35. // Defaults to true:
  36. pub permissions: bool,
  37. pub filesize: bool,
  38. pub user: bool,
  39. }
  40. impl Columns {
  41. pub fn collect(&self, actually_enable_git: bool) -> Vec<Column> {
  42. let mut columns = Vec::with_capacity(4);
  43. if self.inode {
  44. columns.push(Column::Inode);
  45. }
  46. if self.octal {
  47. columns.push(Column::Octal);
  48. }
  49. if self.permissions {
  50. columns.push(Column::Permissions);
  51. }
  52. if self.links {
  53. columns.push(Column::HardLinks);
  54. }
  55. if self.filesize {
  56. columns.push(Column::FileSize);
  57. }
  58. if self.blocks {
  59. columns.push(Column::Blocks);
  60. }
  61. if self.user {
  62. columns.push(Column::User);
  63. }
  64. if self.group {
  65. columns.push(Column::Group);
  66. }
  67. if self.time_types.modified {
  68. columns.push(Column::Timestamp(TimeType::Modified));
  69. }
  70. if self.time_types.changed {
  71. columns.push(Column::Timestamp(TimeType::Changed));
  72. }
  73. if self.time_types.created {
  74. columns.push(Column::Timestamp(TimeType::Created));
  75. }
  76. if self.time_types.accessed {
  77. columns.push(Column::Timestamp(TimeType::Accessed));
  78. }
  79. if cfg!(feature = "git") && self.git && actually_enable_git {
  80. columns.push(Column::GitStatus);
  81. }
  82. columns
  83. }
  84. }
  85. /// A table contains these.
  86. #[derive(Debug, Copy, Clone)]
  87. pub enum Column {
  88. Permissions,
  89. FileSize,
  90. Timestamp(TimeType),
  91. Blocks,
  92. User,
  93. Group,
  94. HardLinks,
  95. Inode,
  96. GitStatus,
  97. Octal,
  98. }
  99. /// Each column can pick its own **Alignment**. Usually, numbers are
  100. /// right-aligned, and text is left-aligned.
  101. #[derive(Copy, Clone)]
  102. pub enum Alignment {
  103. Left,
  104. Right,
  105. }
  106. impl Column {
  107. /// Get the alignment this column should use.
  108. pub fn alignment(self) -> Alignment {
  109. match self {
  110. Self::FileSize |
  111. Self::HardLinks |
  112. Self::Inode |
  113. Self::Blocks |
  114. Self::GitStatus => Alignment::Right,
  115. _ => Alignment::Left,
  116. }
  117. }
  118. /// Get the text that should be printed at the top, when the user elects
  119. /// to have a header row printed.
  120. pub fn header(self) -> &'static str {
  121. match self {
  122. Self::Permissions => "Permissions",
  123. Self::FileSize => "Size",
  124. Self::Timestamp(t) => t.header(),
  125. Self::Blocks => "Blocks",
  126. Self::User => "User",
  127. Self::Group => "Group",
  128. Self::HardLinks => "Links",
  129. Self::Inode => "inode",
  130. Self::GitStatus => "Git",
  131. Self::Octal => "Octal",
  132. }
  133. }
  134. }
  135. /// Formatting options for file sizes.
  136. #[derive(PartialEq, Debug, Copy, Clone)]
  137. pub enum SizeFormat {
  138. /// Format the file size using **decimal** prefixes, such as “kilo”,
  139. /// “mega”, or “giga”.
  140. DecimalBytes,
  141. /// Format the file size using **binary** prefixes, such as “kibi”,
  142. /// “mebi”, or “gibi”.
  143. BinaryBytes,
  144. /// Do no formatting and just display the size as a number of bytes.
  145. JustBytes,
  146. }
  147. impl Default for SizeFormat {
  148. fn default() -> Self {
  149. Self::DecimalBytes
  150. }
  151. }
  152. /// The types of a file’s time fields. These three fields are standard
  153. /// across most (all?) operating systems.
  154. #[derive(PartialEq, Debug, Copy, Clone)]
  155. pub enum TimeType {
  156. /// The file’s modified time (`st_mtime`).
  157. Modified,
  158. /// The file’s changed time (`st_ctime`)
  159. Changed,
  160. /// The file’s accessed time (`st_atime`).
  161. Accessed,
  162. /// The file’s creation time (`btime` or `birthtime`).
  163. Created,
  164. }
  165. impl TimeType {
  166. /// Returns the text to use for a column’s heading in the columns output.
  167. pub fn header(self) -> &'static str {
  168. match self {
  169. Self::Modified => "Date Modified",
  170. Self::Changed => "Date Changed",
  171. Self::Accessed => "Date Accessed",
  172. Self::Created => "Date Created",
  173. }
  174. }
  175. }
  176. /// Fields for which of a file’s time fields should be displayed in the
  177. /// columns output.
  178. ///
  179. /// There should always be at least one of these — there’s no way to disable
  180. /// the time columns entirely (yet).
  181. #[derive(PartialEq, Debug, Copy, Clone)]
  182. #[allow(clippy::struct_excessive_bools)]
  183. pub struct TimeTypes {
  184. pub modified: bool,
  185. pub changed: bool,
  186. pub accessed: bool,
  187. pub created: bool,
  188. }
  189. impl Default for TimeTypes {
  190. /// By default, display just the ‘modified’ time. This is the most
  191. /// common option, which is why it has this shorthand.
  192. fn default() -> Self {
  193. Self {
  194. modified: true,
  195. changed: false,
  196. accessed: false,
  197. created: false,
  198. }
  199. }
  200. }
  201. /// The **environment** struct contains any data that could change between
  202. /// running instances of exa, depending on the user’s computer’s configuration.
  203. ///
  204. /// Any environment field should be able to be mocked up for test runs.
  205. pub struct Environment {
  206. /// Localisation rules for formatting numbers.
  207. numeric: locale::Numeric,
  208. /// The computer’s current time zone. This gets used to determine how to
  209. /// offset files’ timestamps.
  210. tz: Option<TimeZone>,
  211. /// Mapping cache of user IDs to usernames.
  212. users: Mutex<UsersCache>,
  213. }
  214. impl Environment {
  215. pub fn lock_users(&self) -> MutexGuard<'_, UsersCache> {
  216. self.users.lock().unwrap()
  217. }
  218. fn load_all() -> Self {
  219. let tz = match determine_time_zone() {
  220. Ok(t) => {
  221. Some(t)
  222. }
  223. Err(ref e) => {
  224. println!("Unable to determine time zone: {}", e);
  225. None
  226. }
  227. };
  228. let numeric = locale::Numeric::load_user_locale()
  229. .unwrap_or_else(|_| locale::Numeric::english());
  230. let users = Mutex::new(UsersCache::new());
  231. Self { tz, numeric, users }
  232. }
  233. }
  234. fn determine_time_zone() -> TZResult<TimeZone> {
  235. if let Ok(file) = env::var("TZ") {
  236. TimeZone::from_file({
  237. if file.starts_with("/") {
  238. file
  239. } else {
  240. format!("/usr/share/zoneinfo/{}", {
  241. if file.starts_with(":") {
  242. file.replacen(":", "", 1)
  243. } else {
  244. file
  245. }
  246. })
  247. }
  248. })
  249. } else {
  250. TimeZone::from_file("/etc/localtime")
  251. }
  252. }
  253. lazy_static! {
  254. static ref ENVIRONMENT: Environment = Environment::load_all();
  255. }
  256. pub struct Table<'a> {
  257. columns: Vec<Column>,
  258. theme: &'a Theme,
  259. env: &'a Environment,
  260. widths: TableWidths,
  261. time_format: TimeFormat,
  262. size_format: SizeFormat,
  263. git: Option<&'a GitCache>,
  264. }
  265. #[derive(Clone)]
  266. pub struct Row {
  267. cells: Vec<TextCell>,
  268. }
  269. impl<'a, 'f> Table<'a> {
  270. pub fn new(options: &'a Options, git: Option<&'a GitCache>, theme: &'a Theme) -> Table<'a> {
  271. let columns = options.columns.collect(git.is_some());
  272. let widths = TableWidths::zero(columns.len());
  273. let env = &*ENVIRONMENT;
  274. Table {
  275. theme,
  276. widths,
  277. columns,
  278. git,
  279. env,
  280. time_format: options.time_format,
  281. size_format: options.size_format,
  282. }
  283. }
  284. pub fn widths(&self) -> &TableWidths {
  285. &self.widths
  286. }
  287. pub fn header_row(&self) -> Row {
  288. let cells = self.columns.iter()
  289. .map(|c| TextCell::paint_str(self.theme.ui.header, c.header()))
  290. .collect();
  291. Row { cells }
  292. }
  293. pub fn row_for_file(&self, file: &File<'_>, xattrs: bool) -> Row {
  294. let cells = self.columns.iter()
  295. .map(|c| self.display(file, *c, xattrs))
  296. .collect();
  297. Row { cells }
  298. }
  299. pub fn add_widths(&mut self, row: &Row) {
  300. self.widths.add_widths(row)
  301. }
  302. fn permissions_plus(&self, file: &File<'_>, xattrs: bool) -> f::PermissionsPlus {
  303. f::PermissionsPlus {
  304. file_type: file.type_char(),
  305. permissions: file.permissions(),
  306. xattrs,
  307. }
  308. }
  309. fn octal_permissions(&self, file: &File<'_>) -> f::OctalPermissions {
  310. f::OctalPermissions {
  311. permissions: file.permissions(),
  312. }
  313. }
  314. fn display(&self, file: &File<'_>, column: Column, xattrs: bool) -> TextCell {
  315. match column {
  316. Column::Permissions => {
  317. self.permissions_plus(file, xattrs).render(self.theme)
  318. }
  319. Column::FileSize => {
  320. file.size().render(self.theme, self.size_format, &self.env.numeric)
  321. }
  322. Column::HardLinks => {
  323. file.links().render(self.theme, &self.env.numeric)
  324. }
  325. Column::Inode => {
  326. file.inode().render(self.theme.ui.inode)
  327. }
  328. Column::Blocks => {
  329. file.blocks().render(self.theme)
  330. }
  331. Column::User => {
  332. file.user().render(self.theme, &*self.env.lock_users())
  333. }
  334. Column::Group => {
  335. file.group().render(self.theme, &*self.env.lock_users())
  336. }
  337. Column::GitStatus => {
  338. self.git_status(file).render(self.theme)
  339. }
  340. Column::Octal => {
  341. self.octal_permissions(file).render(self.theme.ui.octal)
  342. }
  343. Column::Timestamp(TimeType::Modified) => {
  344. file.modified_time().render(self.theme.ui.date, &self.env.tz, self.time_format)
  345. }
  346. Column::Timestamp(TimeType::Changed) => {
  347. file.changed_time().render(self.theme.ui.date, &self.env.tz, self.time_format)
  348. }
  349. Column::Timestamp(TimeType::Created) => {
  350. file.created_time().render(self.theme.ui.date, &self.env.tz, self.time_format)
  351. }
  352. Column::Timestamp(TimeType::Accessed) => {
  353. file.accessed_time().render(self.theme.ui.date, &self.env.tz, self.time_format)
  354. }
  355. }
  356. }
  357. fn git_status(&self, file: &File<'_>) -> f::Git {
  358. debug!("Getting Git status for file {:?}", file.path);
  359. self.git
  360. .map(|g| g.get(&file.path, file.is_directory()))
  361. .unwrap_or_default()
  362. }
  363. pub fn render(&self, row: Row) -> TextCell {
  364. let mut cell = TextCell::default();
  365. let iter = row.cells.into_iter()
  366. .zip(self.widths.iter())
  367. .enumerate();
  368. for (n, (this_cell, width)) in iter {
  369. let padding = width - *this_cell.width;
  370. match self.columns[n].alignment() {
  371. Alignment::Left => {
  372. cell.append(this_cell);
  373. cell.add_spaces(padding);
  374. }
  375. Alignment::Right => {
  376. cell.add_spaces(padding);
  377. cell.append(this_cell);
  378. }
  379. }
  380. cell.add_spaces(1);
  381. }
  382. cell
  383. }
  384. }
  385. pub struct TableWidths(Vec<usize>);
  386. impl Deref for TableWidths {
  387. type Target = [usize];
  388. fn deref(&self) -> &Self::Target {
  389. &self.0
  390. }
  391. }
  392. impl TableWidths {
  393. pub fn zero(count: usize) -> Self {
  394. Self(vec![0; count])
  395. }
  396. pub fn add_widths(&mut self, row: &Row) {
  397. for (old_width, cell) in self.0.iter_mut().zip(row.cells.iter()) {
  398. *old_width = max(*old_width, *cell.width);
  399. }
  400. }
  401. pub fn total(&self) -> usize {
  402. self.0.len() + self.0.iter().sum::<usize>()
  403. }
  404. }