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