Index range expansion#212
Index range expansion#212ridiculousfish merged 8 commits intofish-shell:masterfrom maxfl:index_range
Conversation
Builtin 'set' now can set variable index ranges: set test[1..3] a b c #works set test[-1..-3] a b c #works if variable have enough elements set test[2..-2] a b c #works set test[1..3 -1..-2] a b c b b #works Expand now can parse index ranges. But not handle for now. TODO: * Add variable substitution index ranges: echo $PATH[-1..1] * Add command substitution index range: echo (seq 10)[-1..-4] * Add process substitution indexes and ranges: echo %vim[-1]
Now the following code works: > echo (seq 10)[-1..1] With output: 10 9 8 7 6 5 4 3 2 1
echo $PATH[-1..1] #now works Add tests for ranges
echo $PATH[1..$n]
|
And what doesn't work: |
|
This looks incredible. I'm blown away. What happens if the start and end indices are the same, e.g. |
|
Right. I've tried to do everything with a minimal changes and tried not to introduce excessive checks. |
|
There's been no other commentary on this and I very much want it for an upcoming release, so I've merged this via To anyone, please feel free to share more feedback! To git@github.com:fish-shell/fish-shell.git |
|
Nice. Thank you. On Fri, 20 Jul 2012 01:59:38 +0800, ridiculousfish
|
I've added possibility to expand index ranges for variable expansion, command substitution, and set builtin.
This pull request is not intended to be merged in a short term (at least until I update the documentation).
I just want to show it and to get some feedback.
Here is what works (see tests/test8.in for examples):
Range is defined as following: i1..i2, where i1, and i2 are two non-zero integers. They may be negative.
This functruction is expanded to the sequence of integers between i1 and i2. It respects the direction:
1..5 -> 1 2 3 4 5
5..1 -> 5 4 3 2 1
It knows about the array/output size and respects negative numbers. Consider the variable with 5 elements:
3..-1 -> 3 4 5
-1..-3 -> 5 4 3
-1..1 -> 5 4 3 2 1 # this is useful for inverting variables and output.
Of course this can be used only in [] brackets. Several ranges can be specified:
Overlapping ranges can be specified:
echo (seq 10)[1..5 1..10]Set command also respects ranges:
set var[6..10] (seq 5)Limits can be variables or command substitution:
Any feedback is appreciated.
I had also idea of making possible to apply indexes to the process substitution, so I can write:
kill %gvim[-1]
But I'm not sure if it worth doing for now. Any thoughts on this?