Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Read/write multiple histories from non-interactive shell



I tried pretty much same code but without "zsh -i", that's why the subject says "non-interactive". My larger script misbehaves and exits with 0 before it could complete if I replace #!/usr/bin/env zsh with #!/usr/bin/env -S zsh -fi or #!/usr/bin/env -S zsh -i. Is there a restriction on starting any existing functional zsh program with -fi? Put another way, are all non-interactive shell scripts guaranteed to function when shebang is changed to #!/usr/bin/env -S zsh -fi??


On Mon, Feb 13, 2023 at 12:18 AM Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx> wrote:
On Mon, Feb 13, 2023 at 8:36 AM OG Code Poet <ogcodepoet@xxxxxxxxx> wrote:
>
> Let's say there is a non-interactive script with multiple form fields (each with a different vared), and a user can enter the form multiple times. I want to preserve individual history for each form field.
>
> There are two possibilities:
>
> 1. Keep history internal to the script
>         I couldn't find an interface for this. Doing ``fc -p`` once in the beginning of script does provide an internal history, but it is shared between all vareds (which is not ideal).
> 2. Keep history external to the script
>         Not all ``fc`` commands work. ``fc -R`` does read correctly from external history files. But ``print -s``, ``fc-W`` and ``fc -A`` do not. Seems the only option is to do an echo "$string" >>~/path//form_entry_1.hist file. But I guess that has disadvantages because it lacks the benefits that zsh provides in resolving duplicates.
>
> Is there a way out? Should this also be copied to zsh-workers for feature request?

How about this?

    #!/usr/bin/env -S zsh -fi

    histdir=~/.formhist
    mkdir -p -- $histdir || exit

    function read-field() {
      emulate -L zsh
      local var=$1
      local desc=$2
      fc -pa $histdir/$var 1000 1000
      trap 'exit 130' INT
      vared -hep "Enter $desc: " -c $var || exit
      print -rs -- ${(P)var}
    }

    while true; do
      read-field first_name '%F{green}First Name%f'
      typeset -p first_name
      unset first_name

      read-field email '%F{yellow}Email Address%f'
      typeset -p email
      unset email
    done

The trap is a workaround for what looks like a bug. Without it, if
interrupt vared with Ctrl-C, the history file gets truncated to its
first entry.

Roman.


Messages sorted by: Reverse Date, Date, Thread, Author