allow view-source to view aliases (#6135)

This commit is contained in:
pwygab 2022-07-26 21:06:16 +08:00 committed by GitHub
parent d856ac92f4
commit b12a3dd0e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,4 @@
use itertools::Itertools;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
@ -131,6 +132,15 @@ impl Command for ViewSource {
Vec::new(),
))
}
} else if let Some(alias_id) = engine_state.find_alias(val.as_bytes(), &[]) {
let contents = &mut engine_state.get_alias(alias_id).iter().map(|span| {
String::from_utf8_lossy(engine_state.get_span_contents(span)).to_string()
});
Ok(Value::String {
val: contents.join(" "),
span: call.head,
}
.into_pipeline_data())
} else {
Err(ShellError::GenericError(
"Cannot view value".to_string(),
@ -185,6 +195,14 @@ impl Command for ViewSource {
span: Span::test_data(),
}),
},
Example {
description: "View the source of an alias",
example: r#"alias hello = echo hi; view-source hello"#,
result: Some(Value::String {
val: "echo hi".to_string(),
span: Span::test_data(),
}),
},
]
}
}