8000 Index range expansion by maxfl · Pull Request #212 · fish-shell/fish-shell · GitHub
[go: up one dir, main page]

Skip to content

Index range expansion#212

Merged
ridiculousfish merged 8 commits intofish-shell:masterfrom
maxfl:index_range
Jul 19, 2012
Merged

Index range expansion#212
ridiculousfish merged 8 commits intofish-shell:masterfrom
maxfl:index_range

Conversation

@maxfl
Copy link
Contributor
@maxfl maxfl commented Jul 8, 2012

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:

 echo $PATH[1..2 3..-1]

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:

 echo $PATH[1..(seq 3)]
 set -n 3
 echo $PATH[1..$3]

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?

maxfl added 8 commits July 7, 2012 10:17
Fish now doesn't join the fish_prompt output. This breaks the default
fish_prompt. Make default fish_prompt single-line. Fixes #203.

Add -l flag to 'read' documentation. Remove ambiguous '-x' description.
Fixes #157.
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
@maxfl
Copy link
Contributor Author
maxfl commented Jul 8, 2012

And what doesn't work:
echo $PATH[1..{2,3}] doesn't work because '{}' is not expanded inside '[]'
echo (seq 5)[(seq 5)] because '()' is also not expanded inside '[]'

@ridiculousfish
Copy link
Member

This looks incredible. I'm blown away.

What happens if the start and end indices are the same, e.g. echo $PATH[2..2]? If the bounds are inclusive, then I guess it is the same as echo $PATH[2]

@maxfl
Copy link
Contributor Author
maxfl commented Jul 8, 2012

Right. I've tried to do everything with a minimal changes and tried not to introduce excessive checks.
So 2..2 is just 2.
If you put 0 there, it will also expand: 0..4 -> 0 1 2 3 4, but it will issue out of bounds error then.

@ridiculousfish ridiculousfish merged commit b25b5bf into fish-shell:master Jul 19, 2012
@ridiculousfish
Copy link
Member

There's been no other commentary on this and I very much want it for an upcoming release, so I've merged this via git merge -s recursive -Xignore-space-change

To anyone, please feel free to share more feedback!

To git@github.com:fish-shell/fish-shell.git
7039e01..bb4a050 master -> master

@maxfl
Copy link
Contributor Author
maxfl commented Jul 20, 2012

Nice. Thank you.
I'm busy now and will update documentation in 1-2 weeks.

On Fri, 20 Jul 2012 01:59:38 +0800, ridiculousfish
reply@reply.github.com
wrote:

There's been no other commentary on this and I very much want it for an
upcoming release, so I've merged this via git merge -s recursive -Xignore-space-change

To anyone, please feel free to share more feedback!

To git@github.com:fish-shell/fish-shell.git
7039e01..bb4a050 master -> master


Reply to this email directly or view it on GitHub:
#212 (comment)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0