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