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