Could fish support string manipulating? #924

Closed
leafonsword opened this Issue Jul 22, 2013 · 24 comments

Comments

Projects
None yet
8 participants
@leafonsword
Contributor

leafonsword commented Jul 22, 2013

Bash and zsh both support string manipulating,could fish also support?
I wish fish's string manipulating could be more friendly than bash/zsh~

@xfix

This comment has been minimized.

Show comment
Hide comment
@xfix

xfix Jul 22, 2013

Member

I support this (this could be for example used to easily fix #906), but there is issue of syntax. Something like $variable[2] wouldn't work, because in fish every variable is an array (IMO, it's a design flaw, but not something that could be easily fixed).

But perhaps we don't need accessing single characters. In Perl, there is no way to access single characters (because of similar syntax issues), aside of using substr. Perhaps something like substr $variable 2 1 would work for reading, but that still has problems with writing (perhaps something like set variable (substr $variable 2 1 cat) would work). The syntax I propose is from Perl (well, mostly, in Perl substr is lvalue (in Perl you can do substr($variable, 1, 1) = 'cat'), but this is simply impossible in fish).

Member

xfix commented Jul 22, 2013

I support this (this could be for example used to easily fix #906), but there is issue of syntax. Something like $variable[2] wouldn't work, because in fish every variable is an array (IMO, it's a design flaw, but not something that could be easily fixed).

But perhaps we don't need accessing single characters. In Perl, there is no way to access single characters (because of similar syntax issues), aside of using substr. Perhaps something like substr $variable 2 1 would work for reading, but that still has problems with writing (perhaps something like set variable (substr $variable 2 1 cat) would work). The syntax I propose is from Perl (well, mostly, in Perl substr is lvalue (in Perl you can do substr($variable, 1, 1) = 'cat'), but this is simply impossible in fish).

@sethillgard

This comment has been minimized.

Show comment
Hide comment
@sethillgard

sethillgard Jul 22, 2013

I know its not standard, but why not some other symbol, like double brackets:

$variable[[2]]

I know its not standard, but why not some other symbol, like double brackets:

$variable[[2]]

@xfix

This comment has been minimized.

Show comment
Hide comment
@xfix

xfix Jul 23, 2013

Member

@sethillgard: I guess it could work, but it's ugly IMO. But I think I like this syntax (it's better than alternatives).

But perhaps something like $variable{2} from PHP would work... Or not, there is issue with that and set.

Member

xfix commented Jul 23, 2013

@sethillgard: I guess it could work, but it's ugly IMO. But I think I like this syntax (it's better than alternatives).

But perhaps something like $variable{2} from PHP would work... Or not, there is issue with that and set.

@sethillgard

This comment has been minimized.

Show comment
Hide comment
@sethillgard

sethillgard Jul 23, 2013

@glitchmr Yeah, people have a strong association of {} with sets and () with function calls. Breaking that would be bad.

@glitchmr Yeah, people have a strong association of {} with sets and () with function calls. Breaking that would be bad.

@xfix

This comment has been minimized.

Show comment
Hide comment
@xfix

xfix Jul 23, 2013

Member

@sethillgard: I was talking about set, as in set variable{2}. With current fish grammar it would mean set variable2. Sets aren't the issue, as it's accessing syntax, and PHP uses {2} syntax anyway for strings.

Member

xfix commented Jul 23, 2013

@sethillgard: I was talking about set, as in set variable{2}. With current fish grammar it would mean set variable2. Sets aren't the issue, as it's accessing syntax, and PHP uses {2} syntax anyway for strings.

@zanchey

This comment has been minimized.

Show comment
Hide comment
@zanchey

zanchey Jul 25, 2013

Member

see also #156

Member

zanchey commented Jul 25, 2013

see also #156

@X4

This comment has been minimized.

Show comment
Hide comment
@X4

X4 Sep 12, 2013

I second @sethillgard there needs to be much more thought put into developing the fish-language. Personally I think the language should be dropped completely and replaced by API-Binding wrappers to language "X". In example: Bash/ZSH, Elixir Lua or something that cool and easy to learn.

In short: Give other languages access to fish's internal state, variables, function and settings and drop the fish language.

X4 commented Sep 12, 2013

I second @sethillgard there needs to be much more thought put into developing the fish-language. Personally I think the language should be dropped completely and replaced by API-Binding wrappers to language "X". In example: Bash/ZSH, Elixir Lua or something that cool and easy to learn.

In short: Give other languages access to fish's internal state, variables, function and settings and drop the fish language.

@ridiculousfish

This comment has been minimized.

Show comment
Hide comment
@ridiculousfish

ridiculousfish Sep 12, 2013

Member

@X4, are you proposing that you would run commands on the command line by writing Lua code snippets? Or do you mean that there would be one language for running commands interactively, and another for scripts?

Member

ridiculousfish commented Sep 12, 2013

@X4, are you proposing that you would run commands on the command line by writing Lua code snippets? Or do you mean that there would be one language for running commands interactively, and another for scripts?

@X4

This comment has been minimized.

Show comment
Hide comment
@X4

X4 Sep 20, 2013

@ridiculousfish sorry for the late response. The first.
Well, if you could use Lua, Ruby, Elixir, Python or any other high-level language to modify the command-line by accessing fish's internal state, then it would save you a lot of work, reduce the codebase and make it more powerful at the same time. No need for the fish language then and you would have perfect string-manipulation as a bonus.

X4 commented Sep 20, 2013

@ridiculousfish sorry for the late response. The first.
Well, if you could use Lua, Ruby, Elixir, Python or any other high-level language to modify the command-line by accessing fish's internal state, then it would save you a lot of work, reduce the codebase and make it more powerful at the same time. No need for the fish language then and you would have perfect string-manipulation as a bonus.

@xfix

This comment has been minimized.

Show comment
Hide comment
@xfix

xfix Sep 21, 2013

Member

By the way, expr provides great external string parsing.

$ expr substr + string 2 3
tri
$ expr length + 'fish shell'
10
Member

xfix commented Sep 21, 2013

By the way, expr provides great external string parsing.

$ expr substr + string 2 3
tri
$ expr length + 'fish shell'
10
@ridiculousfish

This comment has been minimized.

Show comment
Hide comment
@ridiculousfish

ridiculousfish Sep 21, 2013

Member

Wow, I've never heard of expr. Neat!

Member

ridiculousfish commented Sep 21, 2013

Wow, I've never heard of expr. Neat!

@X4

This comment has been minimized.

Show comment
Hide comment

X4 commented Sep 23, 2013

Thanks @glitchmr !

@newhook

This comment has been minimized.

Show comment
Hide comment
@newhook

newhook Feb 12, 2015

@X4/@ridiculousfish have you given any more thought to the idea of replacing the fish language with something more real? What I don't see is how this works correctly on the command line? After all "ls" isn't a valid piece of lua. What did you have in mind?

newhook commented Feb 12, 2015

@X4/@ridiculousfish have you given any more thought to the idea of replacing the fish language with something more real? What I don't see is how this works correctly on the command line? After all "ls" isn't a valid piece of lua. What did you have in mind?

@ridiculousfish

This comment has been minimized.

Show comment
Hide comment
@ridiculousfish

ridiculousfish Feb 12, 2015

Member

I'm keeping an open mind :)

Member

ridiculousfish commented Feb 12, 2015

I'm keeping an open mind :)

@newhook

This comment has been minimized.

Show comment
Hide comment
@newhook

newhook Feb 12, 2015

One idea could be to keep the fish language as is, and then allow native language functions:

pyfunction blah
// python code goes here
end

All of the fish variables would be made available as python defined variables. Lists would be converted to python lists and so forth.

newhook commented Feb 12, 2015

One idea could be to keep the fish language as is, and then allow native language functions:

pyfunction blah
// python code goes here
end

All of the fish variables would be made available as python defined variables. Lists would be converted to python lists and so forth.

@newhook

This comment has been minimized.

Show comment
Hide comment
@newhook

newhook Feb 13, 2015

Upon reflection that approach doesn't work all that well. I have a better idea which I'm trying out to integrate python scripting into fish.

newhook commented Feb 13, 2015

Upon reflection that approach doesn't work all that well. I have a better idea which I'm trying out to integrate python scripting into fish.

@newhook

This comment has been minimized.

Show comment
Hide comment
@newhook

newhook Feb 16, 2015

Ok, so I tested out my idea and it works. The basic idea is to write a python extension for fish. The current implementation exposes a fish module in python. It has the following functions:

# Add a new function with the given name.
fish.add(name, function)
# Get a fish variable.
fish.get(variable)
# Split a string variable into a list (used for fish arrays).
fish.split(string)
# set -g b
fish.set("a", "-g", "b")
# Evaluate a command. For example, fish.call('cd /')
fish.call(string)

I added a fish_python_path (like fish_function_path). All python files in this path are evaluated. For example:

// test.py
import fish

dirstack = []
def pushd():
   argv  = fish.split(fish.get('argv'))
   pwd = fish.get('PWD')
   dirstack.append(pwd)
   fish.cmd("cd %s" % (argv[0]))

fish.add('pushd', pushd)

Python files are reloaded upon change, much like the autoloaded fish functions. To this point the change hasn't made all that many changes to the fish internals. Most of the changes involve function.cpp to add a new python function, and exec.cpp to call a python function rather than a fish function.

Any thoughts? Concerns? Comments?

newhook commented Feb 16, 2015

Ok, so I tested out my idea and it works. The basic idea is to write a python extension for fish. The current implementation exposes a fish module in python. It has the following functions:

# Add a new function with the given name.
fish.add(name, function)
# Get a fish variable.
fish.get(variable)
# Split a string variable into a list (used for fish arrays).
fish.split(string)
# set -g b
fish.set("a", "-g", "b")
# Evaluate a command. For example, fish.call('cd /')
fish.call(string)

I added a fish_python_path (like fish_function_path). All python files in this path are evaluated. For example:

// test.py
import fish

dirstack = []
def pushd():
   argv  = fish.split(fish.get('argv'))
   pwd = fish.get('PWD')
   dirstack.append(pwd)
   fish.cmd("cd %s" % (argv[0]))

fish.add('pushd', pushd)

Python files are reloaded upon change, much like the autoloaded fish functions. To this point the change hasn't made all that many changes to the fish internals. Most of the changes involve function.cpp to add a new python function, and exec.cpp to call a python function rather than a fish function.

Any thoughts? Concerns? Comments?

@ridiculousfish

This comment has been minimized.

Show comment
Hide comment
@ridiculousfish

ridiculousfish Feb 17, 2015

Member

(I reformatted your comment)

Member

ridiculousfish commented Feb 17, 2015

(I reformatted your comment)

@ridiculousfish

This comment has been minimized.

Show comment
Hide comment
@ridiculousfish

ridiculousfish Feb 17, 2015

Member

Is the idea that fish would embed a Python interpreter?

Member

ridiculousfish commented Feb 17, 2015

Is the idea that fish would embed a Python interpreter?

@newhook

This comment has been minimized.

Show comment
Hide comment
@newhook

newhook Feb 17, 2015

Yes. I guess it could be optional, although it strikes me that making it mandatory would provide a more uniform and standard environment. The change I made added entries to configure.ac to detect the python installation and configure the includes and libraries appropriately.

newhook commented Feb 17, 2015

Yes. I guess it could be optional, although it strikes me that making it mandatory would provide a more uniform and standard environment. The change I made added entries to configure.ac to detect the python installation and configure the includes and libraries appropriately.

@X4

This comment has been minimized.

Show comment
Hide comment
@X4

X4 Mar 3, 2015

What I meant by exposing the internal state of fish was to expose an API similar to Web API's that are often times accessible through Resful URIs. I was thinking that fish could've been implemented using a FSM or PEG, which means that you can either expose a stack oriented API DSL for the FSM, or in the case of a PEG just provide a language DSL1 that resembles a Restful API with URIs giving acces to fish functions, history etc.

That way you save a lot of time exposing fish functions via modules and are not bound to existing language bindings. Well if you're just lazy and have no lust to use a PEGs (in case you've not used it in the fish code) why not integrate a simple httpd and expose the fish API programatically? You'd save time exposing each new function and people would have a shell with built in Web API.

  1. factor programming language

X4 commented Mar 3, 2015

What I meant by exposing the internal state of fish was to expose an API similar to Web API's that are often times accessible through Resful URIs. I was thinking that fish could've been implemented using a FSM or PEG, which means that you can either expose a stack oriented API DSL for the FSM, or in the case of a PEG just provide a language DSL1 that resembles a Restful API with URIs giving acces to fish functions, history etc.

That way you save a lot of time exposing fish functions via modules and are not bound to existing language bindings. Well if you're just lazy and have no lust to use a PEGs (in case you've not used it in the fish code) why not integrate a simple httpd and expose the fish API programatically? You'd save time exposing each new function and people would have a shell with built in Web API.

  1. factor programming language
@ridiculousfish

This comment has been minimized.

Show comment
Hide comment
@ridiculousfish

ridiculousfish Mar 3, 2015

Member

@X4 Sorry, I still don't understand what you're proposing. Today, I might write this on the command line:

ls *.txt

This is parsed and executed according to the fish language. If we dropped the fish language and replaced it with Python or Lua or WebAPI or whatever, how would the user run that command? What would the user type?

Member

ridiculousfish commented Mar 3, 2015

@X4 Sorry, I still don't understand what you're proposing. Today, I might write this on the command line:

ls *.txt

This is parsed and executed according to the fish language. If we dropped the fish language and replaced it with Python or Lua or WebAPI or whatever, how would the user run that command? What would the user type?

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Mar 25, 2015

This and #156 are actually duplicates, I think.

ghost commented Mar 25, 2015

This and #156 are actually duplicates, I think.

@ridiculousfish

This comment has been minimized.

Show comment
Hide comment
@ridiculousfish

ridiculousfish Mar 25, 2015

Member

Good call, let's track in #156

Member

ridiculousfish commented Mar 25, 2015

Good call, let's track in #156

@faho faho added the duplicate label Dec 13, 2015

@faho faho removed this from the fish-future milestone Dec 13, 2015

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