Skip to content

Displaying file type and git status in ls #7612

Closed

Description

@KodiCraft

Related problem

Modern alternatives to ls such as lsd or exa usually display additional helpful contextual information allowing the user to understand at a glace what each file is and its status. This includes an icon that illustrates the file's mime type (for instance, an icon of a picture for image files) and/or a letter or symbol representing the file's git status (for instance, "M" for a file that has been modified since the last commit). Providing this information in the default ls command would make it generally more versatile for users and provide much more flexibility in scripting. For instance, a user could make a script iterating over untracked files of a git repo, or a script that only executes on image/* files.

Describe the solution you'd like

Allowing the type column in ls to display the mime type of a file, with an optional flag to return to the "legacy" option of simply displaying "file".

Adding a git status column displayed when the directory is in a git repository indicating the file's git status.

Describe alternatives you've considered

No response

Additional context and details

I am already working on a simple fork implementing these features on Linux as it's a feature I wanted on my own personal version, though I believe it could be interesting for it to be implemented upstream.

Activity

  1. fdncred commented on Dec 27, 2022

    @fdncred
    Contributor

    Would you mind including some screenshots? I think this sounds cool and would like to see what it looks like. Thanks!

    We do have icons like this, that were borrowed from exa

    // Attribution: Thanks exa. Most of this file is taken from around here

    Screenshot 2022-12-27 at 7 13 22 AM

  2. webbedspace commented on Dec 27, 2022

    @webbedspace
    Contributor

    So the thing about Nushell ls is that, because of how it's meant to be filtered through select name and such instead of just displayed, the default setting is designed to perform the minimum of filesystem I/O. This is why -d and --all aren't the default. So, things like this type column would better belong behind a flag.

    By the way, is there even an easy way to compute the MIME type of arbitrary files? I know that the file POSIX command uses a database called /usr/share/file/magic, but that just outputs an arbitrary and non-uniform description.

  3. fdncred commented on Dec 27, 2022

    @fdncred
    Contributor

    For mime-type, I'm guessing we'd just infer the type by the extension. Anything else would probably be to impactful. Maybe something like this https://github.com/Lynnesbian/new_mime_guess ?

  4. KodiCraft commented on Dec 27, 2022

    @KodiCraft
    Author

    new_mime_guess is what I was going for too, it works well and doesn't seem too demanding. Plus, similarly to how exa does it, information like git status that could take longer to identify should be disabled by default.

  5. fdncred commented on Dec 27, 2022

    @fdncred
    Contributor

    I don't see the benefit of mime-type. Why do we need that? What's a use-case for it?

  6. KodiCraft commented on Dec 27, 2022

    @KodiCraft
    Author

    It could be useful for tasks applied to an entire type of files without having to parse the file extension manually. For instance, a user could display all images in a directory, regardless of if they are a png, jpg, or anything else, by just doing ls -t | where type =~ image (with -t being the flag to show mime type). It would in general help the user automate tasks intuitively.

    For a personal case, I have had some of my scripts break when dealing with rather recent image formats like avif and webp just because of the fact I manually handled extensions. By supporting mime-types, we are providing a helpful option for flexibility and categorization.

  7. theAkito commented on Dec 27, 2022

    @theAkito

    Basically, more or less roughly, what I have been suggesting here.

    #7524

  8. KodiCraft commented on Dec 27, 2022

    @KodiCraft
    Author

    I actually do like the idea of making this more fundamental to nushell rather than an ls specific thing, though that sounds like quite the undertaking.

  9. fdncred commented on Dec 27, 2022

    @fdncred
    Contributor

    Thoughts?
    Screenshot 2022-12-27 at 11 43 20 AM
    I'm basically saying, "if it's a file, show me the mime type, if -m is used".

    pub fn get_file_type(md: &std::fs::Metadata, display_name: &str, use_mime_type: bool) -> String {
        let ft = md.file_type();
        let mut file_type = "unknown";
        if ft.is_dir() {
            file_type = "dir";
        } else if ft.is_file() {
            file_type = "file";
        } else if ft.is_symlink() {
            file_type = "symlink";
        } else {
            #[cfg(unix)]
            {
                if ft.is_block_device() {
                    file_type = "block device";
                } else if ft.is_char_device() {
                    file_type = "char device";
                } else if ft.is_fifo() {
                    file_type = "pipe";
                } else if ft.is_socket() {
                    file_type = "socket";
                }
            }
        }
        if use_mime_type {
            let guess = mime_guess::from_path(display_name);
            let mime_guess = match guess.first() {
                Some(mime_type) => mime_type.essence_str().to_string(),
                None => "unknown".to_string(),
            };
            if file_type == "file" {
                mime_guess
            } else {
                file_type.to_string()
            }
        } else {
            file_type.to_string()
        }
    }

    Screenshot 2022-12-27 at 11 45 53 AM

  10. KodiCraft commented on Dec 27, 2022

    @KodiCraft
    Author

    That's pretty much exactly how I prototyped it as well in my fork, this also seems to work on Windows from my testing, though I haven't had the time to write proper tests yet.

  11. webbedspace commented on Dec 27, 2022

    @webbedspace
    Contributor

    How about instead of putting it in the "type" column, a new "mime" column is added to ls when -m is used? I think having one column suddenly change meaning off of a flag is a bit unseemly.

  12. KodiCraft commented on Dec 27, 2022

    @KodiCraft
    Author

    To be fair, that already somewhat happens with -d, the flag makes the 'type' column more specific in the same way -d does the 'size' column.

  13. theAkito commented on Dec 27, 2022

    @theAkito

    I would strongly suggest, as already hinted in the discussion I initiated, that this example is nice, but "type" should be "mime" in this case.

    The reason for that is, that "type" is extremely generic. It could be anything. For example, it could be some internal Nu-specific type or something. When it's called "mime", there is no debate. It's short, quick & correct, without ambiguity.

  14. fdncred commented on Dec 27, 2022

    @fdncred
    Contributor

    I stated my case in #7616 on type vs mime.

  15. theAkito commented on Dec 27, 2022

    @theAkito

    I stated my case in #7616 on type vs mime.

    I have read your reasoning & deem it valid. I think, both of us have a point & I can see either option making sense.

    I personally don't like mixing up stuff like column definitions, which is kind of anti-functional, but on the other hand it might get confusing to add more & more different column names to remember over time.

    Both ways are reasonable.

  16. KodiCraft commented on Dec 27, 2022

    @KodiCraft
    Author

    PR implementing the git status feature: #7620

  17. sholderbach commented on Dec 28, 2022

    @sholderbach
    Member

    I think based on the fact that #7616 landed with mime-types (in scope as it is a common system interface) and the discussion on #7620 concluded that tight coupling with git is outside of the scope of the core nushell project we should close this issue and encourage plugins or scripts contributing further git integrations through https://github.com/nushell/nu_scripts or https://github.com/nushell/awesome-nu

    Additional argument for future "standard library/core" consideration. git although a de facto standard by virtue of being predominantly used, is just one of several implementations of a version control system. hg / svn etc. still exist. Introducing it to the core would thus be a well intentioned opinionated decision but not foster a standard or increase interoperability.
    Things that make nushell use work better across Windows and Unixes or implement the core of a common standard on the other hand reinforce efforts to make things standardized and seamlessly cross-platform.

  18. KodiCraft commented on Dec 28, 2022

    @KodiCraft
    Author

    Closed, see above comment

  19. fdncred commented on Dec 28, 2022

    @fdncred
    Contributor

    kind of unrelated but I wonder if it's possible for someone to write a plugin that replaces and internal command. for instance, i wonder if someone wrapped the exa or lsd code in a nushell plugin and named it ls, would that work? i have no idea but it's interesting.

  20. webbedspace commented on Dec 28, 2022

    @webbedspace
    Contributor

    You can already write aliases over internals, can't you? alias ls = ls -a works (though I wouldn't use it).

  21. fdncred commented on Dec 28, 2022

    @fdncred
    Contributor

    Yes, you can do that wish aliases and custom commands. I assume the same would work with plugins but I'm not sure as I've never done it.

  22. added this to the v0.74 milestone on Dec 29, 2022
  23. hkalbasi commented on Aug 15, 2026

    @hkalbasi

    I wanted to use ls -m instead of file to improve speed of my nushell script. Why it doesn't read the file content?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    category:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions