define path expansion

parse-tree
Jordan Orelli 2 years ago
parent cd494d0f76
commit 3f6eaa8e61

92
Cargo.lock generated

@ -8,12 +8,55 @@ version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "dirs"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.140"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
[[package]]
name = "log"
version = "0.4.17"
@ -45,6 +88,26 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags",
]
[[package]]
name = "redox_users"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [
"getrandom",
"redox_syscall",
"thiserror",
]
[[package]]
name = "syn"
version = "1.0.107"
@ -87,12 +150,41 @@ name = "wash"
version = "0.1.0"
dependencies = [
"anyhow",
"dirs",
"log",
"macros",
"thiserror",
"windows",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.44.0"

@ -4,15 +4,11 @@ version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
# [workspace]
# members = [
# "macros"
# ]
[dependencies]
anyhow = "1.0"
thiserror = "1.0"
macros = { path = "macros" }
dirs = "4"
log = "0.4"

@ -16,7 +16,9 @@ use crate::log::*;
use shell::Shell;
fn main() -> Result<()> {
match Log::file("C:\\Users\\jordan\\wash.log") {
let mut shell = Shell::new()?;
let log_path = shell.expand_path("~/wash.log");
match Log::file(log_path) {
Ok(f) => {
let target = Box::leak(Box::new(f));
_ = set_logger(target).map(|()| set_max_level(LevelFilter::Debug));
@ -27,7 +29,6 @@ fn main() -> Result<()> {
}
let prompt = Prompt::new();
let mut shell = Shell::new()?;
prompt.print(&mut shell.output)?;
info!("» enter");

@ -1,11 +1,13 @@
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
use crate::output;
use crate::input;
use crate::line::Line;
use crate::log::*;
use crate::{input, line::Line, log::*, output};
use std::{
fs::File,
io::{Read, Seek, SeekFrom, Write},
path::{Path, PathBuf},
};
use anyhow::Result;
use dirs;
pub struct Shell {
pub input: input::Reader,
@ -64,6 +66,33 @@ impl Shell {
Ok(())
}
pub fn expand_path<P: AsRef<Path>>(&self, p: P) -> PathBuf {
let p = p.as_ref();
match p.to_str() {
Some(s) => {
if s.len() == 0 || !s.starts_with('~') {
return p.to_path_buf();
}
}
None => {
return p.to_path_buf();
}
}
let mut parts = p.components();
let first = parts.next().unwrap().as_os_str().to_str().unwrap();
if first == "~" {
dirs::home_dir().unwrap().join(parts)
} else {
let my_home = dirs::home_dir().unwrap();
let home_root = my_home.parent().unwrap();
let first = &first[1..];
let other_home = home_root.join(first);
other_home.join(parts)
}
}
pub fn eval(&mut self, cmd: String, args: Vec<&str>) -> Result<bool> {
match cmd.as_str() {
"pwd" => {
@ -175,5 +204,3 @@ impl Shell {
}
}
}
// pub fn expand() { }

Loading…
Cancel
Save