zsh-users
 help / color / mirror / code / Atom feed
From: Roman Perepelitsa <roman.perepelitsa@gmail.com>
To: OG Code Poet <ogcodepoet@gmail.com>
Cc: zsh-users@zsh.org
Subject: Re: Read/write multiple histories from non-interactive shell
Date: Mon, 13 Feb 2023 09:18:46 +0100	[thread overview]
Message-ID: <CAN=4vMrRZ5RmbipKmJq0mp37ejztdKKahVwhRZZprMSUAUJvqQ@mail.gmail.com> (raw)
In-Reply-To: <CADmFDtV9uQk6u1OmobJiMAMm6kSo63M+L0q1hCsvmNr+4_L2Xg@mail.gmail.com>

On Mon, Feb 13, 2023 at 8:36 AM OG Code Poet <ogcodepoet@gmail.com> 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.


  parent reply	other threads:[~2023-02-13  8:19 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-13  7:35 OG Code Poet
2023-02-13  7:43 ` Lawrence Velázquez
2023-02-13  8:18 ` Roman Perepelitsa [this message]
2023-02-13  8:57   ` OG Code Poet
2023-02-13  9:43     ` Roman Perepelitsa
2023-02-16  6:14       ` OG Code Poet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAN=4vMrRZ5RmbipKmJq0mp37ejztdKKahVwhRZZprMSUAUJvqQ@mail.gmail.com' \
    --to=roman.perepelitsa@gmail.com \
    --cc=ogcodepoet@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).