nushell on your web browser
nushell wasm terminal
at main 8.0 kB view raw
1use nu_command::*; 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 // add_database_decls(&mut working_set); 22 23 // Charts 24 bind_command! { 25 Histogram 26 } 27 28 // Filters 29 bind_command! { 30 All, 31 Any, 32 Append, 33 Chunks, 34 Columns, 35 Compact, 36 Default, 37 Drop, 38 DropColumn, 39 DropNth, 40 Each, 41 Enumerate, 42 Every, 43 Filter, 44 Find, 45 First, 46 Flatten, 47 Get, 48 GroupBy, 49 Headers, 50 Insert, 51 IsEmpty, 52 IsNotEmpty, 53 Interleave, 54 Items, 55 Join, 56 Take, 57 Merge, 58 MergeDeep, 59 Move, 60 TakeWhile, 61 TakeUntil, 62 Last, 63 Length, 64 Lines, 65 ParEach, 66 ChunkBy, 67 Prepend, 68 Reduce, 69 Reject, 70 Rename, 71 Reverse, 72 Select, 73 Skip, 74 SkipUntil, 75 SkipWhile, 76 Slice, 77 Sort, 78 SortBy, 79 SplitList, 80 Tee, 81 Transpose, 82 Uniq, 83 UniqBy, 84 Upsert, 85 Update, 86 Values, 87 Where, 88 Window, 89 Wrap, 90 Zip, 91 }; 92 93 // Misc 94 bind_command! { 95 Panic, 96 }; 97 98 // Path 99 bind_command! { 100 Path, 101 PathBasename, 102 PathSelf, 103 PathDirname, 104 PathExists, 105 PathExpand, 106 PathJoin, 107 PathParse, 108 PathRelativeTo, 109 PathSplit, 110 PathType, 111 }; 112 113 // Help 114 bind_command! { 115 Help, 116 HelpAliases, 117 HelpExterns, 118 HelpCommands, 119 HelpModules, 120 HelpOperators, 121 HelpPipeAndRedirect, 122 HelpEscapes, 123 }; 124 125 // Debug 126 bind_command! { 127 Ast, 128 Debug, 129 DebugEnv, 130 DebugExperimentalOptions, 131 DebugInfo, 132 DebugProfile, 133 Explain, 134 Inspect, 135 Metadata, 136 MetadataAccess, 137 MetadataSet, 138 TimeIt, 139 View, 140 ViewBlocks, 141 ViewFiles, 142 ViewIr, 143 ViewSource, 144 ViewSpan, 145 }; 146 147 // Strings 148 bind_command! { 149 Ansi, 150 AnsiLink, 151 AnsiStrip, 152 Char, 153 Decode, 154 Encode, 155 DecodeHex, 156 EncodeHex, 157 DecodeBase32, 158 EncodeBase32, 159 DecodeBase32Hex, 160 EncodeBase32Hex, 161 DecodeBase64, 162 EncodeBase64, 163 Detect, 164 DetectColumns, 165 DetectType, 166 Parse, 167 Split, 168 SplitChars, 169 SplitColumn, 170 SplitRow, 171 SplitWords, 172 Str, 173 StrCapitalize, 174 StrContains, 175 StrDistance, 176 StrDowncase, 177 StrEndswith, 178 StrExpand, 179 StrJoin, 180 StrReplace, 181 StrIndexOf, 182 StrLength, 183 StrReverse, 184 StrStats, 185 StrStartsWith, 186 StrSubstring, 187 StrTrim, 188 StrUpcase, 189 Format, 190 FormatDate, 191 FormatDuration, 192 FormatFilesize, 193 }; 194 195 // Date 196 bind_command! { 197 Date, 198 DateFromHuman, 199 DateHumanize, 200 DateListTimezones, 201 DateNow, 202 DateToTimezone, 203 }; 204 205 // Formats 206 bind_command! { 207 From, 208 FromCsv, 209 FromJson, 210 FromMsgpack, 211 FromMsgpackz, 212 FromNuon, 213 FromOds, 214 FromSsv, 215 FromToml, 216 FromTsv, 217 FromXlsx, 218 FromXml, 219 FromYaml, 220 FromYml, 221 To, 222 ToCsv, 223 ToJson, 224 ToMd, 225 ToMsgpack, 226 ToMsgpackz, 227 ToNuon, 228 ToText, 229 ToToml, 230 ToTsv, 231 Upsert, 232 Where, 233 ToXml, 234 ToYaml, 235 ToYml, 236 }; 237 238 // Viewers 239 bind_command! { 240 Griddle, 241 Table, 242 }; 243 244 // Conversions 245 bind_command! { 246 Fill, 247 Into, 248 IntoBool, 249 IntoBinary, 250 IntoCellPath, 251 IntoDatetime, 252 IntoDuration, 253 IntoFloat, 254 IntoFilesize, 255 IntoInt, 256 IntoRecord, 257 IntoString, 258 IntoGlob, 259 IntoValue, 260 SplitCellPath, 261 }; 262 263 // Env 264 bind_command! { 265 ExportEnv, 266 LoadEnv, 267 // SourceEnv, 268 WithEnv, 269 ConfigNu, 270 ConfigEnv, 271 ConfigFlatten, 272 ConfigMeta, 273 ConfigReset, 274 ConfigUseColors, 275 }; 276 277 // Math 278 bind_command! { 279 Math, 280 MathAbs, 281 MathAvg, 282 MathCeil, 283 MathFloor, 284 MathMax, 285 MathMedian, 286 MathMin, 287 MathMode, 288 MathProduct, 289 MathRound, 290 MathSqrt, 291 MathStddev, 292 MathSum, 293 MathVariance, 294 MathLog, 295 }; 296 297 // Bytes 298 bind_command! { 299 Bytes, 300 BytesLen, 301 BytesSplit, 302 BytesStartsWith, 303 BytesEndsWith, 304 BytesReverse, 305 BytesReplace, 306 BytesAdd, 307 BytesAt, 308 BytesIndexOf, 309 BytesCollect, 310 BytesRemove, 311 BytesBuild 312 } 313 314 bind_command! { 315 Url, 316 UrlBuildQuery, 317 UrlSplitQuery, 318 UrlDecode, 319 UrlEncode, 320 UrlJoin, 321 UrlParse, 322 } 323 324 // Generators 325 bind_command! { 326 Cal, 327 Seq, 328 SeqDate, 329 SeqChar, 330 Generate, 331 }; 332 333 // Hash 334 bind_command! { 335 Hash, 336 HashMd5::default(), 337 HashSha256::default(), 338 }; 339 340 // Removed 341 bind_command! { 342 LetEnv, 343 DateFormat, 344 }; 345 346 // bind_command! { 347 // Stor, 348 // StorCreate, 349 // StorDelete, 350 // StorExport, 351 // StorImport, 352 // StorInsert, 353 // StorOpen, 354 // StorReset, 355 // StorUpdate, 356 // }; 357 358 working_set.render() 359 }; 360 361 if let Err(err) = engine_state.merge_delta(delta) { 362 eprintln!("Error creating default context: {err:?}"); 363 } 364 365 // Cache the table decl id so we don't have to look it up later 366 let table_decl_id = engine_state.find_decl("table".as_bytes(), &[]); 367 engine_state.table_decl_id = table_decl_id; 368 369 engine_state 370}