diff --git a/src/commands/autoview.rs b/src/commands/autoview.rs index cfbcbe728..85471c4ff 100644 --- a/src/commands/autoview.rs +++ b/src/commands/autoview.rs @@ -51,7 +51,10 @@ pub fn autoview( let result = table.run(raw.with_input(input), &context.commands).await.unwrap(); result.collect::>().await; } else { - println!("TODO!") + let table = context.expect_command("table"); + let result = table.run(raw.with_input(input), &context.commands).await.unwrap(); + result.collect::>().await; + //println!("TODO!") // TODO // let mut host = context.host.lock().unwrap(); // for i in input.iter() { diff --git a/src/commands/classified.rs b/src/commands/classified.rs index c4e44244a..53ce51733 100644 --- a/src/commands/classified.rs +++ b/src/commands/classified.rs @@ -158,13 +158,18 @@ impl InternalCommand { let full_path = std::path::PathBuf::from(cwd); - let (file_extension, contents, contents_tag, _) = + let (file_extension, contents, contents_tag, span_source) = crate::commands::open::fetch( &full_path, &location, Span::unknown(), )?; + if let Some(uuid) = contents_tag.origin { + // If we have loaded something, track its source + context.add_span_source(uuid, span_source); + } + match contents { Value::Primitive(Primitive::String(string)) => { let value = crate::commands::open::parse_as_value( @@ -178,7 +183,7 @@ impl InternalCommand { } value => context .shell_manager - .push(Box::new(ValueShell::new(value.tagged(Tag::unknown())))), + .push(Box::new(ValueShell::new(value.tagged(contents_tag)))), } } } diff --git a/src/commands/open.rs b/src/commands/open.rs index 4f6edf35f..263b909fd 100644 --- a/src/commands/open.rs +++ b/src/commands/open.rs @@ -226,35 +226,43 @@ pub fn fetch( } } else { cwd.push(Path::new(location)); - match std::fs::read(&cwd) { - Ok(bytes) => match std::str::from_utf8(&bytes) { - Ok(s) => Ok(( - cwd.extension() - .map(|name| name.to_string_lossy().to_string()), - Value::string(s), - Tag { + if let Ok(cwd) = dunce::canonicalize(cwd) { + match std::fs::read(&cwd) { + Ok(bytes) => match std::str::from_utf8(&bytes) { + Ok(s) => Ok(( + cwd.extension() + .map(|name| name.to_string_lossy().to_string()), + Value::string(s), + Tag { + span, + origin: Some(Uuid::new_v4()), + }, + SpanSource::File(cwd.to_string_lossy().to_string()), + )), + Err(_) => Ok(( + None, + Value::Binary(bytes), + Tag { + span, + origin: Some(Uuid::new_v4()), + }, + SpanSource::File(cwd.to_string_lossy().to_string()), + )), + }, + Err(_) => { + return Err(ShellError::labeled_error( + "File could not be opened", + "file not found", span, - origin: Some(Uuid::new_v4()), - }, - SpanSource::File(cwd.to_string_lossy().to_string()), - )), - Err(_) => Ok(( - None, - Value::Binary(bytes), - Tag { - span, - origin: Some(Uuid::new_v4()), - }, - SpanSource::File(cwd.to_string_lossy().to_string()), - )), - }, - Err(_) => { - return Err(ShellError::labeled_error( - "File could not be opened", - "file not found", - span, - )); + )); + } } + } else { + return Err(ShellError::labeled_error( + "File could not be opened", + "file not found", + span, + )); } } } diff --git a/src/commands/shells.rs b/src/commands/shells.rs index e97fd0a9e..0c5982296 100644 --- a/src/commands/shells.rs +++ b/src/commands/shells.rs @@ -6,9 +6,17 @@ pub fn shells(args: CommandArgs, _registry: &CommandRegistry) -> Result Result { - dict.insert("origin", Value::string(source)); + tags.insert("origin", Value::string(source)); } Some(SpanSource::Url(source)) => { - dict.insert("origin", Value::string(source)); + tags.insert("origin", Value::string(source)); } _ => {} } - tags.insert_tagged("span", dict.into_tagged_value()); } tags.into_tagged_value() diff --git a/src/object/base.rs b/src/object/base.rs index 2da89c181..5dffe015d 100644 --- a/src/object/base.rs +++ b/src/object/base.rs @@ -204,7 +204,7 @@ impl fmt::Debug for ValueDebug<'a> { impl Tagged { crate fn tagged_type_name(&self) -> Tagged { let name = self.type_name(); - Tagged::from_simple_spanned_item(name, self.span()) + Tagged::from_item(name, self.tag()) } } diff --git a/src/object/meta.rs b/src/object/meta.rs index 6475ba1e8..bd43180b6 100644 --- a/src/object/meta.rs +++ b/src/object/meta.rs @@ -1,3 +1,4 @@ +use crate::context::{SourceMap, SpanSource}; use crate::prelude::*; use crate::Text; use derive_new::new; @@ -108,6 +109,14 @@ impl Tagged { self.tag.origin } + pub fn origin_name(&self, source_map: &SourceMap) -> Option { + match self.tag.origin.map(|x| source_map.get(&x)) { + Some(Some(SpanSource::File(file))) => Some(file.clone()), + Some(Some(SpanSource::Url(url))) => Some(url.clone()), + _ => None, + } + } + pub fn item(&self) -> &T { &self.item } diff --git a/src/shell/filesystem_shell.rs b/src/shell/filesystem_shell.rs index 2f709d176..db1be0a1a 100644 --- a/src/shell/filesystem_shell.rs +++ b/src/shell/filesystem_shell.rs @@ -1,4 +1,5 @@ use crate::commands::command::EvaluatedStaticCommandArgs; +use crate::context::SourceMap; use crate::object::dir_entry_dict; use crate::prelude::*; use crate::shell::completer::NuCompleter; @@ -55,7 +56,7 @@ impl FilesystemShell { } impl Shell for FilesystemShell { - fn name(&self) -> String { + fn name(&self, _source_map: &SourceMap) -> String { "filesystem".to_string() } diff --git a/src/shell/shell.rs b/src/shell/shell.rs index fd0b5ed5c..416717c26 100644 --- a/src/shell/shell.rs +++ b/src/shell/shell.rs @@ -1,9 +1,10 @@ use crate::commands::command::EvaluatedStaticCommandArgs; +use crate::context::SourceMap; use crate::errors::ShellError; use crate::stream::OutputStream; pub trait Shell { - fn name(&self) -> String; + fn name(&self, source_map: &SourceMap) -> String; fn ls(&self, args: EvaluatedStaticCommandArgs) -> Result; fn cd(&self, args: EvaluatedStaticCommandArgs) -> Result; fn path(&self) -> String; diff --git a/src/shell/shell_manager.rs b/src/shell/shell_manager.rs index 205db391e..9c05707ba 100644 --- a/src/shell/shell_manager.rs +++ b/src/shell/shell_manager.rs @@ -73,8 +73,8 @@ impl ShellManager { pub fn next(&mut self) { { let mut x = self.shells.lock().unwrap(); - let shell = x.pop().unwrap(); - x.insert(0, shell); + let shell = x.remove(0); + x.push(shell); } self.set_path(self.path()); } @@ -82,8 +82,8 @@ impl ShellManager { pub fn prev(&mut self) { { let mut x = self.shells.lock().unwrap(); - let shell = x.remove(0); - x.push(shell); + let shell = x.pop().unwrap(); + x.insert(0, shell); } self.set_path(self.path()); } diff --git a/src/shell/value_shell.rs b/src/shell/value_shell.rs index 34bb5c291..d8b267dbc 100644 --- a/src/shell/value_shell.rs +++ b/src/shell/value_shell.rs @@ -1,4 +1,5 @@ use crate::commands::command::EvaluatedStaticCommandArgs; +use crate::context::SourceMap; use crate::prelude::*; use crate::shell::shell::Shell; use std::ffi::OsStr; @@ -53,8 +54,15 @@ impl ValueShell { } impl Shell for ValueShell { - fn name(&self) -> String { - "value".to_string() + fn name(&self, source_map: &SourceMap) -> String { + let origin_name = self.value.origin_name(source_map); + format!( + "{}", + match origin_name { + Some(x) => format!("{{{}}}", x), + None => format!("<{}>", self.value.item.type_name(),), + } + ) } fn ls(&self, _args: EvaluatedStaticCommandArgs) -> Result {