4 ms·
Have you seen nushell? It lets me one-liner so many things that would have previously taken breaking out a "real" language to do Contrived example: ls | whe
by jakkos 9mo ago
Have you seen nushell? It lets me one-liner so many things that would have previously taken breaking out a "real" language to do
Contrived example:
ls | where type == 'file' | sort-by size | take 4 | each {|f| {n: $f.name, s: ($f.size | format filesize MB) }} | to json
outputs
{
"n": "clippy.toml",
"s": "0.000001 MB"
},
{
"n": "README.md",
"s": "0.000009 MB"
},
{
"n": "rustfmt.toml",
"s": "0.000052 MB"
},
{
"n": "typos.toml",
"s": "0.00009 MB"
}
- fainpul 9mo agoIn PowerShell: gci -file | sort-object size | select name, size -first 4 | % { $_.size /= 1MB; $_ } | ConvertTo-Json
- NSPG911 9mo agoi dont think you need to select name and size, you can just remove it and use `select -first 4`, but cool, I never knew about `/=` syntax
- fainpul 9mo agoI was trying to replicate the nushell example, which only has name and size in the output.
- xigoi 9mo agoConvertTo-Json? What kind of naming convention is that, especially with all the other commands being lowercase?
- fainpul 9mo agoThe naming convention is `verb-noun`. It's convenient for discoverability and consistency. The short commands are aliases. The last command is properly cased, because I pressed tab (it auto-completes and fixes the case). The other commands I typed without tab completion. You can write however you want, PS is not case sensitive.
- 0x3444ac53 9mo agoI've tried nushell and other shell replacements and it just feels like I'm learning a new programming language for no good reason
- fainpul 9mo agoOk if it's not for you. But there is of course a very good reason — work with objects in the pipeline instead of "dumb text". Also PowerShell and nushell are quite nice to learn, whereas Bash is absolutely horrible.
- kelnos 9mo agoI wonder if this is a case of "worse is better", or just the long-term entrenchment of text. Because nushell hasn't been adopted all that much compared to bash or even zsh (or a "real" scripting language like python or ruby). I don't know much about PowerShell adoption (haven't used Windows in over 20 years), but I'd assume since it's a first-party system that's default installed(?), it's done better adoption-wise. I agree that bash sucks, but I really have no motivation to learn something like nushell. I can get by with bash for simpler things, and when I get frustrated with bash, I switch to python, which is default-available everywhere I personally need it to be. Back to text, though... I'm honestly not sure objects are strictly better than dumb text. Objects means higher cognitive overhead; text is... well, text. You can see it right there in front of you, count lines and characters, see its delimiters and structure, and come up with code to manipulate it. And, again, if I need objects, I have python.
- fainpul 9mo agoI get the point of "either Bash or straight to a real programming language". That's what I do too, for automation. I like how PowerShell makes one-off tasks easier which would otherwise be the typical pipe of cat, grep, sed, awk etc. About objects vs. text: I'm convinced that objects are vastly superior. There was a comment about this here with good arguments: https://news.ycombinator.com/item?id=45907248 https://news.ycombinator.com/item?id=45907248
- 9mo ago
- BoppreH 9mo agoFor me the best benefit of nushell is not the easier syntax, but the static type checks. It catches most typos before running the script, which is a godsend when the script is slow and/or has destructive operations.
- ifh-hn 9mo agoWas just about to suggest nushell. I love programming in nushell, the out of the box features are excellent.
- vidarh 9mo agoWith this: E = Struct.new(:name, :size, :type) def ls = Dir.children('.').map{ s=File::Stat.new(_1); E.new(_1, s.size, s.file? ? 'file' : 'dir') } This becomes valid Ruby: ls.find_all{_1.type == 'file'}.sort_by(&:size).take(4).map{ {n: _1.name, s: _1.size } }.each { puts JSON.pretty_generate(_1) } (drops your size formatting, so not strictly equivalent) Which isn't meant to "compete" - nushell looks nice -, but to show that the lower-threshold option for those of us who don't want to switch shells is to throw together a few helpers in a language... (you can get much closer to your example with another helper or two and a few more "evil" abuses of Ruby's darker corners, but I'm not sure it'd be worth it; I might a wrapper for the above in my bin/ though)
- deleted 9mo ago[deleted]
- deleted 9mo ago[deleted]
- dev_l1x_be 9mo agothis is pretty cool!