| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- use std::iter::repeat;
- use options::{SizeFormat, TimeType};
- use ansi_term::Style;
- use unicode_width::UnicodeWidthStr;
- #[derive(PartialEq, Debug, Copy, Clone)]
- pub enum Column {
- Permissions,
- FileSize(SizeFormat),
- Timestamp(TimeType),
- Blocks,
- User,
- Group,
- HardLinks,
- Inode,
- GitStatus,
- }
- /// Each column can pick its own **Alignment**. Usually, numbers are
- /// right-aligned, and text is left-aligned.
- #[derive(Copy, Clone)]
- pub enum Alignment {
- Left, Right,
- }
- impl Column {
- /// Get the alignment this column should use.
- pub fn alignment(&self) -> Alignment {
- match *self {
- Column::FileSize(_) => Alignment::Right,
- Column::HardLinks => Alignment::Right,
- Column::Inode => Alignment::Right,
- Column::Blocks => Alignment::Right,
- Column::GitStatus => Alignment::Right,
- _ => Alignment::Left,
- }
- }
- /// Get the text that should be printed at the top, when the user elects
- /// to have a header row printed.
- pub fn header(&self) -> &'static str {
- match *self {
- Column::Permissions => "Permissions",
- Column::FileSize(_) => "Size",
- Column::Timestamp(t) => t.header(),
- Column::Blocks => "Blocks",
- Column::User => "User",
- Column::Group => "Group",
- Column::HardLinks => "Links",
- Column::Inode => "inode",
- Column::GitStatus => "Git",
- }
- }
- }
- /// Pad a string with the given number of spaces.
- fn spaces(length: usize) -> String {
- repeat(" ").take(length).collect()
- }
- impl Alignment {
- /// Pad a string with the given alignment and number of spaces.
- ///
- /// This doesn't take the width the string *should* be, rather the number
- /// of spaces to add: this is because the strings are usually full of
- /// invisible control characters, so getting the displayed width of the
- /// string is not as simple as just getting its length.
- pub fn pad_string(&self, string: &str, padding: usize) -> String {
- match *self {
- Alignment::Left => format!("{}{}", string, spaces(padding)),
- Alignment::Right => format!("{}{}", spaces(padding), string),
- }
- }
- }
- #[derive(PartialEq, Debug)]
- pub struct Cell {
- pub length: usize,
- pub text: String,
- }
- impl Cell {
- pub fn paint(style: Style, string: &str) -> Cell {
- Cell {
- text: style.paint(string).to_string(),
- length: UnicodeWidthStr::width(string),
- }
- }
- }
|