[pass] Proposal: fuzzy search

Kjetil Torgrim Homme kjetil.homme at redpill-linpro.com
Tue Aug 16 14:44:46 CEST 2016


Den 16. aug. 2016 03:28, Kun Zhang skreiv:
>     ## Regular Expression
>     line 338: path=$(echo "$matches" | perl -pe 's/\e\[?.*?[\@-~]//g')
>     Why not use sed or builtin?
>     Who wants to install all of perl to run bash code?
> 
> 
> I googled around but didn't find other code that successfully remove
> ANSI color codes for me.
> So I landed with the first solution that worked. I can look harder though.


rewriting the regexp to not be dependent on Perl's non-greedy syntax is
quite simple. the bash-only solution below is a bit roundabout since
there is no substitute function for regular expressions, so we have to
extract the actual plain strings using the =~ operator and then do text
substitutions:

    path="$matches"
    printf -v ansi_color_re '\033\\[[^@-~]*[@-~]'
    while [[ "$path" =~ $ansi_color_re ]]
    do
        path=${path/${BASH_REMATCH[0]}/}
    done

(note, I changed the [ after ESC to be mandatory, I think that is more
correct.)

alternatively, you can write it using GNU sed like this:

    path=$(echo "$matches" | sed 's/\o033\[[^@-~]*[@-~]//g'

or more portably:

    printf -v ESC '\033'
    path=$(echo "$matches" | sed s/${ESC}'\[[^@-~]*[@-~]//g'


-- 
Kjetil T. Homme
Redpill Linpro - Changing the game

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://lists.zx2c4.com/pipermail/password-store/attachments/20160816/3bcfdc3e/attachment.asc>


More information about the Password-Store mailing list