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