Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ls #130

Merged
merged 135 commits into from
Dec 28, 2020
Merged

Implement ls #130

merged 135 commits into from
Dec 28, 2020

Conversation

jeremyvii
Copy link
Contributor

@jeremyvii jeremyvii commented Oct 14, 2020

This PR implements the ls utility. Below lists the arguments with the short and long flags.

  • -A|--almost-all
  • -C|--order-top-to-bottom
  • -F|--classify
  • -H|--no-dereference
  • -L|--dereference
  • -R|--recursive
  • -S|--sort-size
  • -a|--all
  • -c|--file-status-modification
  • -d|--directory
  • -f|--no-sort
  • -g|--no-owner
  • -i|--inode
  • -k|--block-size
  • -l|--list
  • -m|--comma-separate
  • -n|--numeric-uid-gid
  • -o|--no-group
  • -p|--indicator
  • -q|--hide-control-chars
  • -r|--reverse
  • -s|--size
  • -t|--time
  • -u|--last-accessed
  • -x|--order-left-to-write
  • -1|--one-per-line

Copy link
Owner

@GrayJack GrayJack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you intend to finish the last missing item in the check list? No hurries! The work here is amazing as is, and it's perfectly fine to merge without a complete implementation. 😃

Also I would like to keep track of the open PRs, because sometimes the author forgets to finish the draft, rendering us unable to merge the changes if finished! (I did this once 😆)

In this review I just added 2 comments, one is a nitpick, another one is that I did a breaking change in the coreutils_core API

ls/src/file.rs Outdated

use ansi_term::Color;

extern crate chrono;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need extern crate on 2018 edition

ls/src/file.rs Outdated
Comment on lines 98 to 123
/// Retrieves the file's user name as a string. If the `-n` flag is set,
/// the the user's ID is returned
pub fn user(&self) -> Result<BString, PasswdError> {
if self.flags.numeric_uid_gid {
return Ok(BString::from(self.metadata.uid().to_string()));
}

match Passwd::from_uid(self.metadata.uid()) {
Ok(passwd) => Ok(passwd.name().to_owned()),
Err(err) => Err(err),
}
}

/// Retrieves the file's group name as a string. If the `-n` flag is set,
/// the the group's ID is returned
pub fn group(&self) -> Result<BString, GroupError> {
if self.flags.numeric_uid_gid {
return Ok(BString::from(self.metadata.gid().to_string()));
}

match Group::from_gid(self.metadata.gid()) {
Ok(group) => Ok(group.name().to_owned()),
Err(err) => Err(err),
}
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a recent commit I changed the Group and Passwd to return io::Result, because all os-module-like public API seems to return io::Error on problems, and most of the rest of the Core OS API was already returning a io::Error, so it made sense to change for better consistency with the Core API and with the Rust ecosystem.

Can you change that? I also can change that before merging, I have no problems with that

@jeremyvii
Copy link
Contributor Author

jeremyvii commented Dec 26, 2020

Do you intend to finish the last missing item in the check list? No hurries! The work here is amazing as is, and it's perfectly fine to merge without a complete implementation.

Also I would like to keep track of the open PRs, because sometimes the author forgets to finish the draft, rendering us unable to merge the changes if finished! (I did this once )

In this review I just added 2 comments, one is a nitpick, another one is that I did a breaking change in the coreutils_core API

My apologies! I have been sitting on 03f3ab5 since December 17th (forgot to push this commit). Work and the holidays had me distracted. I will take care of your reviews, see if there is any clean up I can do, and move this PR out of a draft state.

@GrayJack
Copy link
Owner

No need to apologize! We need to rest a little bit in the Holidays, I wasn't expecting a reply this month because of the Holidays LOL

Take your time, no need to hurry!

@jeremyvii
Copy link
Contributor Author

I am unsure when this began exactly, but I noticed today that my BufWriter was not longer outputting anything when passing it into the write! macro. I messed with it for quiet a while but couldn't determine why. I got around this by creating a new BufWriter in 9daf166 in the output function but I am not sure that this is a good solution. Do you have any thoughts or suggestions?

I would also like to note that the term_grid crate works excellently for the displaying the files in a grid. However, there is a bug in the terminal size calculation that causes the grid to display in one column even when there is enough room in the terminal (see ogham/rust-term-grid#11).

@jeremyvii jeremyvii marked this pull request as ready for review December 26, 2020 17:31
@GrayJack
Copy link
Owner

I am unsure when this began exactly, but I noticed today that my BufWriter was not longer outputting anything when passing it into the write! macro. I messed with it for quiet a while but couldn't determine why. I got around this by creating a new BufWriter in 9daf166 in the output function but I am not sure that this is a good solution. Do you have any thoughts or suggestions?

I think the reason that is happening is because the BufWriter is failing to flush when it gets out of the scope (end of main function), because you're process::exiting it. My guess is that if you call writer.flush() before the process::exit it will return to output.

If I'm correct, I think a more clean solution is to only process::exit if exit_code is different than zero. That way, when it's zero (no errors happened ) the main function exits normally and drops the BufWriter, making it flush it's contents to the underlining writer.

ls/src/output.rs Outdated

/// Writes the provided files in the default format.
pub(crate) fn default<W: Write>(files: Files, writer: &mut W, flags: Flags) -> io::Result<()> {
if !is_tty(&io::stdout()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we checking for the right thing here? In my mind I was thinking that here we wanted to check if the underlying W in BufWriter<W> is not a TTY, not specifically the stdout.

Let me know what you think, I may have misunderstood this part of the code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be misunderstanding, but I believe it would be the same either way since the BufWriter instance is loaded with the stdout file descriptor.I went ahead and made this change though. I had to refactor to writer parameters to use BufWriter<Stdout> as it's type in order to get the file descriptor reference. Although I cannot remember why I was using the parameterized type for the writer to begin with.

@GrayJack
Copy link
Owner

LGTM! bors r+

@GrayJack
Copy link
Owner

bors r+

@bors bors bot merged commit 8df4130 into GrayJack:dev Dec 28, 2020
@jeremyvii jeremyvii deleted the create-ls-utility branch December 28, 2020 20:38
@GrayJack GrayJack mentioned this pull request Dec 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants