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