2020-01-16 10:05:53 +01:00
|
|
|
pub mod commands;
|
2019-12-15 17:15:06 +01:00
|
|
|
pub mod fs;
|
|
|
|
pub mod macros;
|
|
|
|
pub mod playground;
|
|
|
|
|
2021-02-19 02:24:27 +01:00
|
|
|
pub struct Outcome {
|
|
|
|
pub out: String,
|
|
|
|
pub err: String,
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:03:28 +02:00
|
|
|
#[cfg(windows)]
|
|
|
|
pub const NATIVE_PATH_ENV_VAR: &str = "Path";
|
|
|
|
#[cfg(not(windows))]
|
2021-05-13 05:03:49 +02:00
|
|
|
pub const NATIVE_PATH_ENV_VAR: &str = "PATH";
|
|
|
|
|
2021-06-25 05:58:37 +02:00
|
|
|
#[cfg(windows)]
|
2021-07-23 16:03:28 +02:00
|
|
|
pub const NATIVE_PATH_ENV_SEPARATOR: char = ';';
|
2021-07-24 15:42:50 +02:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
pub const NATIVE_PATH_ENV_SEPARATOR: char = ':';
|
2021-06-25 05:58:37 +02:00
|
|
|
|
2021-02-19 02:24:27 +01:00
|
|
|
impl Outcome {
|
|
|
|
pub fn new(out: String, err: String) -> Outcome {
|
|
|
|
Outcome { out, err }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 17:15:06 +01:00
|
|
|
pub fn pipeline(commands: &str) -> String {
|
|
|
|
commands
|
2022-02-17 23:23:04 +01:00
|
|
|
.trim()
|
2019-12-15 17:15:06 +01:00
|
|
|
.lines()
|
|
|
|
.map(|line| line.trim())
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.join(" ")
|
|
|
|
.trim_end()
|
|
|
|
.to_string()
|
|
|
|
}
|
|
|
|
|
2022-07-29 22:42:00 +02:00
|
|
|
pub fn nu_repl_code(source_lines: &[&str]) -> String {
|
|
|
|
let mut out = String::from("nu --testbin=nu_repl [ ");
|
|
|
|
|
|
|
|
for line in source_lines.iter() {
|
|
|
|
// convert each "line" to really be a single line to prevent nu! macro joining the newlines
|
|
|
|
// with ';'
|
|
|
|
let line = pipeline(line);
|
|
|
|
|
|
|
|
out.push('`');
|
|
|
|
out.push_str(&line);
|
|
|
|
out.push('`');
|
|
|
|
out.push(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
out.push(']');
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2020-01-23 17:21:05 +01:00
|
|
|
pub fn shell_os_paths() -> Vec<std::path::PathBuf> {
|
|
|
|
let mut original_paths = vec![];
|
|
|
|
|
2021-05-13 05:03:49 +02:00
|
|
|
if let Some(paths) = std::env::var_os(NATIVE_PATH_ENV_VAR) {
|
2020-01-23 17:21:05 +01:00
|
|
|
original_paths = std::env::split_paths(&paths).collect::<Vec<_>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
original_paths
|
|
|
|
}
|
|
|
|
|
2020-01-12 22:44:22 +01:00
|
|
|
#[cfg(test)]
|
2019-12-15 17:15:06 +01:00
|
|
|
mod tests {
|
|
|
|
use super::pipeline;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn constructs_a_pipeline() {
|
|
|
|
let actual = pipeline(
|
|
|
|
r#"
|
|
|
|
open los_tres_amigos.txt
|
|
|
|
| from-csv
|
|
|
|
| get rusty_luck
|
2022-02-04 03:01:45 +01:00
|
|
|
| into int
|
2020-06-19 04:02:01 +02:00
|
|
|
| math sum
|
2019-12-15 17:15:06 +01:00
|
|
|
| echo "$it"
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual,
|
2022-02-04 03:01:45 +01:00
|
|
|
r#"open los_tres_amigos.txt | from-csv | get rusty_luck | into int | math sum | echo "$it""#
|
2019-12-15 17:15:06 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|