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