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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
135 commits
Select commit Hold shift + click to select a range
5b19719
Ls: create ls utility
jeremyvii Oct 12, 2020
a0b80b6
Ls: get a files group ownership
jeremyvii Oct 12, 2020
60db2ca
Ls: use consistent parameter name
jeremyvii Oct 12, 2020
b513919
Ls: get the file owner's name
jeremyvii Oct 12, 2020
f214d01
Ls: add comments
jeremyvii Oct 12, 2020
93f318e
Ls: attempt to count sub directories
jeremyvii Oct 12, 2020
3c55bcf
Ls: run cargo fmt
jeremyvii Oct 12, 2020
56e0d01
Ls: add time sort option
jeremyvii Oct 13, 2020
5d9adc6
Ls: add option to reverse sort order
jeremyvii Oct 13, 2020
a0499b1
Ls: create flags struct
jeremyvii Oct 13, 2020
dd5eff9
Ls: color file names
jeremyvii Oct 13, 2020
458ff53
Ls: convert all names to lowercase before sorting
jeremyvii Oct 13, 2020
25a19e0
Ls: ensure an item is a file
jeremyvii Oct 13, 2020
9c2ff1d
Ls: color default listing
jeremyvii Oct 14, 2020
3cca096
Ls: format code
jeremyvii Oct 14, 2020
97bb47e
Ls: format symlinks
jeremyvii Oct 14, 2020
f574489
Ls: move sorting to functions
jeremyvii Oct 14, 2020
ee46d4c
Ls: retrieve hard link count with metadata method
jeremyvii Oct 14, 2020
c9c0426
Ls: add option to print blocks
jeremyvii Oct 14, 2020
c1f94cd
Ls: check filename without styles applied
jeremyvii Oct 14, 2020
ed240f1
Ls: add fallback time
jeremyvii Oct 14, 2020
5546fbc
Ls: reverse date sort by default
jeremyvii Oct 14, 2020
5e448d3
Ls: reduce duplicated code
jeremyvii Oct 15, 2020
e860ec6
Ls: add -g flag
jeremyvii Oct 15, 2020
eae76a7
Ls: add -m option
jeremyvii Oct 15, 2020
55a3d46
Ls: don't show symlink source name with -m flag
jeremyvii Oct 15, 2020
22ceb15
Ls: add numeric-uid-gid flag
jeremyvii Oct 16, 2020
93b2024
Ls: run cargo fmt
jeremyvii Oct 16, 2020
f6262d5
Ls: create struct for -l rows
jeremyvii Oct 16, 2020
3bbe8f8
Ls: calculate column padding
jeremyvii Oct 17, 2020
a5d67c8
Ls: fix broken flags
jeremyvii Oct 17, 2020
d668280
Ls: add comments
jeremyvii Oct 17, 2020
6000440
Ls: get local time
jeremyvii Oct 19, 2020
2495277
Ls: add -F flag
jeremyvii Oct 19, 2020
325347a
Ls: split into separate modules
jeremyvii Oct 19, 2020
1bb0f31
Ls: capitilize help messages
jeremyvii Oct 19, 2020
17670cd
Ls: add -S flag
jeremyvii Oct 19, 2020
2998c94
Ls: update ls progress in readme
jeremyvii Oct 19, 2020
89d8043
Ls: remove unnecessary code
jeremyvii Oct 19, 2020
9a327c3
Ls: add -u flag
jeremyvii Oct 19, 2020
55a2e10
Ls: remove excess space
jeremyvii Oct 19, 2020
c169660
Ls: add -p flag
jeremyvii Oct 19, 2020
1f47cb8
Ls: try to reduce complexity
jeremyvii Oct 20, 2020
fc48f45
Ls: switch to write
jeremyvii Oct 20, 2020
bd2ed13
Ls: remove name prefix
jeremyvii Oct 20, 2020
dcc5c8a
Ls: add -i flag
jeremyvii Oct 20, 2020
df2b26e
Ls: cargo fmt
jeremyvii Oct 20, 2020
35749a9
Ls: implement -A flag
jeremyvii Oct 20, 2020
8f34012
Ls: filter out hidden files
jeremyvii Oct 20, 2020
805aee3
Ls: simplify filter
jeremyvii Oct 20, 2020
235afda
Ls: print file name when there are mutiple files
jeremyvii Oct 20, 2020
9397eec
Ls: add -L flag
jeremyvii Oct 21, 2020
f04275e
Ls: add comments
jeremyvii Oct 21, 2020
1483a3b
Ls: reduce code
jeremyvii Oct 21, 2020
c13e3b7
Ls: reduce code
jeremyvii Oct 21, 2020
3d33cc5
Merge branch 'create-ls-utility' of github.com:jeremyvii/coreutils in…
jeremyvii Oct 21, 2020
ce748f4
Ls: add comment
jeremyvii Oct 21, 2020
75dfe23
Ls: implement -a
jeremyvii Oct 22, 2020
7a8d6a0
Ls: clean up
jeremyvii Oct 22, 2020
9983de8
Ls: remove unnecessary line
jeremyvii Oct 22, 2020
46728ba
Ls: format with nightly
jeremyvii Oct 23, 2020
71162af
Ls: add long options
jeremyvii Oct 23, 2020
98d9775
Ls: implement -f flag
jeremyvii Oct 23, 2020
9532c4b
Ls: use different time format depending on duration
jeremyvii Oct 26, 2020
123017c
Ls: add utility to all platforms
jeremyvii Oct 27, 2020
e12e37e
Ls: append period to help text
jeremyvii Oct 27, 2020
c2d94d9
Ls: remove panics
jeremyvii Oct 27, 2020
03915d1
Ls: use proper naming for getters
jeremyvii Oct 27, 2020
45b83f1
Ls: return result rather than panic
jeremyvii Oct 27, 2020
e4aaa30
Ls: return result rather than panic
jeremyvii Oct 28, 2020
bc47f51
Ls: return result rather than panic
jeremyvii Oct 29, 2020
8eb26a6
Ls: add comments
jeremyvii Oct 29, 2020
3a83d7d
Ls: remove panic
jeremyvii Oct 29, 2020
ebdb64b
Ls: retrieve values once
jeremyvii Oct 30, 2020
f62e547
Ls: read individual files as well as directories
jeremyvii Oct 30, 2020
31cce2a
Ls: calculate the total
jeremyvii Oct 31, 2020
ef2aa7f
Ls: clean up
jeremyvii Nov 2, 2020
4ce77d7
Ls: format code
jeremyvii Nov 2, 2020
b7420c5
Ls: return current time rather than panic
jeremyvii Nov 2, 2020
4484cfc
Ls: remove panic
jeremyvii Nov 2, 2020
8c42c18
Ls: format code
jeremyvii Nov 2, 2020
e37f135
Ls: ignore . when sorting by name
jeremyvii Nov 2, 2020
b4283fc
Ls: add periods
jeremyvii Nov 2, 2020
867c941
Ls: remove unneeded crate
jeremyvii Nov 3, 2020
3cd23eb
Ls: switch to BufWriter
jeremyvii Nov 3, 2020
d97d9a5
Ls: implement -k flag
jeremyvii Nov 3, 2020
008e433
Ls: remove return type from main
jeremyvii Nov 3, 2020
1005f7c
Ls: run cargo fmt
jeremyvii Nov 3, 2020
89c353a
Ls: implement -c flag
jeremyvii Nov 3, 2020
86c9852
Ls: refactor column calculation
jeremyvii Nov 5, 2020
16b3eb1
Ls: remove unneeded result
jeremyvii Nov 5, 2020
4f0a9bf
Ls: get terminal dimensions
jeremyvii Nov 6, 2020
fd02d47
Ls: implement -x flag
jeremyvii Nov 6, 2020
70896bd
Ls: implement -C flag
jeremyvii Nov 6, 2020
3451df4
Ls: run cargo fmt
jeremyvii Nov 6, 2020
2b09f06
Ls: update example
jeremyvii Nov 6, 2020
2adb520
Ls: attempt to fix error in freebsd
jeremyvii Nov 6, 2020
7a8be65
Ls: attempt to fix platform specific error
jeremyvii Nov 6, 2020
84c26af
Ls: default to newlines if unable to use grid
jeremyvii Nov 6, 2020
212f635
Ls: add char device color
jeremyvii Nov 6, 2020
6e74c47
Ls: format code
jeremyvii Nov 6, 2020
7c22ad0
Ls: optionally show file color
jeremyvii Nov 6, 2020
929a632
Ls: switch to type alias
jeremyvii Nov 9, 2020
8fb46dd
Ls: implement -d flag
jeremyvii Nov 9, 2020
69f71e6
Ls: implement -o flag
jeremyvii Nov 10, 2020
077f1a8
Ls: only colorize file names if output is a tty
jeremyvii Nov 10, 2020
f640f2d
Ls: use bstr to deal with non utf-8 chars
jeremyvii Nov 12, 2020
470df1f
Ls: run cargo fmt
jeremyvii Nov 12, 2020
8f44561
Ls: use bstring rather than string
jeremyvii Nov 16, 2020
7419ae4
Ls: format code
jeremyvii Nov 16, 2020
5f6e311
Ls: clean up
jeremyvii Nov 16, 2020
00ca739
Ls: write out bytes if stdout isn't a tty
jeremyvii Nov 16, 2020
38a6fb2
Ls: refactor utility output
jeremyvii Nov 17, 2020
58fc379
Ls: implement -1 flag
jeremyvii Nov 18, 2020
f7e2f9c
Ls: clean up
jeremyvii Nov 19, 2020
6b9136e
Ls: fix comment
jeremyvii Nov 19, 2020
661e807
Ls: implement -H flag
jeremyvii Nov 19, 2020
1afe7b5
Ls: remove extra whitespace
jeremyvii Nov 19, 2020
e90fc04
Ls: display std crates at the top
jeremyvii Nov 19, 2020
f193776
Ls: fix comment
jeremyvii Nov 19, 2020
869f22f
Ls: remove unneeded with values
jeremyvii Nov 19, 2020
37b74be
Ls: clean up
jeremyvii Nov 19, 2020
1bf0901
Ls: implement the -q flag
jeremyvii Nov 20, 2020
1b452c7
Ls: align output when using the -d flag
jeremyvii Nov 20, 2020
8a02a57
Ls: remove unwrap usage
jeremyvii Nov 23, 2020
5a7377a
Ls: begin -R flag
jeremyvii Nov 30, 2020
03f3ab5
Ls: display directories recursively
jeremyvii Dec 17, 2020
b4aaf67
Merge github.com:GrayJack/coreutils into create-ls-utility
jeremyvii Dec 26, 2020
33bc2be
Ls: return io error
jeremyvii Dec 26, 2020
6374060
Ls: add comment
jeremyvii Dec 26, 2020
9daf166
Ls: create new bufwriter
jeremyvii Dec 26, 2020
5cde34e
Ls: add function comments
jeremyvii Dec 26, 2020
fe448b5
Revert "Ls: create new bufwriter"
jeremyvii Dec 28, 2020
e3c35c3
Ls: only exit with code if the code isn't zero
jeremyvii Dec 28, 2020
9ffb259
Ls: check if bufwriter is a tty
jeremyvii Dec 28, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ members = [
"id",
"link",
"logname",
"ls",
GrayJack marked this conversation as resolved.
Show resolved Hide resolved
"mkdir",
"mkfifo",
"mktemp",
Expand Down
1 change: 1 addition & 0 deletions DragonflyBSD.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"groups",
"head",
"id",
'ls',
"link",
"logname",
"mkdir",
Expand Down
1 change: 1 addition & 0 deletions FreeBSD.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"groups",
"head",
"id",
'ls',
"link",
"logname",
"mkdir",
Expand Down
1 change: 1 addition & 0 deletions Fuchsia.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"groups",
"head",
"id",
'ls',
"link",
"logname",
"mkdir",
Expand Down
1 change: 1 addition & 0 deletions Haiku.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"groups",
"head",
"id",
'ls',
"link",
"logname",
"mkdir",
Expand Down
1 change: 1 addition & 0 deletions Linux.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ members = [
"head",
"id",
"link",
'ls',
"logname",
"mkdir",
"mkfifo",
Expand Down
1 change: 1 addition & 0 deletions MacOS.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"groups",
"head",
"id",
'ls',
"link",
"logname",
"mkdir",
Expand Down
1 change: 1 addition & 0 deletions NetBSD.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"groups",
"head",
"id",
'ls',
"link",
"logname",
"mkdir",
Expand Down
1 change: 1 addition & 0 deletions OpenBSD.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"groups",
"head",
"id",
'ls',
"link",
"logname",
"mkdir",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ cargo install --path .
| link | | | X |
| ln | X | | |
| logname | | | X |
| ls | X | | |
| ls | | X | |
| mkdir | | | X |
| mktemp | | | X |
| mkfifo | X | | |
Expand Down
1 change: 1 addition & 0 deletions Solaris.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"groups",
"head",
"id",
'ls',
"link",
"logname",
"mkdir",
Expand Down
1 change: 1 addition & 0 deletions Unix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"groups",
"head",
"id",
'ls',
"link",
"logname",
"mkdir",
Expand Down
29 changes: 28 additions & 1 deletion coreutils_core/src/os/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
os::unix::io::AsRawFd,
};

use libc::ttyname;
use libc::{ioctl, ttyname, winsize, TIOCGWINSZ};

// use crate::file_descriptor::FileDescriptor;

Expand Down Expand Up @@ -99,3 +99,30 @@ impl<T: AsRawFd> IsTTY for T {
pub fn is_tty(file_descriptor: &impl AsRawFd) -> bool {
unsafe { libc::isatty(file_descriptor.as_raw_fd()) == 1 }
}

/// Gets the width and height of a TTY.
///
/// ## Example
/// ``` rust
/// use coreutils_core::os::tty::tty_dimensions;
/// let dimensions = tty_dimensions(&std::io::stdout());
/// ```
#[inline]
pub fn tty_dimensions(file_descriptor: &impl AsRawFd) -> Option<(u16, u16)> {
if !is_tty(file_descriptor) {
return None;
}

let mut size = winsize { ws_row: 0, ws_col: 0, ws_xpixel: 0, ws_ypixel: 0 };

let tiocgwinsz = TIOCGWINSZ;

#[cfg(target_os = "freebsd")]
let tiocgwinsz: u64 = tiocgwinsz.into();

if unsafe { ioctl(file_descriptor.as_raw_fd(), tiocgwinsz, &mut size) } == -1 {
return None;
}

Some((size.ws_col, size.ws_row))
}
21 changes: 21 additions & 0 deletions ls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "ls"
version = "0.1.0"
authors = ["Jeremy Jackson <git@jeremyvii.com>"]
license = "MPL-2.0-no-copyleft-exception"
build = "build.rs"
edition = "2018"
description = "List information about the FILEs (the current directory by default)."

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "^2.33.0", features = ["wrap_help"] }
coreutils_core = { path = "../coreutils_core" }
unix_mode = "0.1.1"
chrono = "0.4"
ansi_term = "0.12.1"
term_grid = "0.2.0"

[build-dependencies]
clap = "^2.33.0"
24 changes: 24 additions & 0 deletions ls/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::env;

use clap::Shell;

#[path = "src/cli.rs"]
mod cli;

fn main() {
let mut app = cli::create_app();

let out_dir = match env::var("OUT_DIR") {
Ok(dir) => dir,
Err(err) => {
eprintln!("No OUT_DIR: {}", err);
return;
},
};

app.gen_completions("ls", Shell::Zsh, out_dir.clone());
app.gen_completions("ls", Shell::Fish, out_dir.clone());
app.gen_completions("ls", Shell::Bash, out_dir.clone());
app.gen_completions("ls", Shell::PowerShell, out_dir.clone());
app.gen_completions("ls", Shell::Elvish, out_dir);
}
197 changes: 197 additions & 0 deletions ls/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
use clap::{
crate_authors, crate_description, crate_name, crate_version, App, AppSettings::ColoredHelp, Arg,
};

pub(crate) fn create_app<'a, 'b>() -> App<'a, 'b> {
App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.help_message("Display help information.")
.version_message("Display version information.")
.help_short("?")
.settings(&[ColoredHelp])
.arg(
Arg::with_name("FILE")
.help("File(s) to list")
.required(true)
.multiple(true)
.default_value("."),
)
.arg(
Arg::with_name("all")
.help(
"Write out all directory entries, including those whose names begin with a \
<period> ( '.' ).",
)
.short("a")
.long("all"),
)
.arg(
Arg::with_name("almost_all")
.help(
"Write out all directory entries, including those whose names begin with a \
<period> ( '.' ) but excluding the entries dot and dot-dot (if they exist).",
)
.short("A")
.long("almost-all"),
)
.arg(
Arg::with_name("file_status_modification")
.help(
"Use time of last modification of the file status information instead of last \
modification of the file itself for sorting -t or writing -l.",
)
.short("c")
.long("file-status-modification"),
)
.arg(
Arg::with_name("order_top_to_bottom")
.help("Write multi-text-column output with entries sorted down the columns.")
.short("C")
.long("order-top-to-bottom"),
)
.arg(
Arg::with_name("directory")
.help("List directories and files themselves, rather than their contents.")
.short("d")
.long("directory"),
)
.arg(
Arg::with_name("no_sort")
.help(
"Output is not sorted. This option turns on -a. It also negates the effect of \
the -r, -S and -t options.",
)
.short("f")
.long("no-sort"),
)
.arg(
Arg::with_name("classify")
.help("Append indicator (one of */=>@|) to entries.")
.short("F")
.long("classify"),
)
.arg(
Arg::with_name("no_dereference")
.help("Follow symbolic links listed on the command line.")
.short("H")
.long("no-dereference"),
)
.arg(
Arg::with_name("block_size")
.help(
"Set the block size for the -s option and the per-directory block count \
written for the -l, -n, -s, -g, and -o options to 1024 bytes.",
)
.short("k")
.long("block-size"),
)
.arg(
Arg::with_name("comma_separate")
.help("Fill width with a comma separated list of entries.")
.short("m")
.long("comma-separate"),
)
.arg(
Arg::with_name("dereference")
.help(
"When showing file information for a symbolic link, show information for the \
file the link references rather than for the link itself.",
)
.short("L")
.long("dereference"),
)
.arg(
Arg::with_name("indicator")
.help("Write a <slash> ( '/' ) after each filename if that file is a directory.")
.short("p")
.long("indicator"),
)
.arg(
Arg::with_name("inode")
.help("For each file, write the file's file serial number.")
.short("i")
.long("inode"),
)
.arg(
Arg::with_name("last_accessed")
.help(
"Use time of last access instead of last modification of the file for sorting \
-t or writing -l.",
)
.short("u")
.long("last-accessed"),
)
.arg(Arg::with_name("list").help("Use a long listing format").short("l").long("list"))
.arg(
Arg::with_name("no_owner")
.help("Like -l, but do not list owner.")
.short("g")
.long("no-owner"),
)
.arg(
Arg::with_name("numeric_uid_gid")
.help("Like -l, but list numeric user and group IDs.")
.short("n")
.long("numeric-uid-gid"),
)
.arg(
Arg::with_name("no_group")
.help("Like -l, but do not list group.")
.short("o")
.long("no-group"),
)
.arg(
Arg::with_name("hide_control_chars")
.help(
"Force each instance of non-printable filename characters to be written as \
the � character. This is the default behavior if the output is to a terminal \
device.",
)
.short("q")
.long("hide-control-chars"),
)
.arg(
Arg::with_name("reverse")
.help("Reverse order while sorting.")
.short("r")
.long("reverse"),
)
.arg(
Arg::with_name("recursive")
.help("Recursively print subdirectories.")
.short("R")
.long("recursive"),
)
.arg(
Arg::with_name("size")
.help("Print the allocated size of each file, in blocks.")
.short("s")
.long("size"),
)
.arg(
Arg::with_name("sort_size")
.help("Sort by first file size, largest first.")
.short("S")
.long("sort-size"),
)
.arg(
Arg::with_name("time")
.help("Sort by modification time, newest first.")
.short("t")
.long("time"),
)
.arg(
Arg::with_name("order_left_to_right")
.help("Sort columns left to right.")
.short("x")
.long("order-left-to-right"),
)
.arg(
Arg::with_name("one_per_line")
.help("Force output to be one entry per line.")
.short("1")
.long("one-per-line"),
)
}
Loading