Skip to main content

nu_command/
default_context.rs

1use crate::*;
2use nu_protocol::engine::{EngineState, StateWorkingSet};
3
4pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
5    let delta = {
6        let mut working_set = StateWorkingSet::new(&engine_state);
7
8        macro_rules! bind_command {
9            ( $( $command:expr ),* $(,)? ) => {
10                $( working_set.add_decl(Box::new($command)); )*
11            };
12        }
13
14        // If there are commands that have the same name as default declarations,
15        // they have to be registered before the main declarations. This helps to make
16        // them only accessible if the correct input value category is used with the
17        // declaration
18
19        // Database-related
20        // Adds all related commands to query databases
21        #[cfg(feature = "sqlite")]
22        add_database_decls(&mut working_set);
23
24        // Charts
25        bind_command! {
26            Histogram
27        }
28
29        // Filters
30        #[cfg(feature = "rand")]
31        bind_command! {
32            Shuffle
33        }
34        bind_command! {
35            All,
36            Any,
37            Append,
38            Chunks,
39            Columns,
40            Combinations,
41            Compact,
42            Default,
43            Difference,
44            Drop,
45            DropColumn,
46            DropNth,
47            Each,
48            Enumerate,
49            Every,
50            Filter,
51            Find,
52            First,
53            Flatten,
54            Get,
55            GroupBy,
56            Headers,
57            Insert,
58            IsEmpty,
59            IsNotEmpty,
60            Interleave,
61            Intersect,
62            Items,
63            Join,
64            Take,
65            Merge,
66            MergeDeep,
67            Move,
68            TakeWhile,
69            TakeUntil,
70            Last,
71            Length,
72            Lines,
73            ParEach,
74            Peek,
75            Permutations,
76            ChunkBy,
77            Prepend,
78            Reduce,
79            Reject,
80            Rename,
81            Reverse,
82            Select,
83            Skip,
84            SkipUntil,
85            SkipWhile,
86            Slice,
87            Sort,
88            SortBy,
89            SplitList,
90            Tee,
91            Transpose,
92            Uniq,
93            UniqBy,
94            Union,
95            Upsert,
96            Update,
97            Values,
98            Where,
99            Window,
100            Wrap,
101            Zip,
102        };
103
104        // Misc
105        bind_command! {
106            DeleteVar,
107            Panic,
108            Run,
109            Source,
110            Tutor,
111        };
112
113        // Path
114        bind_command! {
115            Path,
116            PathBasename,
117            PathSelf,
118            PathDirname,
119            PathExists,
120            PathExpand,
121            PathJoin,
122            PathParse,
123            PathRelativeTo,
124            PathSplit,
125            PathType,
126        };
127
128        // System
129        #[cfg(feature = "os")]
130        bind_command! {
131            Complete,
132            External,
133            Exec,
134            NuCheck,
135            RunInternal,
136            Sys,
137            SysCpu,
138            SysDisks,
139            SysHost,
140            SysMem,
141            SysNet,
142            SysTemp,
143            SysUsers,
144            UName,
145            Which,
146        };
147
148        // Help
149        bind_command! {
150            Help,
151            HelpAliases,
152            HelpExterns,
153            HelpCommands,
154            HelpModules,
155            HelpOperators,
156            HelpPipeAndRedirect,
157            HelpEscapes,
158        };
159
160        // Debug
161        bind_command! {
162            Ast,
163            Debug,
164            DebugEnv,
165            DebugExperimentalOptions,
166            DebugInfo,
167            DebugProfile,
168            Explain,
169            Inspect,
170            Metadata,
171            MetadataAccess,
172            MetadataSet,
173            TimeIt,
174            View,
175            ViewBlocks,
176            ViewFiles,
177            ViewIr,
178            ViewSource,
179            ViewSpan,
180        };
181
182        #[cfg(all(feature = "os", windows))]
183        bind_command! { Registry, RegistryQuery }
184
185        #[cfg(all(
186            feature = "os",
187            any(
188                target_os = "android",
189                target_os = "linux",
190                target_os = "freebsd",
191                target_os = "netbsd",
192                target_os = "openbsd",
193                target_os = "macos",
194                target_os = "windows"
195            )
196        ))]
197        bind_command! { Ps };
198
199        // Strings
200        bind_command! {
201            Ansi,
202            AnsiLink,
203            AnsiStrip,
204            Char,
205            Decode,
206            Encode,
207            DecodeHex,
208            EncodeHex,
209            DecodeBase32,
210            EncodeBase32,
211            DecodeBase32Hex,
212            EncodeBase32Hex,
213            DecodeBase64,
214            EncodeBase64,
215            Detect,
216            DetectColumns,
217            DetectType,
218            Parse,
219            Split,
220            SplitChars,
221            SplitColumn,
222            SplitRow,
223            SplitWords,
224            Str,
225            StrCapitalize,
226            StrContains,
227            StrDistance,
228            StrDowncase,
229            StrLowercase,
230            StrEndswith,
231            StrEscapeRegex,
232            StrExpand,
233            StrJoin,
234            StrReplace,
235            StrIndexOf,
236            StrLength,
237            StrReverse,
238            StrStats,
239            StrStartsWith,
240            StrSubstring,
241            StrTrim,
242            StrUpcase,
243            StrUppercase,
244            Format,
245            FormatDate,
246            FormatDuration,
247            FormatFilesize,
248        };
249
250        // FileSystem
251        #[cfg(feature = "os")]
252        bind_command! {
253            Cd,
254            Ls,
255            UMkdir,
256            Mktemp,
257            UMv,
258            UCp,
259            Open,
260            Start,
261            Rm,
262            Save,
263            UTouch,
264            Glob,
265            Watch,
266            Idx,
267            IdxInit,
268            IdxStatus,
269            IdxFind,
270            IdxSearch,
271            IdxDrop,
272            IdxDirs,
273            IdxFiles,
274        };
275
276        #[cfg(all(feature = "os", feature = "sqlite"))]
277        bind_command! {
278            IdxExport,
279            IdxImport,
280        };
281
282        // Platform
283        #[cfg(all(feature = "os", not(target_arch = "wasm32")))]
284        if nu_experimental::NATIVE_CLIP.get() {
285            bind_command! {
286                ClipCommand,
287                ClipCopy,
288                ClipPaste,
289            };
290        }
291
292        #[cfg(feature = "os")]
293        bind_command! {
294            Clear,
295            Du,
296            Input,
297            InputList,
298            InputListen,
299            IsTerminal,
300            Kill,
301            Sleep,
302            Term,
303            TermSize,
304            TermQuery,
305            Whoami,
306        };
307
308        #[cfg(all(unix, feature = "os"))]
309        bind_command! { ULimit };
310
311        #[cfg(all(unix, feature = "os"))]
312        bind_command! { UMask };
313
314        // Date
315        bind_command! {
316            Date,
317            DateFromHuman,
318            DateHumanize,
319            DateListTimezones,
320            DateNow,
321            DateToTimezone,
322        };
323
324        // Shells
325        bind_command! {
326            Exit,
327        };
328
329        // Formats
330        bind_command! {
331            From,
332            FromCsv,
333            FromJson,
334            FromMd,
335            FromMsgpack,
336            FromMsgpackz,
337            FromNuon,
338            FromOds,
339            FromSsv,
340            FromToml,
341            FromTsv,
342            FromXlsx,
343            FromXml,
344            FROM_YAML,
345            FROM_YML,
346            FromKdl,
347            To,
348            ToCsv,
349            ToJson,
350            ToMd,
351            ToMsgpack,
352            ToMsgpackz,
353            ToNuon,
354            ToText,
355            ToToml,
356            ToTsv,
357            ToKdl,
358            Upsert,
359            Where,
360            ToXml,
361            TO_YAML,
362            TO_YML,
363        };
364
365        // Viewers
366        bind_command! {
367            Griddle,
368            Table,
369        };
370
371        // Conversions
372        bind_command! {
373            Fill,
374            Into,
375            IntoBool,
376            IntoBinary,
377            IntoCellPath,
378            IntoDatetime,
379            IntoDuration,
380            IntoFloat,
381            IntoFilesize,
382            IntoInt,
383            IntoRecord,
384            IntoSemver,
385            IntoSemverRange,
386            IntoString,
387            IntoGlob,
388            IntoValue,
389            SplitCellPath,
390        };
391
392        // Semver
393        bind_command! {
394            Semver,
395            SemverBump,
396        };
397
398        // Env
399        bind_command! {
400            ExportEnv,
401            LoadEnv,
402            SourceEnv,
403            WithEnv,
404            ConfigNu,
405            ConfigEnv,
406            ConfigFlatten,
407            ConfigMeta,
408            ConfigReset,
409            ConfigUseColors,
410        };
411
412        // Math
413        bind_command! {
414            Math,
415            MathAbs,
416            MathAvg,
417            MathCbrt,
418            MathCeil,
419            MathFloor,
420            MathMax,
421            MathMedian,
422            MathMin,
423            MathMode,
424            MathProduct,
425            MathRound,
426            MathSqrt,
427            MathStddev,
428            MathSum,
429            MathVariance,
430            MathLog,
431        };
432
433        // Bytes
434        bind_command! {
435            Bytes,
436            BytesLen,
437            BytesSplit,
438            BytesStartsWith,
439            BytesEndsWith,
440            BytesReverse,
441            BytesReplace,
442            BytesAdd,
443            BytesAt,
444            BytesIndexOf,
445            BytesCollect,
446            BytesRemove,
447            BytesBuild
448        }
449
450        // Network
451        #[cfg(feature = "network")]
452        bind_command! {
453            Http,
454            HttpDelete,
455            HttpGet,
456            HttpHead,
457            HttpPatch,
458            HttpPost,
459            HttpPut,
460            HttpOptions,
461            HttpPool,
462            Port,
463            VersionCheck,
464        }
465        bind_command! {
466            Url,
467            UrlBuildQuery,
468            UrlSplitQuery,
469            UrlDecode,
470            UrlEncode,
471            UrlJoin,
472            UrlParse,
473        }
474
475        // Random
476        #[cfg(feature = "rand")]
477        bind_command! {
478            Random,
479            RandomBool,
480            RandomChars,
481            RandomFloat,
482            RandomInt,
483            RandomPass,
484            RandomUuid,
485            RandomBinary
486        };
487
488        // Generators
489        bind_command! {
490            Cal,
491            Seq,
492            SeqDate,
493            SeqChar,
494            Generate,
495        };
496
497        // Hash
498        bind_command! {
499            Hash,
500            HashMd5::default(),
501            HashSha256::default(),
502        };
503
504        // Experimental
505        bind_command! {
506            IsAdmin,
507            JobSpawn,
508            JobList,
509            JobKill,
510            JobId,
511            JobDescribe,
512            Job,
513        };
514
515        #[cfg(not(target_family = "wasm"))]
516        bind_command! {
517            JobSend,
518            JobRecv,
519            JobFlush,
520        }
521
522        #[cfg(all(unix, feature = "os"))]
523        bind_command! {
524            JobUnfreeze,
525        }
526
527        // Removed
528        bind_command! {
529            LetEnv,
530            DateFormat,
531        };
532
533        // Stor
534        #[cfg(feature = "sqlite")]
535        bind_command! {
536            Stor,
537            StorCreate,
538            StorDelete,
539            StorExport,
540            StorImport,
541            StorInsert,
542            StorOpen,
543            StorReset,
544            StorUpdate,
545        };
546
547        working_set.render()
548    };
549
550    if let Err(err) = engine_state.merge_delta(delta) {
551        eprintln!("Error creating default context: {err:?}");
552    }
553
554    // Cache the table decl id so we don't have to look it up later
555    let table_decl_id = engine_state.find_decl("table".as_bytes(), &[]);
556    engine_state.table_decl_id = table_decl_id;
557
558    engine_state
559}