1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use itertools::Itertools;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type,
Value,
};
#[derive(Clone)]
pub struct ViewSource;
impl Command for ViewSource {
fn name(&self) -> &str {
"view-source"
}
fn usage(&self) -> &str {
"View a block, module, or a definition"
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("view-source")
.input_output_types(vec![(Type::Nothing, Type::String)])
.required("item", SyntaxShape::Any, "name or block to view")
.category(Category::Core)
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let arg: Value = call.req(engine_state, stack, 0)?;
let arg_span = arg.span()?;
match arg {
Value::Block { val: block_id, .. } => {
let block = engine_state.get_block(block_id);
if let Some(span) = block.span {
let contents = engine_state.get_span_contents(&span);
Ok(Value::string(String::from_utf8_lossy(contents), call.head)
.into_pipeline_data())
} else {
Ok(Value::string("<internal command>", call.head).into_pipeline_data())
}
}
Value::String { val, .. } => {
if let Some(decl_id) = engine_state.find_decl(val.as_bytes(), &[]) {
let decl = engine_state.get_decl(decl_id);
let sig = decl.signature();
let vec_of_required = &sig.required_positional;
let vec_of_optional = &sig.optional_positional;
let vec_of_flags = &sig.named;
if let Some(block_id) = decl.get_block_id() {
let block = engine_state.get_block(block_id);
if let Some(block_span) = block.span {
let contents = engine_state.get_span_contents(&block_span);
let mut final_contents = String::from("def ");
final_contents.push_str(&val);
final_contents.push_str(" [ ");
for n in vec_of_required {
final_contents.push_str(&n.name);
final_contents.push(':');
final_contents.push_str(&n.shape.to_string());
final_contents.push(' ');
}
for n in vec_of_optional {
final_contents.push_str(&n.name);
final_contents.push_str("?:");
final_contents.push_str(&n.shape.to_string());
final_contents.push(' ');
}
for n in vec_of_flags {
final_contents.push_str("--");
final_contents.push_str(&n.long);
final_contents.push(' ');
if n.short.is_some() {
final_contents.push_str("(-");
final_contents.push(n.short.expect("this cannot trigger."));
final_contents.push(')');
}
if n.arg.is_some() {
final_contents.push_str(": ");
final_contents.push_str(
&n.arg.as_ref().expect("this cannot trigger.").to_string(),
);
}
final_contents.push(' ');
}
final_contents.push_str("] ");
final_contents.push_str(&String::from_utf8_lossy(contents));
Ok(Value::string(final_contents, call.head).into_pipeline_data())
} else {
Err(ShellError::GenericError(
"Cannot view value".to_string(),
"the command does not have a viewable block".to_string(),
Some(arg_span),
None,
Vec::new(),
))
}
} else {
Err(ShellError::GenericError(
"Cannot view value".to_string(),
"the command does not have a viewable block".to_string(),
Some(arg_span),
None,
Vec::new(),
))
}
} else if let Some(module_id) = engine_state.find_module(val.as_bytes(), &[]) {
let module = engine_state.get_module(module_id);
if let Some(module_span) = module.span {
let contents = engine_state.get_span_contents(&module_span);
Ok(Value::string(String::from_utf8_lossy(contents), call.head)
.into_pipeline_data())
} else {
Err(ShellError::GenericError(
"Cannot view value".to_string(),
"the module does not have a viewable block".to_string(),
Some(arg_span),
None,
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(),
"this name does not correspond to a viewable value".to_string(),
Some(arg_span),
None,
Vec::new(),
))
}
}
_ => Err(ShellError::GenericError(
"Cannot view value".to_string(),
"this value cannot be viewed".to_string(),
Some(arg_span),
None,
Vec::new(),
)),
}
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "View the source of a code block",
example: r#"let abc = { echo 'hi' }; view-source $abc"#,
result: Some(Value::test_string("{ echo 'hi' }")),
},
Example {
description: "View the source of a custom command",
example: r#"def hi [] { echo 'Hi!' }; view-source hi"#,
result: Some(Value::test_string("{ echo 'Hi!' }")),
},
Example {
description: "View the source of a custom command, which participates in the caller environment",
example: r#"def-env foo [] { let-env BAR = 'BAZ' }; view-source foo"#,
result: Some(Value::test_string("{ let-env BAR = 'BAZ' }")),
},
Example {
description: "View the source of a module",
example: r#"module mod-foo { export-env { let-env FOO_ENV = 'BAZ' } }; view-source mod-foo"#,
result: Some(Value::test_string(" export-env { let-env FOO_ENV = 'BAZ' }")),
},
Example {
description: "View the source of an alias",
example: r#"alias hello = echo hi; view-source hello"#,
result: Some(Value::test_string("echo hi")),
},
]
}
}