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

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

Open
danielshahaf opened this issue Dec 1, 2021 · 3 comments

Comments

@danielshahaf
Copy link
Member

@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?

@mcornella
Copy link

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.

@danielshahaf
Copy link
Member Author

danielshahaf commented Dec 1, 2021 via email

@nicoulaj
Copy link
Member

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

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

No branches or pull requests

3 participants