Skip to content
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

Allow for multiline env vars when generating and reading /tmp/ytfzf-env #552

Merged
merged 2 commits into from
Jul 13, 2022

Conversation

HoffsMH
Copy link
Contributor

@HoffsMH HoffsMH commented Jul 12, 2022

If you want to have a multiline string anywhere in your environment such as

export FZF_DEFAULT_OPTS='--border
--bind "ctrl-y:execute-silent(echo {} | xclip)+abort"
--preview "(pistol {}) 2> /dev/null | head -500"'

ytfzf will error with

/usr/bin/ytfzf: line 488: export: --: invalid option
export: usage: export [-fn] [name[=value] ...] or export -p

because it is trying to export --bind

(I realize I could put it all on one line but it looks neater in my zshrc this way and it works with fzf just fine) 😛

This change set tells env to delimit variable definitions by the special unix null character, by calling it with -0. This character is much less likely to be found in an env var as it denotes the end of a string.

stuff I searched to come to this solution:
https://www.man7.org/linux/man-pages/man1/env.1.html
https://stackoverflow.com/questions/8677546/reading-null-delimited-strings-through-a-bash-loop

@Euro20179
Copy link
Collaborator

Euro20179 commented Jul 12, 2022

I appreciate the effort, however, env -0, read -d, and starting a string with a $ are all not posix compliant as defined here.

I'll try to look for a way to do the same thing but while confining to posix, but if I can't, I wouldn't mind doing this, but having the old way be a fail safe.

edit:
on a side note, you could use a line continuation

export FZF_DEFAULT_OPTS='--border \
--bind "ctrl-y:execute-silent(echo {} | xclip)+abort" \
--preview "(pistol {}) 2> /dev/null | head -500"'

Adding the backslashes at the end turns thee new lines into a tab or something like it.

edit 2:
Also, please use development branch

@Euro20179
Copy link
Collaborator

Euro20179 commented Jul 12, 2022

Aight i think i've found a solution

diff --git a/ytfzf b/ytfzf
index 208cd23..266143d 100755
--- a/ytfzf
+++ b/ytfzf
@@ -396,11 +396,10 @@ load_thumbnail_viewer () {
 set_vars () {
     check_exists="${1:-1}"
 
-
     #save the ecurrent environment so that any user set variables will be saved
     if [ "$check_exists" -eq 1 ]; then
        tmp_env=/tmp/ytfzf-env
-       env > "$tmp_env"
+       export -p > "$tmp_env"
     fi
 
     gap_space="                                                                                                                   "
@@ -484,12 +483,28 @@ set_vars () {
 
     #read from environment to reset any variables to what the user set
     if [ "$check_exists" -eq 1 ]; then
+       _vars=$(awk -F"\n" '
+BEGIN {
+    vars=""
+}
+/^[^"]+="/{
+    vars=vars "\n" $1
+}
+!/^[^"]+="/{
+    vars=vars "\t" $1
+}
+END{
+    print vars;
+}
+' < "$tmp_env")
        while read -r variable; do
-           export "${variable?}"
-       done < "$tmp_env"
-       rm "$tmp_env"
+           [ -z "$variable" ] && continue
+           export "${variable#export }"
+       done <<EOF
+$data
+EOF
     fi
-    unset check_exists
+    unset check_exists _vars variable
 }
 
 set_vars "${check_vars_exists:-1}"

@HoffsMH HoffsMH changed the base branch from master to development July 13, 2022 01:09
@HoffsMH
Copy link
Contributor Author

HoffsMH commented Jul 13, 2022

line continuation in the variable seems to break my installation of fzf.

I'll rebase ontop of development and try to incorporate your changes thanks!

edit: seems to work on my machine with my env vars thanks! updated!

@Euro20179
Copy link
Collaborator

Euro20179 commented Jul 13, 2022

Actually it's a little weird this works at all because I made an error, line 505 should be $_vars not $data

Edit:
Fixed it

@Euro20179 Euro20179 merged commit 87808e4 into pystardust:development Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants