3 ms·
I love the structured pipes, but as mentioned in another discussion, replacing the shell is a big leap for a lot of people. And you can get quite far without it
by vidarh 9y ago
I love the structured pipes, but as mentioned in another discussion, replacing the shell is a big leap for a lot of people. And you can get quite far without it with tools like "jq". And when I saw this, I just had to tinker a bit to see what I could do with Ruby, based on the example on the homepage:
$ curl -s https://api.github.com/repos/elves/elvish/issues |
jr 'each{|issue| puts "#{issue["number"]}: #{issue["title"]}"} ' | head -n 11
Or (I expect pitchforks when you see the implementation for this):
$ curl -s https://api.github.com/repos/elves/elvish/issues |
jr 'puts{"#{$I["number"]}: #{$I["title"]}"}' | head -n 11
With "jr" looking like this[1]. Expect this to fall apart in all kinds of ways - it was just something I threw together. Not convinced the monkey-patched "puts" is worth it, but it's just an experiment..
[1] https://gist.github.com/vidarh/d088d6e342e665f0b6554ec4fbaa5e53 https://gist.github.com/vidarh/d088d6e342e665f0b6554ec4fbaa5...
- yAnonymous 9y agoI use JSON, because it's simple yet powerful. Command line JSON tools seem to have a very convoluted syntax and that kinda defeats the whole purpose.
- vidarh 9y agoI don't think there's a whole lot more we can achieve beyond what you see there... I mean, I guess you could dynamically assign global variables based on the keys, and enforce treating them as strings, so you could reduce it to jr 'puts{$number+": "+$title}' E.g by changing the "each" in my example with: def each █ $J.each do |i| $I = i; i.keys.each {|k| eval("$#{k} = $I[k].to_s")} block.call(i) end end Though this is error prone - e.g. if the key collides with any pre-defined Ruby globals like $stdin... Would probably be worth using a prefix etc.. This is the downside to using a general purpose language for stuff like this instead of a specialised tool like jq which can ignore other syntax concerns..