New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index range expansion #212

Merged
merged 8 commits into from Jul 19, 2012

Conversation

Projects
None yet
3 participants
@maxfl
Contributor

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 some commits Jul 7, 2012

Set single-line default prompt
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.
Add index ranges
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]
Implemented index ranges for command substitution
Now the following code works:
> echo (seq 10)[-1..1]
With output:
10
9
8
7
6
5
4
3
2
1
Add variable expand ranges
echo $PATH[-1..1] #now works

Add tests for ranges
@maxfl

This comment has been minimized.

Show comment
Hide comment
@maxfl

maxfl Jul 8, 2012

Contributor

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 '[]'

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@ridiculousfish

ridiculousfish Jul 8, 2012

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]

Member

ridiculousfish commented Jul 8, 2012

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

This comment has been minimized.

Show comment
Hide comment
@maxfl

maxfl Jul 8, 2012

Contributor

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.

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@ridiculousfish

ridiculousfish Jul 19, 2012

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

Member

ridiculousfish commented Jul 19, 2012

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

This comment has been minimized.

Show comment
Hide comment
@maxfl

maxfl Jul 20, 2012

Contributor

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)

Contributor

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment