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            StrExpand,
223            StrJoin,
224            StrReplace,
225            StrIndexOf,
226            StrLength,
227            StrReverse,
228            StrStats,
229            StrStartsWith,
230            StrSubstring,
231            StrTrim,
232            StrUpcase,
233            Format,
234            FormatDate,
235            FormatDuration,
236            FormatFilesize,
237        };
238
239        // FileSystem
240        #[cfg(feature = "os")]
241        bind_command! {
242            Cd,
243            Ls,
244            UMkdir,
245            Mktemp,
246            UMv,
247            UCp,
248            Open,
249            Start,
250            Rm,
251            Save,
252            UTouch,
253            Glob,
254            Watch,
255        };
256
257        // Platform
258        #[cfg(all(feature = "os", not(target_arch = "wasm32")))]
259        if nu_experimental::NATIVE_CLIP.get() {
260            bind_command! {
261                ClipCommand,
262                ClipCopy,
263                ClipPaste,
264            };
265        }
266
267        #[cfg(feature = "os")]
268        bind_command! {
269            Clear,
270            Du,
271            Input,
272            InputList,
273            InputListen,
274            IsTerminal,
275            Kill,
276            Sleep,
277            Term,
278            TermSize,
279            TermQuery,
280            Whoami,
281        };
282
283        #[cfg(all(unix, feature = "os"))]
284        bind_command! { ULimit };
285
286        #[cfg(all(unix, feature = "os"))]
287        bind_command! { UMask };
288
289        // Date
290        bind_command! {
291            Date,
292            DateFromHuman,
293            DateHumanize,
294            DateListTimezones,
295            DateNow,
296            DateToTimezone,
297        };
298
299        // Shells
300        bind_command! {
301            Exit,
302        };
303
304        // Formats
305        bind_command! {
306            From,
307            FromCsv,
308            FromJson,
309            FromMsgpack,
310            FromMsgpackz,
311            FromNuon,
312            FromOds,
313            FromSsv,
314            FromToml,
315            FromTsv,
316            FromXlsx,
317            FromXml,
318            FromYaml,
319            FromYml,
320            To,
321            ToCsv,
322            ToJson,
323            ToMd,
324            ToMsgpack,
325            ToMsgpackz,
326            ToNuon,
327            ToText,
328            ToToml,
329            ToTsv,
330            Upsert,
331            Where,
332            ToXml,
333            ToYaml,
334            ToYml,
335        };
336
337        // Viewers
338        bind_command! {
339            Griddle,
340            Table,
341        };
342
343        // Conversions
344        bind_command! {
345            Fill,
346            Into,
347            IntoBool,
348            IntoBinary,
349            IntoCellPath,
350            IntoDatetime,
351            IntoDuration,
352            IntoFloat,
353            IntoFilesize,
354            IntoInt,
355            IntoRecord,
356            IntoString,
357            IntoGlob,
358            IntoValue,
359            SplitCellPath,
360        };
361
362        // Env
363        bind_command! {
364            ExportEnv,
365            LoadEnv,
366            SourceEnv,
367            WithEnv,
368            ConfigNu,
369            ConfigEnv,
370            ConfigFlatten,
371            ConfigMeta,
372            ConfigReset,
373            ConfigUseColors,
374        };
375
376        // Math
377        bind_command! {
378            Math,
379            MathAbs,
380            MathAvg,
381            MathCeil,
382            MathFloor,
383            MathMax,
384            MathMedian,
385            MathMin,
386            MathMode,
387            MathProduct,
388            MathRound,
389            MathSqrt,
390            MathStddev,
391            MathSum,
392            MathVariance,
393            MathLog,
394        };
395
396        // Bytes
397        bind_command! {
398            Bytes,
399            BytesLen,
400            BytesSplit,
401            BytesStartsWith,
402            BytesEndsWith,
403            BytesReverse,
404            BytesReplace,
405            BytesAdd,
406            BytesAt,
407            BytesIndexOf,
408            BytesCollect,
409            BytesRemove,
410            BytesBuild
411        }
412
413        // Network
414        #[cfg(feature = "network")]
415        bind_command! {
416            Http,
417            HttpDelete,
418            HttpGet,
419            HttpHead,
420            HttpPatch,
421            HttpPost,
422            HttpPut,
423            HttpOptions,
424            HttpPool,
425            Port,
426            VersionCheck,
427        }
428        bind_command! {
429            Url,
430            UrlBuildQuery,
431            UrlSplitQuery,
432            UrlDecode,
433            UrlEncode,
434            UrlJoin,
435            UrlParse,
436        }
437
438        // Random
439        #[cfg(feature = "rand")]
440        bind_command! {
441            Random,
442            RandomBool,
443            RandomChars,
444            RandomDice,
445            RandomFloat,
446            RandomInt,
447            RandomUuid,
448            RandomBinary
449        };
450
451        // Generators
452        bind_command! {
453            Cal,
454            Seq,
455            SeqDate,
456            SeqChar,
457            Generate,
458        };
459
460        // Hash
461        bind_command! {
462            Hash,
463            HashMd5::default(),
464            HashSha256::default(),
465        };
466
467        // Experimental
468        bind_command! {
469            IsAdmin,
470            JobSpawn,
471            JobList,
472            JobKill,
473            JobId,
474            JobTag,
475            Job,
476        };
477
478        #[cfg(not(target_family = "wasm"))]
479        bind_command! {
480            JobSend,
481            JobRecv,
482            JobFlush,
483        }
484
485        #[cfg(all(unix, feature = "os"))]
486        bind_command! {
487            JobUnfreeze,
488        }
489
490        // Removed
491        bind_command! {
492            LetEnv,
493            DateFormat,
494        };
495
496        // Stor
497        #[cfg(feature = "sqlite")]
498        bind_command! {
499            Stor,
500            StorCreate,
501            StorDelete,
502            StorExport,
503            StorImport,
504            StorInsert,
505            StorOpen,
506            StorReset,
507            StorUpdate,
508        };
509
510        working_set.render()
511    };
512
513    if let Err(err) = engine_state.merge_delta(delta) {
514        eprintln!("Error creating default context: {err:?}");
515    }
516
517    // Cache the table decl id so we don't have to look it up later
518    let table_decl_id = engine_state.find_decl("table".as_bytes(), &[]);
519    engine_state.table_decl_id = table_decl_id;
520
521    engine_state
522}