logoalt Hacker News

jakkoslast Wednesday at 1:36 PM7 repliesview on HN

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"
  }

Replies

vidarhlast Wednesday at 6:01 PM

With 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)

show 1 reply
0x3444ac53last Wednesday at 2:23 PM

I've tried nushell and other shell replacements and it just feels like I'm learning a new programming language for no good reason

show 4 replies
fainpullast Wednesday at 2:22 PM

In PowerShell:

  gci -file | sort-object size | select name, size -first 4 | % { $_.size /= 1MB; $_ } | ConvertTo-Json
show 2 replies
BoppreHlast Wednesday at 4:42 PM

For 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-hnlast Wednesday at 5:54 PM

Was just about to suggest nushell. I love programming in nushell, the out of the box features are excellent.

dev_l1x_belast Thursday at 7:38 AM

this is pretty cool!