2020-04-20 08:41:51 +02:00
use crate ::commands ::classified ::block ::run_block ;
2020-07-03 21:53:20 +02:00
use crate ::commands ::classified ::maybe_text_codec ::{ MaybeTextCodec , StringOrBinary } ;
2020-09-19 23:29:51 +02:00
use crate ::evaluation_context ::EvaluationContext ;
2020-04-27 19:49:53 +02:00
use crate ::path ::canonicalize ;
2019-07-03 19:37:09 +02:00
use crate ::prelude ::* ;
2020-09-17 08:02:30 +02:00
#[ cfg(feature = " rustyline-support " ) ]
2020-07-22 23:43:52 +02:00
use crate ::shell ::Helper ;
2020-05-14 02:35:22 +02:00
use crate ::EnvironmentSyncer ;
2020-03-06 17:06:39 +01:00
use futures_codec ::FramedRead ;
2020-09-17 01:22:58 +02:00
use nu_errors ::ShellError ;
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
use nu_protocol ::hir ::{ ClassifiedCommand , Expression , InternalCommand , Literal , NamedArguments } ;
2020-09-26 01:40:02 +02:00
use nu_protocol ::{ Primitive , ReturnSuccess , Scope , UntaggedValue , Value } ;
2019-05-23 06:30:43 +02:00
2020-04-06 09:16:14 +02:00
use log ::{ debug , trace } ;
2020-09-17 08:02:30 +02:00
#[ cfg(feature = " rustyline-support " ) ]
use rustyline ::{
self ,
config ::Configurer ,
config ::{ ColorMode , CompletionType , Config } ,
error ::ReadlineError ,
At , Cmd , Editor , KeyPress , Movement , Word ,
} ;
2019-05-23 06:30:43 +02:00
use std ::error ::Error ;
2019-05-24 09:29:16 +02:00
use std ::iter ::Iterator ;
2020-04-27 19:49:53 +02:00
use std ::path ::{ Path , PathBuf } ;
2019-10-13 06:12:43 +02:00
use std ::sync ::atomic ::Ordering ;
2019-05-23 06:30:43 +02:00
2020-08-27 09:57:40 +02:00
pub fn search_paths ( ) -> Vec < std ::path ::PathBuf > {
2020-03-04 19:58:20 +01:00
use std ::env ;
2019-07-04 05:06:43 +02:00
2020-03-04 19:58:20 +01:00
let mut search_paths = Vec ::new ( ) ;
2019-09-13 01:49:29 +02:00
2020-03-04 19:58:20 +01:00
// Automatically add path `nu` is in as a search path
if let Ok ( exe_path ) = env ::current_exe ( ) {
if let Some ( exe_dir ) = exe_path . parent ( ) {
search_paths . push ( exe_dir . to_path_buf ( ) ) ;
2019-09-13 01:49:29 +02:00
}
2019-08-27 03:46:38 +02:00
}
2019-07-03 19:37:09 +02:00
2020-08-18 09:00:02 +02:00
if let Ok ( config ) = nu_data ::config ::config ( Tag ::unknown ( ) ) {
2020-05-28 21:14:32 +02:00
if let Some ( plugin_dirs ) = config . get ( " plugin_dirs " ) {
if let Value {
value : UntaggedValue ::Table ( pipelines ) ,
..
} = plugin_dirs
{
for pipeline in pipelines {
if let Ok ( plugin_dir ) = pipeline . as_string ( ) {
search_paths . push ( PathBuf ::from ( plugin_dir ) ) ;
}
}
2019-11-10 04:44:05 +01:00
}
}
2019-09-12 05:20:42 +02:00
}
search_paths
}
2020-09-19 23:29:51 +02:00
pub fn create_default_context ( interactive : bool ) -> Result < EvaluationContext , Box < dyn Error > > {
let mut context = EvaluationContext ::basic ( ) ? ;
2019-05-23 06:30:43 +02:00
{
use crate ::commands ::* ;
context . add_commands ( vec! [
2020-09-14 16:07:02 +02:00
whole_stream_command ( NuPlugin ) ,
2019-12-09 19:39:51 +01:00
// System/file operations
2020-09-05 04:27:01 +02:00
whole_stream_command ( Exec ) ,
2019-12-28 22:33:31 +01:00
whole_stream_command ( Pwd ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( Ls ) ,
whole_stream_command ( Du ) ,
2019-12-28 22:33:31 +01:00
whole_stream_command ( Cd ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( Remove ) ,
whole_stream_command ( Open ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( Config ) ,
2020-07-11 02:11:04 +02:00
whole_stream_command ( ConfigGet ) ,
whole_stream_command ( ConfigSet ) ,
whole_stream_command ( ConfigSetInto ) ,
whole_stream_command ( ConfigClear ) ,
whole_stream_command ( ConfigLoad ) ,
whole_stream_command ( ConfigRemove ) ,
whole_stream_command ( ConfigPath ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( Help ) ,
whole_stream_command ( History ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( Save ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( Touch ) ,
whole_stream_command ( Cpy ) ,
2020-08-22 07:06:19 +02:00
whole_stream_command ( Date ) ,
2020-08-22 05:46:48 +02:00
whole_stream_command ( DateNow ) ,
whole_stream_command ( DateUTC ) ,
whole_stream_command ( DateFormat ) ,
2020-05-10 01:05:48 +02:00
whole_stream_command ( Cal ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( Mkdir ) ,
2020-07-06 17:27:01 +02:00
whole_stream_command ( Mv ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( Kill ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( Version ) ,
2020-01-20 08:05:32 +01:00
whole_stream_command ( Clear ) ,
2020-09-27 03:54:47 +02:00
whole_stream_command ( Describe ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( Which ) ,
whole_stream_command ( Debug ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( Alias ) ,
2020-05-06 05:56:31 +02:00
whole_stream_command ( WithEnv ) ,
2020-06-27 00:37:31 +02:00
whole_stream_command ( Do ) ,
2020-08-21 19:51:29 +02:00
whole_stream_command ( Sleep ) ,
2019-12-09 19:39:51 +01:00
// Statistics
2019-08-19 07:16:39 +02:00
whole_stream_command ( Size ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( Count ) ,
2020-07-19 19:39:43 +02:00
whole_stream_command ( Benchmark ) ,
2019-12-09 19:39:51 +01:00
// Metadata
whole_stream_command ( Tags ) ,
// Shells
2019-08-19 07:16:39 +02:00
whole_stream_command ( Next ) ,
whole_stream_command ( Previous ) ,
whole_stream_command ( Shells ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( Enter ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( Exit ) ,
// Viewers
whole_stream_command ( Autoview ) ,
whole_stream_command ( Table ) ,
// Text manipulation
2020-05-24 08:41:30 +02:00
whole_stream_command ( Split ) ,
2019-08-19 07:16:39 +02:00
whole_stream_command ( SplitColumn ) ,
whole_stream_command ( SplitRow ) ,
2020-07-03 21:09:38 +02:00
whole_stream_command ( SplitChars ) ,
2019-08-19 07:16:39 +02:00
whole_stream_command ( Lines ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( Echo ) ,
2020-06-04 22:01:24 +02:00
whole_stream_command ( Parse ) ,
2020-05-27 00:19:18 +02:00
whole_stream_command ( Str ) ,
2020-06-01 23:02:57 +02:00
whole_stream_command ( StrToDecimal ) ,
2020-05-27 00:19:18 +02:00
whole_stream_command ( StrToInteger ) ,
whole_stream_command ( StrDowncase ) ,
whole_stream_command ( StrUpcase ) ,
whole_stream_command ( StrCapitalize ) ,
whole_stream_command ( StrFindReplace ) ,
2020-07-12 05:57:39 +02:00
whole_stream_command ( StrFrom ) ,
2020-05-27 00:19:18 +02:00
whole_stream_command ( StrSubstring ) ,
whole_stream_command ( StrSet ) ,
whole_stream_command ( StrToDatetime ) ,
2020-08-04 08:36:51 +02:00
whole_stream_command ( StrContains ) ,
whole_stream_command ( StrIndexOf ) ,
2020-05-27 00:19:18 +02:00
whole_stream_command ( StrTrim ) ,
2020-07-26 20:09:35 +02:00
whole_stream_command ( StrTrimLeft ) ,
whole_stream_command ( StrTrimRight ) ,
2020-07-30 06:51:20 +02:00
whole_stream_command ( StrStartsWith ) ,
whole_stream_command ( StrEndsWith ) ,
2020-06-27 07:38:19 +02:00
whole_stream_command ( StrCollect ) ,
2020-07-03 22:17:44 +02:00
whole_stream_command ( StrLength ) ,
2020-07-14 22:47:04 +02:00
whole_stream_command ( StrReverse ) ,
2020-08-17 22:18:23 +02:00
whole_stream_command ( StrCamelCase ) ,
whole_stream_command ( StrPascalCase ) ,
whole_stream_command ( StrKebabCase ) ,
whole_stream_command ( StrSnakeCase ) ,
whole_stream_command ( StrScreamingSnakeCase ) ,
2020-05-19 21:27:26 +02:00
whole_stream_command ( BuildString ) ,
2020-06-27 00:37:31 +02:00
whole_stream_command ( Ansi ) ,
2020-06-27 23:46:30 +02:00
whole_stream_command ( Char ) ,
2019-12-09 19:39:51 +01:00
// Column manipulation
2020-07-06 17:27:01 +02:00
whole_stream_command ( MoveColumn ) ,
2019-08-19 07:16:39 +02:00
whole_stream_command ( Reject ) ,
2020-05-07 13:03:43 +02:00
whole_stream_command ( Select ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( Get ) ,
2020-05-07 07:33:30 +02:00
whole_stream_command ( Update ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( Insert ) ,
2020-08-27 07:44:18 +02:00
whole_stream_command ( IntoInt ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( SplitBy ) ,
// Row manipulation
2019-08-25 18:14:17 +02:00
whole_stream_command ( Reverse ) ,
2019-10-30 07:54:06 +01:00
whole_stream_command ( Append ) ,
whole_stream_command ( Prepend ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( SortBy ) ,
whole_stream_command ( GroupBy ) ,
2020-05-15 11:16:09 +02:00
whole_stream_command ( GroupByDate ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( First ) ,
whole_stream_command ( Last ) ,
2020-06-16 21:58:41 +02:00
whole_stream_command ( Every ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( Nth ) ,
2020-04-26 08:34:45 +02:00
whole_stream_command ( Drop ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( Format ) ,
whole_stream_command ( Where ) ,
2020-07-04 21:40:04 +02:00
whole_stream_command ( If ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( Compact ) ,
whole_stream_command ( Default ) ,
2020-04-30 06:18:24 +02:00
whole_stream_command ( Skip ) ,
whole_stream_command ( SkipUntil ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( SkipWhile ) ,
2020-04-30 06:18:24 +02:00
whole_stream_command ( Keep ) ,
whole_stream_command ( KeepUntil ) ,
whole_stream_command ( KeepWhile ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( Range ) ,
2020-03-03 22:01:24 +01:00
whole_stream_command ( Rename ) ,
2019-12-31 05:05:02 +01:00
whole_stream_command ( Uniq ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( Each ) ,
2020-09-07 07:54:52 +02:00
whole_stream_command ( EachGroup ) ,
whole_stream_command ( EachWindow ) ,
2020-04-27 04:04:54 +02:00
whole_stream_command ( IsEmpty ) ,
2019-12-09 19:39:51 +01:00
// Table manipulation
2020-07-06 17:27:01 +02:00
whole_stream_command ( Move ) ,
2020-04-30 06:18:24 +02:00
whole_stream_command ( Merge ) ,
2020-03-10 23:00:08 +01:00
whole_stream_command ( Shuffle ) ,
2019-12-09 19:39:51 +01:00
whole_stream_command ( Wrap ) ,
whole_stream_command ( Pivot ) ,
2020-03-29 04:05:57 +02:00
whole_stream_command ( Headers ) ,
2020-08-04 19:16:19 +02:00
whole_stream_command ( Reduce ) ,
2019-12-09 19:39:51 +01:00
// Data processing
whole_stream_command ( Histogram ) ,
Autoenv rewrite, security and scripting (#2083)
* Add args in .nurc file to environment
* Working dummy version
* Add add_nurc to sync_env command
* Parse .nurc file
* Delete env vars after leaving directory
* Removing vals not working, strangely
* Refactoring, add comment
* Debugging
* Debug by logging to file
* Add and remove env var behavior appears correct
However, it does not use existing code that well.
* Move work to cli.rs
* Parse config directories
* I am in a state of distress
* Rename .nurc to .nu
* Some notes for me
* Refactoring
* Removing vars works, but not done in a very nice fashion
* Refactor env_vars_to_delete
* Refactor env_vars_to_add()
* Move directory environment code to separate file
* Refactor from_config
* Restore env values
* Working?
* Working?
* Update comments and change var name
* Formatting
* Remove vars after leaving dir
* Remove notes I made
* Rename config function
* Clippy
* Cleanup and handle errors
* cargo fmt
* Better error messages, remove last (?) unwrap
* FORMAT PLZ
* Rename whitelisted_directories to allowed_directories
* Add comment to clarify how overwritten values are restored.
* Change list of allowed dirs to indexmap
* Rewrite starting
* rewrite everything
* Overwritten env values tracks an indexmap instead of vector
* Refactor restore function
* Untrack removed vars properly
* Performance concerns
* Performance concerns
* Error handling
* Clippy
* Add type aliases for String and OsString
* Deletion almost works
* Working?
* Error handling and refactoring
* nicer errors
* Add TODO file
* Move outside of loop
* Error handling
* Reworking adding of vars
* Reworking adding of vars
* Ready for testing
* Refactoring
* Restore overwritten vals code
* todo.org
* Remove overwritten values tracking, as it is not needed
* Cleanup, stop tracking overwritten values as nu takes care of it
* Init autoenv command
* Initialize autoenv and autoenv trust
* autoenv trust toml
* toml
* Use serde for autoenv
* Optional directory arg
* Add autoenv untrust command
* ... actually add autoenv untrust this time
* OsString and paths
* Revert "OsString and paths"
This reverts commit e6eedf882498c1365ecfc899e5ec11bd83cb055c.
* Fix path
* Fix path
* Autoenv trust and untrust
* Start using autoenv
* Check hashes
* Use trust functionality when setting vars
* Remove unused code
* Clippy
* Nicer errors for autoenv commands
* Non-working errors
* Update error description
* Satisfy fmt
* Errors
* Errors print, but not nicely
* Nicer errors
* fmt
* Delete accidentally added todo.org file
* Rename direnv to autoenv
* Use ShellError instead of Error
* Change tests to pass, danger zone?
* Clippy and errors
* Clippy... again
* Replace match with or_else
* Use sha2 crate for hashing
* parsing and error msg
* Refactoring
* Only apply vars once
* if parent dir
* Delete vars
* Rework exit code
* Adding works
* restore
* Fix possibility of infinite loop
* Refactoring
* Non-working
* Revert "Non-working"
This reverts commit e231b85570bcb3fc838f950e9f5004c6a7c5a2ac.
* Revert "Revert "Non-working""
This reverts commit 804092e46a752266576b044401cc97c317e41f21.
* Autoenv trust works without restart
* Cargo fix
* Script vars
* Serde
* Serde errors
* Entry and exitscripts
* Clippy
* Support windows and handle errors
* Formatting
* Fix infinite loop on windows
* Debugging windows loop
* More windows infinite loop debugging
* Windows loop debugging #3
* windows loop #4
* Don't return err
* Cleanup unused code
* Infinite loop debug
* Loop debugging
* Check if infinite loop is vars_to_add
* env_vars_to_add does not terminate, skip loop as test
* Hypothesis: std::env::current_dir() is messing with something
* Hypothesis: std::env::current_dir() is messing with something
* plz
* make clippy happy
* debugging in env_vars_to_add
* Debbuging env_vars_to_add #2
* clippy
* clippy..
* Fool clippy
* Fix another infinite loop
* Binary search for error location x)
* Binary search #3
* fmt
* Binary search #4
* more searching...
* closing in... maybe
* PLZ
* Cleanup
* Restore commented out functionality
* Handle case when user gives the directory "."
* fmt
* Use fs::canonicalize for paths
* Create optional script section
* fmt
* Add exitscripts even if no entryscripts are defined
* All sections in .nu-env are now optional
* Re-read config file each directory change
* Hot reload after autoenv untrust, don't run exitscripts if untrusted
* Debugging
* Fix issue with recursive adding of vars
* Thank you for finding my issues Mr. Azure
* use std::env
2020-07-05 19:34:00 +02:00
whole_stream_command ( Autoenv ) ,
whole_stream_command ( AutoenvTrust ) ,
whole_stream_command ( AutoenvUnTrust ) ,
2020-06-13 23:49:57 +02:00
whole_stream_command ( Math ) ,
2020-06-18 23:37:18 +02:00
whole_stream_command ( MathAverage ) ,
2020-07-18 06:11:19 +02:00
whole_stream_command ( MathEval ) ,
2020-06-18 23:37:18 +02:00
whole_stream_command ( MathMedian ) ,
whole_stream_command ( MathMinimum ) ,
2020-06-24 19:57:27 +02:00
whole_stream_command ( MathMode ) ,
2020-06-18 23:37:18 +02:00
whole_stream_command ( MathMaximum ) ,
2020-07-14 21:15:02 +02:00
whole_stream_command ( MathStddev ) ,
2020-06-19 04:02:01 +02:00
whole_stream_command ( MathSummation ) ,
2020-07-14 21:15:02 +02:00
whole_stream_command ( MathVariance ) ,
2020-08-27 07:58:01 +02:00
whole_stream_command ( MathProduct ) ,
2019-12-09 19:39:51 +01:00
// File format output
2020-05-04 10:44:33 +02:00
whole_stream_command ( To ) ,
2019-08-19 07:16:39 +02:00
whole_stream_command ( ToCSV ) ,
2020-03-15 04:04:44 +01:00
whole_stream_command ( ToHTML ) ,
2019-08-19 07:16:39 +02:00
whole_stream_command ( ToJSON ) ,
2020-03-19 20:18:24 +01:00
whole_stream_command ( ToMarkdown ) ,
2019-08-19 07:16:39 +02:00
whole_stream_command ( ToTOML ) ,
2019-08-29 11:02:16 +02:00
whole_stream_command ( ToTSV ) ,
2019-09-19 06:25:29 +02:00
whole_stream_command ( ToURL ) ,
2019-08-19 07:16:39 +02:00
whole_stream_command ( ToYAML ) ,
2020-07-24 09:41:22 +02:00
whole_stream_command ( ToXML ) ,
2019-12-09 19:39:51 +01:00
// File format input
2020-05-04 10:44:33 +02:00
whole_stream_command ( From ) ,
2019-08-19 07:16:39 +02:00
whole_stream_command ( FromCSV ) ,
2020-04-26 06:26:35 +02:00
whole_stream_command ( FromEML ) ,
2019-08-29 11:02:16 +02:00
whole_stream_command ( FromTSV ) ,
2019-10-13 22:50:45 +02:00
whole_stream_command ( FromSSV ) ,
2019-08-19 07:16:39 +02:00
whole_stream_command ( FromINI ) ,
whole_stream_command ( FromJSON ) ,
2020-01-07 07:35:00 +01:00
whole_stream_command ( FromODS ) ,
2019-08-19 07:16:39 +02:00
whole_stream_command ( FromTOML ) ,
2019-09-19 06:25:29 +02:00
whole_stream_command ( FromURL ) ,
2019-11-17 04:18:41 +01:00
whole_stream_command ( FromXLSX ) ,
2019-08-19 07:16:39 +02:00
whole_stream_command ( FromXML ) ,
whole_stream_command ( FromYAML ) ,
2019-08-29 05:53:45 +02:00
whole_stream_command ( FromYML ) ,
2020-03-20 20:35:09 +01:00
whole_stream_command ( FromIcs ) ,
whole_stream_command ( FromVcf ) ,
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
// "Private" commands (not intended to be accessed directly)
2020-04-27 03:22:01 +02:00
whole_stream_command ( RunExternalCommand { interactive } ) ,
2020-06-25 07:51:09 +02:00
// Random value generation
whole_stream_command ( Random ) ,
2020-06-26 06:51:05 +02:00
whole_stream_command ( RandomBool ) ,
2020-06-30 06:12:51 +02:00
whole_stream_command ( RandomDice ) ,
2020-07-18 03:59:23 +02:00
#[ cfg(feature = " uuid_crate " ) ]
2020-06-25 07:51:09 +02:00
whole_stream_command ( RandomUUID ) ,
2020-09-03 21:23:02 +02:00
whole_stream_command ( RandomInteger ) ,
2020-07-25 21:29:15 +02:00
// Path
whole_stream_command ( PathBasename ) ,
2020-08-26 21:47:23 +02:00
whole_stream_command ( PathCommand ) ,
whole_stream_command ( PathDirname ) ,
2020-07-27 04:12:07 +02:00
whole_stream_command ( PathExists ) ,
2020-08-26 21:47:23 +02:00
whole_stream_command ( PathExpand ) ,
whole_stream_command ( PathExtension ) ,
whole_stream_command ( PathFilestem ) ,
2020-07-27 04:12:07 +02:00
whole_stream_command ( PathType ) ,
2020-07-29 22:56:56 +02:00
// Url
whole_stream_command ( UrlCommand ) ,
whole_stream_command ( UrlScheme ) ,
whole_stream_command ( UrlPath ) ,
whole_stream_command ( UrlHost ) ,
whole_stream_command ( UrlQuery ) ,
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
] ) ;
2019-08-23 05:29:08 +02:00
2020-09-03 21:21:32 +02:00
#[ cfg(feature = " clipboard-cli " ) ]
2019-08-23 05:29:08 +02:00
{
2020-05-18 14:56:01 +02:00
context . add_commands ( vec! [ whole_stream_command ( crate ::commands ::clip ::Clip ) ] ) ;
2019-08-23 05:29:08 +02:00
}
2019-05-23 06:30:43 +02:00
}
Overhaul the coloring system
This commit replaces the previous naive coloring system with a coloring
system that is more aligned with the parser.
The main benefit of this change is that it allows us to use parsing
rules to decide how to color tokens.
For example, consider the following syntax:
```
$ ps | where cpu > 10
```
Ideally, we could color `cpu` like a column name and not a string,
because `cpu > 10` is a shorthand block syntax that expands to
`{ $it.cpu > 10 }`.
The way that we know that it's a shorthand block is that the `where`
command declares that its first parameter is a `SyntaxShape::Block`,
which allows the shorthand block form.
In order to accomplish this, we need to color the tokens in a way that
corresponds to their expanded semantics, which means that high-fidelity
coloring requires expansion.
This commit adds a `ColorSyntax` trait that corresponds to the
`ExpandExpression` trait. The semantics are fairly similar, with a few
differences.
First `ExpandExpression` consumes N tokens and returns a single
`hir::Expression`. `ColorSyntax` consumes N tokens and writes M
`FlatShape` tokens to the output.
Concretely, for syntax like `[1 2 3]`
- `ExpandExpression` takes a single token node and produces a single
`hir::Expression`
- `ColorSyntax` takes the same token node and emits 7 `FlatShape`s
(open delimiter, int, whitespace, int, whitespace, int, close
delimiter)
Second, `ColorSyntax` is more willing to plow through failures than
`ExpandExpression`.
In particular, consider syntax like
```
$ ps | where cpu >
```
In this case
- `ExpandExpression` will see that the `where` command is expecting a
block, see that it's not a literal block and try to parse it as a
shorthand block. It will successfully find a member followed by an
infix operator, but not a following expression. That means that the
entire pipeline part fails to parse and is a syntax error.
- `ColorSyntax` will also try to parse it as a shorthand block and
ultimately fail, but it will fall back to "backoff coloring mode",
which parsing any unidentified tokens in an unfallible, simple way. In
this case, `cpu` will color as a string and `>` will color as an
operator.
Finally, it's very important that coloring a pipeline infallibly colors
the entire string, doesn't fail, and doesn't get stuck in an infinite
loop.
In order to accomplish this, this PR separates `ColorSyntax`, which is
infallible from `FallibleColorSyntax`, which might fail. This allows the
type system to let us know if our coloring rules bottom out at at an
infallible rule.
It's not perfect: it's still possible for the coloring process to get
stuck or consume tokens non-atomically. I intend to reduce the
opportunity for those problems in a future commit. In the meantime, the
current system catches a number of mistakes (like trying to use a
fallible coloring rule in a loop without thinking about the possibility
that it will never terminate).
2019-10-06 22:22:50 +02:00
2020-02-09 03:24:33 +01:00
Ok ( context )
}
2020-04-15 19:50:35 +02:00
pub async fn run_vec_of_pipelines (
pipelines : Vec < String > ,
redirect_stdin : bool ,
) -> Result < ( ) , Box < dyn Error > > {
2020-09-17 01:22:58 +02:00
let mut syncer = EnvironmentSyncer ::new ( ) ;
let mut context = create_default_context ( false ) ? ;
let config = syncer . get_config ( ) ;
2020-04-15 19:50:35 +02:00
2020-09-17 01:22:58 +02:00
context . configure ( & config , | _ , ctx | {
syncer . load_environment ( ) ;
syncer . sync_env_vars ( ctx ) ;
syncer . sync_path_vars ( ctx ) ;
2020-04-15 19:50:35 +02:00
2020-09-17 01:22:58 +02:00
if let Err ( reason ) = syncer . autoenv ( ctx ) {
print_err ( reason , & Text ::from ( " " ) ) ;
2020-07-18 03:59:23 +02:00
}
2020-04-15 19:50:35 +02:00
2020-09-17 01:22:58 +02:00
let _ = register_plugins ( ctx ) ;
let _ = configure_ctrl_c ( ctx ) ;
} ) ;
let _ = run_startup_commands ( & mut context , & config ) . await ;
2020-04-15 19:50:35 +02:00
for pipeline in pipelines {
2020-04-17 08:19:49 +02:00
run_pipeline_standalone ( pipeline , redirect_stdin , & mut context , true ) . await ? ;
2020-04-15 19:50:35 +02:00
}
2020-02-09 03:24:33 +01:00
Ok ( ( ) )
}
2020-09-17 08:02:30 +02:00
#[ cfg(feature = " rustyline-support " ) ]
fn convert_rustyline_result_to_string ( input : Result < String , ReadlineError > ) -> LineResult {
match input {
Ok ( s ) = > LineResult ::Success ( s ) ,
Err ( ReadlineError ::Interrupted ) = > LineResult ::CtrlC ,
2020-09-21 09:56:10 +02:00
Err ( ReadlineError ::Eof ) = > LineResult ::CtrlD ,
2020-09-17 08:02:30 +02:00
Err ( err ) = > {
outln! ( " Error: {:?} " , err ) ;
LineResult ::Break
}
}
}
2020-09-17 01:22:58 +02:00
/// The entry point for the CLI. Will register all known internal commands, load experimental commands, load plugins, then prepare the prompt and line reader for input.
2020-09-17 08:02:30 +02:00
#[ cfg(feature = " rustyline-support " ) ]
2020-09-19 23:29:51 +02:00
pub async fn cli ( mut context : EvaluationContext ) -> Result < ( ) , Box < dyn Error > > {
2020-09-17 01:22:58 +02:00
let mut syncer = EnvironmentSyncer ::new ( ) ;
let configuration = syncer . get_config ( ) ;
2019-12-31 05:06:36 +01:00
2020-09-17 01:22:58 +02:00
let mut rl = default_rustyline_editor_configuration ( ) ;
2020-08-05 23:34:28 +02:00
2020-09-17 01:22:58 +02:00
context . configure ( & configuration , | config , ctx | {
syncer . load_environment ( ) ;
syncer . sync_env_vars ( ctx ) ;
syncer . sync_path_vars ( ctx ) ;
2020-07-15 09:51:59 +02:00
2020-09-17 01:22:58 +02:00
if let Err ( reason ) = syncer . autoenv ( ctx ) {
print_err ( reason , & Text ::from ( " " ) ) ;
2020-07-22 23:43:52 +02:00
}
2020-09-17 01:22:58 +02:00
let _ = configure_ctrl_c ( ctx ) ;
let _ = configure_rustyline_editor ( & mut rl , config ) ;
2020-07-22 23:43:52 +02:00
2020-09-17 01:22:58 +02:00
let helper = Some ( nu_line_editor_helper ( ctx , config ) ) ;
rl . set_helper ( helper ) ;
} ) ;
2020-07-22 23:43:52 +02:00
2020-09-17 01:22:58 +02:00
let _ = run_startup_commands ( & mut context , & configuration ) . await ;
2020-08-27 13:06:25 +02:00
2020-09-17 01:22:58 +02:00
let history_path = crate ::commands ::history ::history_path ( & configuration ) ;
2020-08-27 13:06:25 +02:00
let _ = rl . load_history ( & history_path ) ;
2020-08-09 01:38:21 +02:00
2020-09-17 01:22:58 +02:00
let skip_welcome_message = configuration
. var ( " skip_welcome_message " )
2020-08-09 01:38:21 +02:00
. map ( | x | x . is_true ( ) )
. unwrap_or ( false ) ;
if ! skip_welcome_message {
println! (
" Welcome to Nushell {} (type 'help' for more info) " ,
clap ::crate_version! ( )
) ;
}
2019-05-26 08:54:41 +02:00
#[ cfg(windows) ]
{
let _ = ansi_term ::enable_ansi_support ( ) ;
}
2019-06-15 20:36:17 +02:00
let mut ctrlcbreak = false ;
2020-04-15 07:43:23 +02:00
2019-05-23 06:30:43 +02:00
loop {
2019-10-13 06:12:43 +02:00
if context . ctrl_c . load ( Ordering ::SeqCst ) {
context . ctrl_c . store ( false , Ordering ::SeqCst ) ;
2019-06-07 02:31:22 +02:00
continue ;
}
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
let cwd = context . shell_manager . path ( ) ;
2019-08-07 19:49:11 +02:00
2019-11-16 21:02:26 +01:00
let colored_prompt = {
2020-09-17 01:22:58 +02:00
if let Some ( prompt ) = configuration . var ( " prompt " ) {
2020-06-27 00:37:31 +02:00
let prompt_line = prompt . as_string ( ) ? ;
2020-06-28 23:06:05 +02:00
match nu_parser ::lite_parse ( & prompt_line , 0 ) . map_err ( ShellError ::from ) {
Ok ( result ) = > {
let mut prompt_block =
nu_parser ::classify_block ( & result , context . registry ( ) ) ;
let env = context . get_env ( ) ;
prompt_block . block . expand_it_usage ( ) ;
match run_block (
& prompt_block . block ,
& mut context ,
InputStream ::empty ( ) ,
2020-09-26 01:40:02 +02:00
Scope ::from_env ( env ) ,
2020-06-28 23:06:05 +02:00
)
. await
{
Ok ( result ) = > match result . collect_string ( Tag ::unknown ( ) ) . await {
Ok ( string_result ) = > {
let errors = context . get_errors ( ) ;
2020-06-27 00:37:31 +02:00
context . maybe_print_errors ( Text ::from ( prompt_line ) ) ;
context . clear_errors ( ) ;
2020-06-28 23:06:05 +02:00
if ! errors . is_empty ( ) {
" > " . to_string ( )
} else {
string_result . item
}
}
Err ( e ) = > {
crate ::cli ::print_err ( e , & Text ::from ( prompt_line ) ) ;
context . clear_errors ( ) ;
" > " . to_string ( )
2020-06-27 00:37:31 +02:00
}
2020-06-28 23:06:05 +02:00
} ,
Err ( e ) = > {
crate ::cli ::print_err ( e , & Text ::from ( prompt_line ) ) ;
context . clear_errors ( ) ;
" > " . to_string ( )
2020-06-27 00:37:31 +02:00
}
}
2020-06-28 23:06:05 +02:00
}
Err ( e ) = > {
crate ::cli ::print_err ( e , & Text ::from ( prompt_line ) ) ;
context . clear_errors ( ) ;
2020-06-27 00:37:31 +02:00
2020-06-28 23:06:05 +02:00
" > " . to_string ( )
2020-06-27 00:37:31 +02:00
}
}
2020-06-25 07:44:55 +02:00
} else {
2020-09-17 08:02:30 +02:00
use crate ::git ::current_branch ;
2019-11-16 21:02:26 +01:00
format! (
2019-11-16 21:42:35 +01:00
" \x1b [32m{}{} \x1b [m> " ,
2019-10-08 15:47:30 +02:00
cwd ,
match current_branch ( ) {
Some ( s ) = > format! ( " ( {} ) " , s ) ,
None = > " " . to_string ( ) ,
}
)
}
} ;
2019-11-16 21:02:26 +01:00
2019-11-16 21:42:35 +01:00
let prompt = {
2020-01-01 21:45:32 +01:00
if let Ok ( bytes ) = strip_ansi_escapes ::strip ( & colored_prompt ) {
String ::from_utf8_lossy ( & bytes ) . to_string ( )
} else {
" > " . to_string ( )
}
2019-11-16 21:42:35 +01:00
} ;
2019-11-16 21:02:26 +01:00
rl . helper_mut ( ) . expect ( " No helper " ) . colored_prompt = colored_prompt ;
2019-09-18 00:21:39 +02:00
let mut initial_command = Some ( String ::new ( ) ) ;
let mut readline = Err ( ReadlineError ::Eof ) ;
while let Some ( ref cmd ) = initial_command {
2019-11-16 21:02:26 +01:00
readline = rl . readline_with_initial ( & prompt , ( & cmd , " " ) ) ;
2019-11-22 09:25:09 +01:00
initial_command = None ;
2019-09-18 00:21:39 +02:00
}
2019-05-23 06:30:43 +02:00
2020-09-17 08:02:30 +02:00
let line = match convert_rustyline_result_to_string ( readline ) {
LineResult ::Success ( s ) = > process_line ( & s , & mut context , false , true ) . await ,
x = > x ,
} ;
2019-11-04 16:47:03 +01:00
2020-01-28 04:13:22 +01:00
// Check the config to see if we need to update the path
// TODO: make sure config is cached so we don't path this load every call
// FIXME: we probably want to be a bit more graceful if we can't set the environment
2020-09-17 01:22:58 +02:00
context . configure ( & configuration , | config , ctx | {
if syncer . did_config_change ( ) {
syncer . reload ( ) ;
syncer . sync_env_vars ( ctx ) ;
syncer . sync_path_vars ( ctx ) ;
}
if let Err ( reason ) = syncer . autoenv ( ctx ) {
print_err ( reason , & Text ::from ( " " ) ) ;
}
let _ = configure_rustyline_editor ( & mut rl , config ) ;
} ) ;
2020-01-28 04:13:22 +01:00
2019-11-04 16:47:03 +01:00
match line {
2019-05-23 06:30:43 +02:00
LineResult ::Success ( line ) = > {
2020-06-15 19:35:24 +02:00
rl . add_history_entry ( & line ) ;
2020-08-27 13:06:25 +02:00
let _ = rl . save_history ( & history_path ) ;
2019-11-04 16:47:03 +01:00
context . maybe_print_errors ( Text ::from ( line ) ) ;
}
LineResult ::Error ( line , err ) = > {
2020-06-15 19:35:24 +02:00
rl . add_history_entry ( & line ) ;
2020-08-27 13:06:25 +02:00
let _ = rl . save_history ( & history_path ) ;
2019-11-04 16:47:03 +01:00
2020-05-18 20:44:27 +02:00
context . with_host ( | _host | {
print_err ( err , & Text ::from ( line . clone ( ) ) ) ;
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
} ) ;
2019-11-04 16:47:03 +01:00
context . maybe_print_errors ( Text ::from ( line . clone ( ) ) ) ;
2019-05-23 06:30:43 +02:00
}
2019-06-15 20:36:17 +02:00
LineResult ::CtrlC = > {
2019-09-25 03:01:38 +02:00
let config_ctrlc_exit = config ::config ( Tag ::unknown ( ) ) ?
. get ( " ctrlc_exit " )
2020-09-15 16:59:51 +02:00
. map ( | s | s . value . is_true ( ) )
2019-09-25 03:01:38 +02:00
. unwrap_or ( false ) ; // default behavior is to allow CTRL-C spamming similar to other shells
if ! config_ctrlc_exit {
continue ;
}
2019-06-15 20:36:17 +02:00
if ctrlcbreak {
2020-08-27 13:06:25 +02:00
let _ = rl . save_history ( & history_path ) ;
2019-06-15 20:36:17 +02:00
std ::process ::exit ( 0 ) ;
} else {
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
context . with_host ( | host | host . stdout ( " CTRL-C pressed (again to quit) " ) ) ;
2019-06-15 20:36:17 +02:00
ctrlcbreak = true ;
continue ;
}
}
2020-09-21 09:56:10 +02:00
LineResult ::CtrlD = > {
context . shell_manager . remove_at_current ( ) ;
if context . shell_manager . is_empty ( ) {
break ;
}
}
2019-05-23 06:30:43 +02:00
LineResult ::Break = > {
break ;
}
}
2019-06-15 20:36:17 +02:00
ctrlcbreak = false ;
2019-05-23 06:30:43 +02:00
}
2019-08-27 00:41:57 +02:00
// we are ok if we can not save history
2020-08-27 13:06:25 +02:00
let _ = rl . save_history ( & history_path ) ;
2019-05-23 06:30:43 +02:00
Ok ( ( ) )
}
2020-09-19 23:29:51 +02:00
pub fn register_plugins ( context : & mut EvaluationContext ) -> Result < ( ) , ShellError > {
2020-09-17 01:22:58 +02:00
if let Ok ( plugins ) = crate ::plugin ::scan ( search_paths ( ) ) {
context . add_commands (
plugins
. into_iter ( )
. filter ( | p | ! context . is_command_registered ( p . name ( ) ) )
. collect ( ) ,
) ;
}
Ok ( ( ) )
}
2020-09-19 23:29:51 +02:00
fn configure_ctrl_c ( _context : & mut EvaluationContext ) -> Result < ( ) , Box < dyn Error > > {
2020-09-17 01:22:58 +02:00
#[ cfg(feature = " ctrlc " ) ]
{
let cc = _context . ctrl_c . clone ( ) ;
ctrlc ::set_handler ( move | | {
cc . store ( true , Ordering ::SeqCst ) ;
} ) ? ;
if _context . ctrl_c . load ( Ordering ::SeqCst ) {
_context . ctrl_c . store ( false , Ordering ::SeqCst ) ;
}
}
Ok ( ( ) )
}
async fn run_startup_commands (
2020-09-19 23:29:51 +02:00
context : & mut EvaluationContext ,
2020-09-17 01:22:58 +02:00
config : & dyn nu_data ::config ::Conf ,
) -> Result < ( ) , ShellError > {
if let Some ( commands ) = config . var ( " startup " ) {
match commands {
Value {
value : UntaggedValue ::Table ( pipelines ) ,
..
} = > {
for pipeline in pipelines {
if let Ok ( pipeline_string ) = pipeline . as_string ( ) {
let _ =
run_pipeline_standalone ( pipeline_string , false , context , false ) . await ;
}
}
}
_ = > {
return Err ( ShellError ::untagged_runtime_error (
" expected a table of pipeline strings as startup commands " ,
) )
}
}
}
Ok ( ( ) )
}
pub async fn run_pipeline_standalone (
pipeline : String ,
redirect_stdin : bool ,
2020-09-19 23:29:51 +02:00
context : & mut EvaluationContext ,
2020-09-17 01:22:58 +02:00
exit_on_error : bool ,
) -> Result < ( ) , Box < dyn Error > > {
2020-09-17 08:02:30 +02:00
let line = process_line ( & pipeline , context , redirect_stdin , false ) . await ;
2020-09-17 01:22:58 +02:00
match line {
LineResult ::Success ( line ) = > {
let error_code = {
let errors = context . current_errors . clone ( ) ;
let errors = errors . lock ( ) ;
if errors . len ( ) > 0 {
1
} else {
0
}
} ;
context . maybe_print_errors ( Text ::from ( line ) ) ;
if error_code ! = 0 & & exit_on_error {
std ::process ::exit ( error_code ) ;
}
}
LineResult ::Error ( line , err ) = > {
context . with_host ( | _host | {
print_err ( err , & Text ::from ( line . clone ( ) ) ) ;
} ) ;
context . maybe_print_errors ( Text ::from ( line ) ) ;
if exit_on_error {
std ::process ::exit ( 1 ) ;
}
}
_ = > { }
}
Ok ( ( ) )
}
2020-09-17 08:02:30 +02:00
#[ cfg(feature = " rustyline-support " ) ]
2020-09-17 01:22:58 +02:00
fn default_rustyline_editor_configuration ( ) -> Editor < Helper > {
#[ cfg(windows) ]
const DEFAULT_COMPLETION_MODE : CompletionType = CompletionType ::Circular ;
#[ cfg(not(windows)) ]
const DEFAULT_COMPLETION_MODE : CompletionType = CompletionType ::List ;
let config = Config ::builder ( ) . color_mode ( ColorMode ::Forced ) . build ( ) ;
let mut rl : Editor < _ > = Editor ::with_config ( config ) ;
// add key bindings to move over a whole word with Ctrl+ArrowLeft and Ctrl+ArrowRight
rl . bind_sequence (
KeyPress ::ControlLeft ,
Cmd ::Move ( Movement ::BackwardWord ( 1 , Word ::Vi ) ) ,
) ;
rl . bind_sequence (
KeyPress ::ControlRight ,
Cmd ::Move ( Movement ::ForwardWord ( 1 , At ::AfterEnd , Word ::Vi ) ) ,
) ;
// Let's set the defaults up front and then override them later if the user indicates
// defaults taken from here https://github.com/kkawakam/rustyline/blob/2fe886c9576c1ea13ca0e5808053ad491a6fe049/src/config.rs#L150-L167
rl . set_max_history_size ( 100 ) ;
rl . set_history_ignore_dups ( true ) ;
rl . set_history_ignore_space ( false ) ;
rl . set_completion_type ( DEFAULT_COMPLETION_MODE ) ;
rl . set_completion_prompt_limit ( 100 ) ;
rl . set_keyseq_timeout ( - 1 ) ;
rl . set_edit_mode ( rustyline ::config ::EditMode ::Emacs ) ;
rl . set_auto_add_history ( false ) ;
rl . set_bell_style ( rustyline ::config ::BellStyle ::default ( ) ) ;
rl . set_color_mode ( rustyline ::ColorMode ::Enabled ) ;
rl . set_tab_stop ( 8 ) ;
if let Err ( e ) = crate ::keybinding ::load_keybindings ( & mut rl ) {
println! ( " Error loading keybindings: {:?} " , e ) ;
}
rl
}
2020-09-17 08:02:30 +02:00
#[ cfg(feature = " rustyline-support " ) ]
2020-09-17 01:22:58 +02:00
fn configure_rustyline_editor (
rl : & mut Editor < Helper > ,
config : & dyn nu_data ::config ::Conf ,
) -> Result < ( ) , ShellError > {
#[ cfg(windows) ]
const DEFAULT_COMPLETION_MODE : CompletionType = CompletionType ::Circular ;
#[ cfg(not(windows)) ]
const DEFAULT_COMPLETION_MODE : CompletionType = CompletionType ::List ;
if let Some ( line_editor_vars ) = config . var ( " line_editor " ) {
for ( idx , value ) in line_editor_vars . row_entries ( ) {
match idx . as_ref ( ) {
" max_history_size " = > {
if let Ok ( max_history_size ) = value . as_u64 ( ) {
rl . set_max_history_size ( max_history_size as usize ) ;
}
}
" history_duplicates " = > {
// history_duplicates = match value.as_string() {
// Ok(s) if s.to_lowercase() == "alwaysadd" => {
// rustyline::config::HistoryDuplicates::AlwaysAdd
// }
// Ok(s) if s.to_lowercase() == "ignoreconsecutive" => {
// rustyline::config::HistoryDuplicates::IgnoreConsecutive
// }
// _ => rustyline::config::HistoryDuplicates::AlwaysAdd,
// };
if let Ok ( history_duplicates ) = value . as_bool ( ) {
rl . set_history_ignore_dups ( history_duplicates ) ;
}
}
" history_ignore_space " = > {
if let Ok ( history_ignore_space ) = value . as_bool ( ) {
rl . set_history_ignore_space ( history_ignore_space ) ;
}
}
" completion_type " = > {
let completion_type = match value . as_string ( ) {
Ok ( s ) if s . to_lowercase ( ) = = " circular " = > {
rustyline ::config ::CompletionType ::Circular
}
Ok ( s ) if s . to_lowercase ( ) = = " list " = > {
rustyline ::config ::CompletionType ::List
}
#[ cfg(all(unix, feature = " with-fuzzy " )) ]
Ok ( s ) if s . to_lowercase ( ) = = " fuzzy " = > {
rustyline ::config ::CompletionType ::Fuzzy
}
_ = > DEFAULT_COMPLETION_MODE ,
} ;
rl . set_completion_type ( completion_type ) ;
}
" completion_prompt_limit " = > {
if let Ok ( completion_prompt_limit ) = value . as_u64 ( ) {
rl . set_completion_prompt_limit ( completion_prompt_limit as usize ) ;
}
}
" keyseq_timeout_ms " = > {
if let Ok ( keyseq_timeout_ms ) = value . as_u64 ( ) {
rl . set_keyseq_timeout ( keyseq_timeout_ms as i32 ) ;
}
}
" edit_mode " = > {
let edit_mode = match value . as_string ( ) {
Ok ( s ) if s . to_lowercase ( ) = = " vi " = > rustyline ::config ::EditMode ::Vi ,
Ok ( s ) if s . to_lowercase ( ) = = " emacs " = > rustyline ::config ::EditMode ::Emacs ,
_ = > rustyline ::config ::EditMode ::Emacs ,
} ;
rl . set_edit_mode ( edit_mode ) ;
// Note: When edit_mode is Emacs, the keyseq_timeout_ms is set to -1
// no matter what you may have configured. This is so that key chords
// can be applied without having to do them in a given timeout. So,
// it essentially turns off the keyseq timeout.
}
" auto_add_history " = > {
if let Ok ( auto_add_history ) = value . as_bool ( ) {
rl . set_auto_add_history ( auto_add_history ) ;
}
}
" bell_style " = > {
let bell_style = match value . as_string ( ) {
Ok ( s ) if s . to_lowercase ( ) = = " audible " = > {
rustyline ::config ::BellStyle ::Audible
}
Ok ( s ) if s . to_lowercase ( ) = = " none " = > rustyline ::config ::BellStyle ::None ,
Ok ( s ) if s . to_lowercase ( ) = = " visible " = > {
rustyline ::config ::BellStyle ::Visible
}
_ = > rustyline ::config ::BellStyle ::default ( ) ,
} ;
rl . set_bell_style ( bell_style ) ;
}
" color_mode " = > {
let color_mode = match value . as_string ( ) {
Ok ( s ) if s . to_lowercase ( ) = = " enabled " = > rustyline ::ColorMode ::Enabled ,
Ok ( s ) if s . to_lowercase ( ) = = " forced " = > rustyline ::ColorMode ::Forced ,
Ok ( s ) if s . to_lowercase ( ) = = " disabled " = > rustyline ::ColorMode ::Disabled ,
_ = > rustyline ::ColorMode ::Enabled ,
} ;
rl . set_color_mode ( color_mode ) ;
}
" tab_stop " = > {
if let Ok ( tab_stop ) = value . as_u64 ( ) {
rl . set_tab_stop ( tab_stop as usize ) ;
}
}
_ = > ( ) ,
}
}
}
Ok ( ( ) )
}
2020-09-17 08:02:30 +02:00
#[ cfg(feature = " rustyline-support " ) ]
2020-09-17 01:22:58 +02:00
fn nu_line_editor_helper (
2020-09-19 23:29:51 +02:00
context : & mut EvaluationContext ,
2020-09-17 01:22:58 +02:00
config : & dyn nu_data ::config ::Conf ,
) -> crate ::shell ::Helper {
let hinter = rustyline_hinter ( config ) ;
crate ::shell ::Helper ::new ( context . clone ( ) , hinter )
}
2020-09-17 08:02:30 +02:00
#[ cfg(feature = " rustyline-support " ) ]
2020-09-17 01:22:58 +02:00
fn rustyline_hinter ( config : & dyn nu_data ::config ::Conf ) -> Option < rustyline ::hint ::HistoryHinter > {
if let Some ( line_editor_vars ) = config . var ( " line_editor " ) {
2020-08-27 07:45:55 +02:00
for ( idx , value ) in line_editor_vars . row_entries ( ) {
if idx = = " show_hints " & & value . expect_string ( ) = = " false " {
return None ;
}
}
}
2020-09-17 01:22:58 +02:00
2020-08-27 07:45:55 +02:00
Some ( rustyline ::hint ::HistoryHinter { } )
}
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-18 00:26:27 +02:00
fn chomp_newline ( s : & str ) -> & str {
if s . ends_with ( '\n' ) {
& s [ .. s . len ( ) - 1 ]
} else {
s
}
}
2020-07-18 03:59:23 +02:00
#[ derive(Debug) ]
2020-06-20 05:41:53 +02:00
pub enum LineResult {
2019-05-23 06:30:43 +02:00
Success ( String ) ,
2019-06-08 00:35:07 +02:00
Error ( String , ShellError ) ,
2019-05-23 06:30:43 +02:00
Break ,
2020-09-21 09:56:10 +02:00
CtrlC ,
CtrlD ,
2019-05-23 06:30:43 +02:00
}
2020-09-19 23:29:51 +02:00
pub async fn parse_and_eval ( line : & str , ctx : & mut EvaluationContext ) -> Result < String , ShellError > {
2020-07-18 03:59:23 +02:00
let line = if line . ends_with ( '\n' ) {
& line [ .. line . len ( ) - 1 ]
} else {
line
} ;
let lite_result = nu_parser ::lite_parse ( & line , 0 ) ? ;
// TODO ensure the command whose examples we're testing is actually in the pipeline
let mut classified_block = nu_parser ::classify_block ( & lite_result , ctx . registry ( ) ) ;
classified_block . block . expand_it_usage ( ) ;
let input_stream = InputStream ::empty ( ) ;
let env = ctx . get_env ( ) ;
run_block (
& classified_block . block ,
ctx ,
input_stream ,
2020-09-26 01:40:02 +02:00
Scope ::from_env ( env ) ,
2020-07-18 03:59:23 +02:00
)
. await ?
. collect_string ( Tag ::unknown ( ) )
. await
. map ( | x | x . item )
}
2020-01-14 08:38:56 +01:00
/// Process the line by parsing the text to turn it into commands, classify those commands so that we understand what is being called in the pipeline, and then run this pipeline
2020-06-20 05:41:53 +02:00
pub async fn process_line (
2020-09-17 08:02:30 +02:00
line : & str ,
2020-09-19 23:29:51 +02:00
ctx : & mut EvaluationContext ,
2020-02-09 03:24:33 +01:00
redirect_stdin : bool ,
2020-03-17 19:13:38 +01:00
cli_mode : bool ,
2020-02-09 03:24:33 +01:00
) -> LineResult {
2020-09-17 08:02:30 +02:00
if line . trim ( ) = = " " {
LineResult ::Success ( line . to_string ( ) )
} else {
let line = chomp_newline ( line ) ;
ctx . raw_input = line . to_string ( ) ;
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-18 00:26:27 +02:00
2020-09-17 08:02:30 +02:00
let result = match nu_parser ::lite_parse ( & line , 0 ) {
Err ( err ) = > {
return LineResult ::Error ( line . to_string ( ) , err . into ( ) ) ;
}
2019-05-23 06:30:43 +02:00
2020-09-17 08:02:30 +02:00
Ok ( val ) = > val ,
} ;
2019-05-23 06:30:43 +02:00
2020-09-17 08:02:30 +02:00
debug! ( " === Parsed === " ) ;
debug! ( " {:#?} " , result ) ;
2019-05-24 09:29:16 +02:00
2020-09-17 08:02:30 +02:00
let mut classified_block = nu_parser ::classify_block ( & result , ctx . registry ( ) ) ;
2020-04-06 09:16:14 +02:00
2020-09-17 08:02:30 +02:00
debug! ( " {:#?} " , classified_block ) ;
//println!("{:#?}", pipeline);
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
2020-09-17 08:02:30 +02:00
if let Some ( failure ) = classified_block . failed {
return LineResult ::Error ( line . to_string ( ) , failure . into ( ) ) ;
}
Restructure and streamline token expansion (#1123)
Restructure and streamline token expansion
The purpose of this commit is to streamline the token expansion code, by
removing aspects of the code that are no longer relevant, removing
pointless duplication, and eliminating the need to pass the same
arguments to `expand_syntax`.
The first big-picture change in this commit is that instead of a handful
of `expand_` functions, which take a TokensIterator and ExpandContext, a
smaller number of methods on the `TokensIterator` do the same job.
The second big-picture change in this commit is fully eliminating the
coloring traits, making coloring a responsibility of the base expansion
implementations. This also means that the coloring tracer is merged into
the expansion tracer, so you can follow a single expansion and see how
the expansion process produced colored tokens.
One side effect of this change is that the expander itself is marginally
more error-correcting. The error correction works by switching from
structured expansion to `BackoffColoringMode` when an unexpected token
is found, which guarantees that all spans of the source are colored, but
may not be the most optimal error recovery strategy.
That said, because `BackoffColoringMode` only extends as far as a
closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in
fairly granular correction strategy.
The current code still produces an `Err` (plus a complete list of
colored shapes) from the parsing process if any errors are encountered,
but this could easily be addressed now that the underlying expansion is
error-correcting.
This commit also colors any spans that are syntax errors in red, and
causes the parser to include some additional information about what
tokens were expected at any given point where an error was encountered,
so that completions and hinting could be more robust in the future.
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
2020-09-17 08:02:30 +02:00
// There's a special case to check before we process the pipeline:
// If we're giving a path by itself
// ...and it's not a command in the path
// ...and it doesn't have any arguments
// ...and we're in the CLI
// ...then change to this directory
if cli_mode
& & classified_block . block . block . len ( ) = = 1
& & classified_block . block . block [ 0 ] . list . len ( ) = = 1
{
if let ClassifiedCommand ::Internal ( InternalCommand {
ref name , ref args , ..
} ) = classified_block . block . block [ 0 ] . list [ 0 ]
2020-04-20 08:41:51 +02:00
{
2020-09-17 08:02:30 +02:00
let internal_name = name ;
let name = args
. positional
. as_ref ( )
. and_then ( | potionals | {
potionals . get ( 0 ) . map ( | e | {
if let Expression ::Literal ( Literal ::String ( ref s ) ) = e . expr {
& s
} else {
" "
}
} )
} )
. unwrap_or ( " " ) ;
if internal_name = = " run_external "
& & args
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
. positional
. as_ref ( )
2020-09-17 08:02:30 +02:00
. map ( | ref v | v . len ( ) = = 1 )
. unwrap_or ( true )
& & args
. named
. as_ref ( )
. map ( NamedArguments ::is_empty )
. unwrap_or ( true )
& & canonicalize ( ctx . shell_manager . path ( ) , name ) . is_ok ( )
& & Path ::new ( & name ) . is_dir ( )
& & ! crate ::commands ::classified ::external ::did_find_command ( & name )
{
// Here we work differently if we're in Windows because of the expected Windows behavior
#[ cfg(windows) ]
2020-03-17 19:13:38 +01:00
{
2020-09-17 08:02:30 +02:00
if name . ends_with ( ':' ) {
// This looks like a drive shortcut. We need to a) switch drives and b) go back to the previous directory we were viewing on that drive
// But first, we need to save where we are now
let current_path = ctx . shell_manager . path ( ) ;
let split_path : Vec < _ > = current_path . split ( ':' ) . collect ( ) ;
if split_path . len ( ) > 1 {
ctx . windows_drives_previous_cwd
. lock ( )
. insert ( split_path [ 0 ] . to_string ( ) , current_path ) ;
}
2020-03-18 03:10:45 +01:00
2020-09-17 08:02:30 +02:00
let name = name . to_uppercase ( ) ;
let new_drive : Vec < _ > = name . split ( ':' ) . collect ( ) ;
if let Some ( val ) =
ctx . windows_drives_previous_cwd . lock ( ) . get ( new_drive [ 0 ] )
{
ctx . shell_manager . set_path ( val . to_string ( ) ) ;
return LineResult ::Success ( line . to_string ( ) ) ;
2020-03-18 03:10:45 +01:00
} else {
2020-09-17 08:02:30 +02:00
ctx . shell_manager
. set_path ( format! ( " {} \\ " , name . to_string ( ) ) ) ;
2020-03-18 03:10:45 +01:00
return LineResult ::Success ( line . to_string ( ) ) ;
}
2020-09-17 08:02:30 +02:00
} else {
2020-03-18 03:10:45 +01:00
ctx . shell_manager . set_path ( name . to_string ( ) ) ;
return LineResult ::Success ( line . to_string ( ) ) ;
}
2020-03-17 19:13:38 +01:00
}
2020-09-17 08:02:30 +02:00
#[ cfg(not(windows)) ]
{
ctx . shell_manager . set_path ( name . to_string ( ) ) ;
return LineResult ::Success ( line . to_string ( ) ) ;
}
2020-03-17 19:13:38 +01:00
}
}
2020-09-17 08:02:30 +02:00
}
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
2020-09-17 08:02:30 +02:00
let input_stream = if redirect_stdin {
let file = futures ::io ::AllowStdIo ::new ( std ::io ::stdin ( ) ) ;
let stream = FramedRead ::new ( file , MaybeTextCodec ::default ( ) ) . map ( | line | {
if let Ok ( line ) = line {
let primitive = match line {
StringOrBinary ::String ( s ) = > Primitive ::String ( s ) ,
StringOrBinary ::Binary ( b ) = > Primitive ::Binary ( b . into_iter ( ) . collect ( ) ) ,
2020-02-09 03:24:33 +01:00
} ;
2019-05-24 09:29:16 +02:00
2020-09-17 08:02:30 +02:00
Ok ( Value {
value : UntaggedValue ::Primitive ( primitive ) ,
tag : Tag ::unknown ( ) ,
} )
} else {
panic! ( " Internal error: could not read lines of text from stdin " )
}
} ) ;
stream . to_input_stream ( )
} else {
InputStream ::empty ( )
} ;
classified_block . block . expand_it_usage ( ) ;
trace! ( " {:#?} " , classified_block ) ;
let env = ctx . get_env ( ) ;
match run_block (
& classified_block . block ,
ctx ,
input_stream ,
2020-09-26 01:40:02 +02:00
Scope ::from_env ( env ) ,
2020-09-17 08:02:30 +02:00
)
. await
{
Ok ( input ) = > {
// Running a pipeline gives us back a stream that we can then
// work through. At the top level, we just want to pull on the
// values to compute them.
use futures ::stream ::TryStreamExt ;
let context = RunnableContext {
input ,
shell_manager : ctx . shell_manager . clone ( ) ,
host : ctx . host . clone ( ) ,
ctrl_c : ctx . ctrl_c . clone ( ) ,
current_errors : ctx . current_errors . clone ( ) ,
registry : ctx . registry . clone ( ) ,
name : Tag ::unknown ( ) ,
raw_input : line . to_string ( ) ,
} ;
if let Ok ( mut output_stream ) =
crate ::commands ::autoview ::command ::autoview ( context ) . await
{
loop {
match output_stream . try_next ( ) . await {
Ok ( Some ( ReturnSuccess ::Value ( Value {
value : UntaggedValue ::Error ( e ) ,
..
} ) ) ) = > return LineResult ::Error ( line . to_string ( ) , e ) ,
Ok ( Some ( _item ) ) = > {
if ctx . ctrl_c . load ( Ordering ::SeqCst ) {
break ;
2020-02-09 03:24:33 +01:00
}
}
2020-09-17 08:02:30 +02:00
Ok ( None ) = > break ,
Err ( e ) = > return LineResult ::Error ( line . to_string ( ) , e ) ,
2020-02-09 03:24:33 +01:00
}
}
}
2020-09-17 08:02:30 +02:00
LineResult ::Success ( line . to_string ( ) )
2019-05-23 06:30:43 +02:00
}
2020-09-17 08:02:30 +02:00
Err ( err ) = > LineResult ::Error ( line . to_string ( ) , err ) ,
2019-05-23 06:30:43 +02:00
}
}
}
2020-05-18 20:44:27 +02:00
pub fn print_err ( err : ShellError , source : & Text ) {
2020-04-21 05:14:18 +02:00
if let Some ( diag ) = err . into_diagnostic ( ) {
2020-05-18 20:44:27 +02:00
let source = source . to_string ( ) ;
let mut files = codespan_reporting ::files ::SimpleFiles ::new ( ) ;
files . add ( " shell " , source ) ;
let writer = codespan_reporting ::term ::termcolor ::StandardStream ::stderr (
codespan_reporting ::term ::termcolor ::ColorChoice ::Always ,
) ;
let config = codespan_reporting ::term ::Config ::default ( ) ;
2020-04-21 05:14:18 +02:00
let _ = std ::panic ::catch_unwind ( move | | {
2020-05-18 20:44:27 +02:00
let _ = codespan_reporting ::term ::emit ( & mut writer . lock ( ) , & config , & files , & diag ) ;
2020-04-21 05:14:18 +02:00
} ) ;
}
2019-08-16 00:18:18 +02:00
}
2020-04-11 21:05:59 +02:00
#[ cfg(test) ]
mod tests {
#[ quickcheck ]
fn quickcheck_parse ( data : String ) -> bool {
2020-04-20 08:41:51 +02:00
if let Ok ( lite_block ) = nu_parser ::lite_parse ( & data , 0 ) {
2020-09-19 23:29:51 +02:00
let context = crate ::evaluation_context ::EvaluationContext ::basic ( ) . unwrap ( ) ;
2020-04-20 08:41:51 +02:00
let _ = nu_parser ::classify_block ( & lite_block , context . registry ( ) ) ;
2020-04-11 21:05:59 +02:00
}
2020-04-20 08:41:51 +02:00
true
2020-04-11 21:05:59 +02:00
}
}