zsh-docker icon indicating copy to clipboard operation
zsh-docker copied to clipboard

docs += zsh function that eval's a code snippet on various zsh versions?

Open danielshahaf opened this issue 2 years ago • 3 comments

@mcornella posted on https://www.zsh.org/workers/49600 a code snippet for quickly eval'ing a string on various zsh versions:

https://github.com/mcornella/dotfiles/blob/4bf802f6/functions#L78-L118

It might be useful to link that from the documentation here? Or perhaps even copy it into this repository (changing the default value of$image in the last two functions in the process), if Marc gives us permission?

danielshahaf avatar Dec 01 '21 05:12 danielshahaf

Sounds good to me, though at the moment they're personal functions and they're adapted very specifically to how I work. Let me post here the documentation for each one and you can adapt it yourself if you need.

zsh_docker_versions

Parses the DockerHub Registry API to get all tags of the passed image. In this case it could be useful for zshusers/zsh or ohmyzsh/zsh, but really it's image-agnostic. Used in zsh_refresh_docker to pull all images.

zsh_refresh_docker

As mentioned above, this only pulls all tags for an image hosted on the DockerHub Registry. Nothing more to see. This needs to be called before zsh_test_versions or the glob filter won't work (because there won't be any matching images).

zsh_test_versions

This one has a bit more uses that probably should be best documented in a usage message as well.

It has two optional arguments that get a value, that must be specified before the command argument:

  • --image: it gets an image name (e.g. zshusers/zsh). Default: ohmyzsh/zsh.
  • --glob: this is a glob-like parameter used to filter the tags of the image. In particular, 5.* runs for only 5.x versions of the image. If not specified, all versions are run.

Then, it has a command argument(s):

  • Mode 1: if a single argument - is passed, the Docker image runs zsh in interactive mode with stdin passed in to all zsh invocations:

    zsh_test_versions - <<EOF
    #code
    EOF
    

    You can also redirect a file to it. Note that for extra cleanliness in the output, you should put a fully formed Zsh "command" in each line. If you define, for example, a function in multiple lines you'll get PS2 lines in the output.

  • Mode 2: if any other arguments, those will be all passed to zsh -c.

The only reason I programmed the - invocation was for convenience to run zsh interactively, to see how the prompt behaves. This could be abstracted away and have it be a convenient way to define the zsh code to evaluate, and let the user define the arguments to pass to zsh. At the moment this works for my use case, but if you want to do this I guess it wouldn't be hard since we're already using zparseopts.

Oh, one last thing: the way the output is printed depends on mode 1 or mode 2. Mode 1 is optimized so that the version is shown in a single line, and the rest is the Docker output. The last empty prompt line is cleared with printf '\r\e[2K'. In mode 2 the output is structured like so: the version is shown without a newline, and then the docker output follows, which is preferably a single line.

mcornella avatar Dec 01 '21 16:12 mcornella

Thanks a lot for the go-head and for the docstrings, Marc!

I haven't got much to say about the high-level functionality here (I don't even have docker installed currently), but one drive-by point:

Oh, one last thing: the way the output is printed depends on mode 1 or mode 2. Mode 1 is optimized so that the version is shown in a single line, and the rest is the Docker output. The last empty prompt line is cleared with printf '\r\e[2K'.

One possible change here — which you might want to pick up for your dotfiles as well — would be to use ${terminfo} and ${colors} (or «print -P») rather than hardcode escape sequences. I'm not sure what \e[2K is, but \e[K and and \e[1K are respectively ${terminfo[el]} and ${terminfo[el1]} on my system, so one of those might work.

Thanks again :)

danielshahaf avatar Dec 01 '21 19:12 danielshahaf

That's pretty nice, maybe it would be worth creating a separate project for these functions ?

nicoulaj avatar Feb 13 '22 17:02 nicoulaj