Make DeviceIDs its own type

This is so we can define a render method on it.
This commit is contained in:
Benjamin Sago 2017-05-20 21:55:18 +01:00
parent 24a5d71f4b
commit ddd34f3b1f
3 changed files with 28 additions and 23 deletions

View File

@ -135,14 +135,17 @@ pub enum Size {
/// ///
/// This is what ls does as well. Without it, the devices will just have /// This is what ls does as well. Without it, the devices will just have
/// file sizes of zero. /// file sizes of zero.
/// DeviceIDs(DeviceIDs),
/// You can see what these device numbers mean: }
/// - http://www.lanana.org/docs/device-list/
/// - http://www.lanana.org/docs/device-list/devices-2.6+.txt /// The major and minor device IDs that gets displayed for device files.
DeviceIDs { ///
major: u8, /// You can see what these device numbers mean:
minor: u8, /// - http://www.lanana.org/docs/device-list/
} /// - http://www.lanana.org/docs/device-list/devices-2.6+.txt
pub struct DeviceIDs {
pub major: u8,
pub minor: u8,
} }

View File

@ -273,10 +273,10 @@ impl<'dir> File<'dir> {
} }
else if self.is_char_device() || self.is_block_device() { else if self.is_char_device() || self.is_block_device() {
let dev = self.metadata.rdev(); let dev = self.metadata.rdev();
f::Size::DeviceIDs { f::Size::DeviceIDs(f::DeviceIDs {
major: (dev / 256) as u8, major: (dev / 256) as u8,
minor: (dev % 256) as u8, minor: (dev % 256) as u8,
} })
} }
else { else {
f::Size::Some(self.metadata.len()) f::Size::Some(self.metadata.len())

View File

@ -13,7 +13,7 @@ impl f::Size {
let size = match *self { let size = match *self {
f::Size::Some(s) => s, f::Size::Some(s) => s,
f::Size::None => return TextCell::blank(colours.punctuation), f::Size::None => return TextCell::blank(colours.punctuation),
f::Size::DeviceIDs { major, minor } => return render_device_ids(colours, major, minor), f::Size::DeviceIDs(ref ids) => return ids.render(colours),
}; };
let result = match size_format { let result = match size_format {
@ -48,9 +48,10 @@ impl f::Size {
} }
} }
fn render_device_ids(colours: &Colours, major: u8, minor: u8) -> TextCell { impl f::DeviceIDs {
let major = major.to_string(); fn render(&self, colours: &Colours) -> TextCell {
let minor = minor.to_string(); let major = self.major.to_string();
let minor = self.minor.to_string();
TextCell { TextCell {
width: DisplayWidth::from(major.len() + 1 + minor.len()), width: DisplayWidth::from(major.len() + 1 + minor.len()),
@ -60,4 +61,5 @@ fn render_device_ids(colours: &Colours, major: u8, minor: u8) -> TextCell {
colours.size.minor.paint(minor), colours.size.minor.paint(minor),
].into(), ].into(),
} }
}
} }