Skip to content
This repository has been archived by the owner on Feb 22, 2022. It is now read-only.

Filter enhancements, syntax suggestions #26

Open
DaveBenham opened this issue Dec 29, 2019 · 5 comments
Open

Filter enhancements, syntax suggestions #26

DaveBenham opened this issue Dec 29, 2019 · 5 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@DaveBenham
Copy link
Collaborator

DaveBenham commented Dec 29, 2019

Some suggestions:

  • Always use single quotes whenever a string of characters is required. Escape single quote literal as '', For example !undefined;'This is Dave''s idea'!
  • Ditch the 0 padding construct and allow specification of any pad string for both left and right alignment. Default to spaces for padding. So !var;05! would now be written as !var;5'0'.
    A little more verbose, but now you can do something like
    set "TOC=Chapter 1" & echo !TOC;-30' .'! yielding Chapter 1. . . . . . . . . . .
  • Consider abbreviating filter function names: length -> len, upper -> up, lower -> low, capital -> cap. I can't think of anything for trim, so trim, ltrim, and rtrim would stay the same. Using the 'string' syntax, !var;trim[xy]! would become !var;trim'xy'!.
  • Implement case sensitive find/replace as !var;'search'='replace'!. In addition to providing case sensitive support, it would allow searching for = and * . Additional related extensions to consider
    • !var;'search'i='replace'! - case insensitive search, still useful because allows = and *'
    • !var;'search'3='replace'! - only replace the 3rd occurrence
    • !var;'search'-1='replace'! - only replace the last occurrence
    • An occurrence value of 0 would be the default, meaning replace all occurrences
    • i and occurrence value can be combined
    • !var;*'search'='replace'! - replace everything from beginning through 1st occurrence of search. !var;*'search'-1='replace'! would replace from beginning through last occurrence, etc. No occurrence, occurrence of 0, and occurrence of 1 would all be equivalent.
    • Put * after 'search' to replace from search to end of string. For example !var;'search'*-1='replace'! would replace from the last occurrence of search to the end of string.

Lastly, I'm not sure if this is a bug or a feature request, but I would expect something like zero pad and right align to be chainable. For example, set n=1 & echo !n;03;5! should yield --001, but I am getting ----1 (I substituted dash for space). Note that with my proposed new syntax, the code would be written as !n;3'0';5!.

@carlos-montiers carlos-montiers added enhancement New feature or request bug Something isn't working labels Dec 29, 2019
@adoxa
Copy link
Collaborator

adoxa commented Dec 29, 2019

  • Always use single quotes whenever a string of characters is required. Escape single quote literal as '', For example !undefined;'This is Dave''s idea'!

Escaping is on the cards (need a way to get colon), but atm it's not supported, hence the three string types. Doubling up the quote will work with all types, so I see no reason to restrict it to one.

  • Ditch the 0 padding construct and allow specification of any pad string for both left and right alignment. Default to spaces for padding. So !var;05! would now be written as !var;5'0'.

Not a fan of the syntax, looks more like 5 zeroes, not length of 5, pad with zero.

A little more verbose, but now you can do something like
set "TOC=Chapter 1" & echo !TOC;-30' .'! yielding Chapter 1. . . . . . . . . . .

I was thinking of something like the Perl/Python-style * operator: echo Chapter one!30* .! (if the variable starts with a number followed by a star, repeat the rest that many times).

  • Consider abbreviating filter function names: length -> len, upper -> up, lower -> low, capital -> cap. [...] Using the 'string' syntax, !var;trim[xy]! would become !var;trim'xy'!.

Heh, this goes against Carlos' new filters; I'm content how they are. I chose square brackets as that's what Take Command uses (not that I actually use it). Your syntax looks like it will trim the string xy, not the characters x and y.

  • Implement case sensitive find/replace as !var;'search'='replace'!. In addition to providing case sensitive support, it would allow searching for = and * . [...]

I do want something like that, but again, not sure of your syntax. I was thinking more of a sed-style s/search/replace/options (where / could be any non-alphanumeric).

Lastly, I'm not sure if this is a bug or a feature request, but I would expect something like zero pad and right align to be chainable. For example, set n=1 & echo !n;03;5! should yield --001, but I am getting ----1 (I substituted dash for space). Note that with my proposed new syntax, the code would be written as !n;3'0';5!.

ATM padding and length are delayed and applied after all the other modifiers, so 03;5 is set a length of 3, padding with zeroes, then set the length to 5. That's indeed what I get: 00005. I guess I could apply it immediately, allowing what you want.

@DaveBenham
Copy link
Collaborator Author

. . . hence the three string types. Doubling up the quote will work with all types, so I see no reason to restrict it to one.

I didn't realize you could also quote with backtick or double quote when specifying a default.

Is there any built in help for anything other than called @extensions? Help for filters and all new behaviors would be great. Or is that another existing feature that I am missing?

I was thinking of something like the Perl/Python-style * operator: echo Chapter one!30* .! (if the variable starts with a number followed by a star, repeat the rest that many times).

Not quite the same, though perhaps also useful. I was looking for a way of choosing the character (or string) used to pad a value to a particular length - a generalized form of zero padding that allows padding on either side, and with any string value.

@adoxa
Copy link
Collaborator

adoxa commented Dec 29, 2019

Is there any built in help for anything other than called @extensions?

Not at the moment, but eventually.

Not quite the same, though perhaps also useful. I was looking for a way of choosing the character (or string) used to pad a value to a particular length - a generalized form of zero padding that allows padding on either side, and with any string value.

Oh, yes. How about a more explicit lpad[string] and rpad[string], similar to trim? The length would remain separate: rpad[. ];30.

@DaveBenham
Copy link
Collaborator Author

That would be good, except I don't like separating the length from the pad string. They are part of the same operation, and chaining makes more sense when they are together.

So any of the following make sense to me:

rpad[30,string]
rpad(30,string)
rpad(30,'string')
rpad(30'string')
rpad30'string'

In all cases, the string is optional, with a default value of spaces. If this were implemented, then there is no need for the numeric left and right justification filter. My original suggestion was an attempt to preserve the terseness of the original syntax with augmented capability.

@adoxa
Copy link
Collaborator

adoxa commented Dec 29, 2019

I think I prefer the first (rather not use parentheses, as they might require escaping), having quotes optional (as a way to add ']'; the character-based options have it first, so escaping isn't required). Thus rpad[30,string] and rpad[30,'string'] are equivalent. There could also be rpad[30,$var], which pads to the expansion of $var (that exists now, but as text substitution; I'd have to tweak it a bit). I don't see why we can't keep the existing number style and have these modifiers.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants