Skip to main content

shannon_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            FromMsgpack,
311            FromMsgpackz,
312            FromNuon,
313            FromOds,
314            FromSsv,
315            FromToml,
316            FromTsv,
317            FromXlsx,
318            FromXml,
319            FROM_YAML,
320            FROM_YML,
321            To,
322            ToCsv,
323            ToJson,
324            ToMd,
325            ToMsgpack,
326            ToMsgpackz,
327            ToNuon,
328            ToText,
329            ToToml,
330            ToTsv,
331            Upsert,
332            Where,
333            ToXml,
334            TO_YAML,
335            TO_YML,
336        };
337
338        // Viewers
339        bind_command! {
340            Griddle,
341            Table,
342        };
343
344        // Conversions
345        bind_command! {
346            Fill,
347            Into,
348            IntoBool,
349            IntoBinary,
350            IntoCellPath,
351            IntoDatetime,
352            IntoDuration,
353            IntoFloat,
354            IntoFilesize,
355            IntoInt,
356            IntoRecord,
357            IntoString,
358            IntoGlob,
359            IntoValue,
360            SplitCellPath,
361        };
362
363        // Env
364        bind_command! {
365            ExportEnv,
366            LoadEnv,
367            SourceEnv,
368            WithEnv,
369            ConfigNu,
370            ConfigEnv,
371            ConfigFlatten,
372            ConfigMeta,
373            ConfigReset,
374            ConfigUseColors,
375        };
376
377        // Math
378        bind_command! {
379            Math,
380            MathAbs,
381            MathAvg,
382            MathCeil,
383            MathFloor,
384            MathMax,
385            MathMedian,
386            MathMin,
387            MathMode,
388            MathProduct,
389            MathRound,
390            MathSqrt,
391            MathStddev,
392            MathSum,
393            MathVariance,
394            MathLog,
395        };
396
397        // Bytes
398        bind_command! {
399            Bytes,
400            BytesLen,
401            BytesSplit,
402            BytesStartsWith,
403            BytesEndsWith,
404            BytesReverse,
405            BytesReplace,
406            BytesAdd,
407            BytesAt,
408            BytesIndexOf,
409            BytesCollect,
410            BytesRemove,
411            BytesBuild
412        }
413
414        // Network
415        #[cfg(feature = "network")]
416        bind_command! {
417            Http,
418            HttpDelete,
419            HttpGet,
420            HttpHead,
421            HttpPatch,
422            HttpPost,
423            HttpPut,
424            HttpOptions,
425            HttpPool,
426            Port,
427            VersionCheck,
428        }
429        bind_command! {
430            Url,
431            UrlBuildQuery,
432            UrlSplitQuery,
433            UrlDecode,
434            UrlEncode,
435            UrlJoin,
436            UrlParse,
437        }
438
439        // Random
440        #[cfg(feature = "rand")]
441        bind_command! {
442            Random,
443            RandomBool,
444            RandomChars,
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            JobDescribe,
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}