add ps and early help

This commit is contained in:
JT
2021-10-02 10:53:13 +13:00
parent 5b3b74ebec
commit c5e9ff5f14
18 changed files with 957 additions and 18 deletions

View File

@ -35,4 +35,14 @@ impl Call {
false
}
pub fn get_flag_expr(&self, flag_name: &str) -> Option<Expression> {
for name in &self.named {
if flag_name == name.0 {
return name.1.clone();
}
}
None
}
}

View File

@ -1,5 +1,5 @@
use super::Command;
use crate::{ast::Block, BlockId, DeclId, Span, Type, VarId};
use crate::{ast::Block, BlockId, DeclId, Signature, Span, Type, VarId};
use core::panic;
use std::{collections::HashMap, slice::Iter};
@ -164,6 +164,21 @@ impl EngineState {
.expect("internal error: missing declaration")
}
pub fn get_decls(&self) -> Vec<Signature> {
let mut output = vec![];
for decl in self.decls.iter() {
if decl.get_block_id().is_none() {
let mut signature = (*decl).signature();
signature.usage = decl.usage().to_string();
signature.extra_usage = decl.extra_usage().to_string();
output.push(signature);
}
}
output
}
pub fn get_block(&self, block_id: BlockId) -> &Block {
self.blocks
.get(block_id)

View File

@ -1,7 +1,7 @@
use super::EngineState;
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use crate::{ShellError, Value, VarId};
use crate::{ShellError, Signature, Value, VarId};
#[derive(Clone)]
pub struct EvaluationContext {
@ -46,6 +46,10 @@ impl EvaluationContext {
pub fn print_stack(&self) {
self.stack.print_stack();
}
pub fn get_commands_info(&self) -> Vec<Signature> {
self.engine_state.borrow().get_decls()
}
}
#[derive(Debug)]

View File

@ -74,4 +74,8 @@ pub enum ShellError {
#[error("Unsupported input")]
#[diagnostic(code(nu::shell::unsupported_input), url(docsrs))]
UnsupportedInput(String, #[label("{0}")] Span),
#[error("Flag not found")]
#[diagnostic(code(nu::shell::flag_not_found), url(docsrs))]
FlagNotFound(String, #[label("{0} not found")] Span),
}

View File

@ -1,6 +1,11 @@
use miette::SourceSpan;
use serde::{Deserialize, Serialize};
pub struct Spanned<T> {
pub item: T,
pub span: Span,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Span {
pub start: usize,

View File

@ -29,6 +29,10 @@ pub enum Value {
val: u64,
span: Span,
},
Duration {
val: u64,
span: Span,
},
Range {
val: Box<Range>,
span: Span,
@ -86,6 +90,7 @@ impl Value {
Value::Int { span, .. } => *span,
Value::Float { span, .. } => *span,
Value::Filesize { span, .. } => *span,
Value::Duration { span, .. } => *span,
Value::Range { span, .. } => *span,
Value::String { span, .. } => *span,
Value::Record { span, .. } => *span,
@ -104,6 +109,7 @@ impl Value {
Value::Int { span, .. } => *span = new_span,
Value::Float { span, .. } => *span = new_span,
Value::Filesize { span, .. } => *span = new_span,
Value::Duration { span, .. } => *span = new_span,
Value::Range { span, .. } => *span = new_span,
Value::String { span, .. } => *span = new_span,
Value::Record { span, .. } => *span = new_span,
@ -125,6 +131,7 @@ impl Value {
Value::Int { .. } => Type::Int,
Value::Float { .. } => Type::Float,
Value::Filesize { .. } => Type::Filesize,
Value::Duration { .. } => Type::Duration,
Value::Range { .. } => Type::Range,
Value::String { .. } => Type::String,
Value::Record { cols, vals, .. } => {
@ -146,6 +153,7 @@ impl Value {
Value::Int { val, .. } => val.to_string(),
Value::Float { val, .. } => val.to_string(),
Value::Filesize { val, .. } => format!("{} bytes", val),
Value::Duration { val, .. } => format!("{} ns", val),
Value::Range { val, .. } => {
format!(
"range: [{}]",
@ -185,6 +193,7 @@ impl Value {
Value::Int { val, .. } => val.to_string(),
Value::Float { val, .. } => val.to_string(),
Value::Filesize { val, .. } => format!("{} bytes", val),
Value::Duration { val, .. } => format!("{} ns", val),
Value::Range { val, .. } => val
.into_iter()
.map(|x| x.into_string())