refactor and stuff

main
_ 2025-03-21 00:46:08 -05:00
parent 220869b003
commit f4ce34e757
3 changed files with 58 additions and 162 deletions

145
Cargo.lock generated
View File

@ -2,62 +2,11 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "addr2line"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1"
dependencies = [
"gimli",
]
[[package]]
name = "adler2"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.97" version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f" checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f"
dependencies = [
"backtrace",
]
[[package]]
name = "backtrace"
version = "0.3.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a"
dependencies = [
"addr2line",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
"windows-targets",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "gimli"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
[[package]]
name = "libc"
version = "0.2.171"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6"
[[package]] [[package]]
name = "look_at_that" name = "look_at_that"
@ -65,97 +14,3 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
] ]
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "miniz_oxide"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e3e04debbb59698c15bacbb6d93584a8c0ca9cc3213cb423d31f760d8843ce5"
dependencies = [
"adler2",
]
[[package]]
name = "object"
version = "0.36.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87"
dependencies = [
"memchr",
]
[[package]]
name = "rustc-demangle"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

View File

@ -12,4 +12,9 @@ name = "lk"
path = "src/main.rs" path = "src/main.rs"
[dependencies] [dependencies]
anyhow = { version = "1.0.97", features = ["backtrace"] } anyhow = { version = "1.0.97" }
[profile.release]
codegen-units = 1
lto = true
opt-level = "z"

View File

@ -1,29 +1,65 @@
use anyhow::{Context as _, Result, bail}; use anyhow::{Context as _, Result, bail};
fn main() -> Result<()> { fn main() -> Result<()> {
use std::fs; use std::io::IsTerminal as _;
let is_terminal = std::io::stdout().is_terminal();
let mut args = std::env::args(); let mut args = std::env::args();
let _exe_name = args.next().context("How do we not have an exe name?")?; let _exe_name = args.next().context("How do we not have an exe name?")?;
let mut filenames = vec![]; let mut filenames = vec![];
for arg in args { for arg in args {
filenames.push(arg); filenames.push(arg);
} }
for filename in filenames {
let metadata = fs::metadata(&filename)?; let app = App {
if metadata.is_dir() { filenames,
shell_out("/usr/bin/ls", &filename)?; is_terminal,
} else { };
shell_out("/usr/bin/cat", &filename)?; app.run()
}
}
Ok(())
} }
fn shell_out(cmd: &str, arg: &str) -> Result<()> { struct App {
let status = std::process::Command::new(cmd).arg(arg).status()?; filenames: Vec<String>,
if !status.success() { is_terminal: bool,
bail!("subprocess {cmd} failed"); }
}
Ok(()) impl App {
fn run(&self) -> Result<()> {
if self.filenames.is_empty() {
return self.ls(".");
}
for filename in &self.filenames {
let metadata = std::fs::metadata(&filename)?;
if metadata.is_dir() {
self.ls(filename)?;
} else {
self.cat(filename)?;
}
}
Ok(())
}
fn cat(&self, filename: &str) -> Result<()> {
// Use `/usr/bin` just in case for some weird reason we're symlinked as cat,
// we don't want to recurse into ourselves
let status = std::process::Command::new("/usr/bin/cat").arg(filename).status()?;
if !status.success() {
bail!("subprocess `cat` failed");
}
Ok(())
}
fn ls(&self, filename: &str) -> Result<()> {
// Use `/usr/bin` just in case for some weird reason we're symlinked as ls,
// we don't want to recurse into ourselves
let mut command = std::process::Command::new("/usr/bin/ls");
if self.is_terminal {
command.arg("--color=always");
}
let status = command.arg(filename).status()?;
if !status.success() {
bail!("subprocess `ls` failed");
}
Ok(())
}
} }