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