8000 docs: Add copy buttons to all the codeblocks by faho · Pull Request #8218 · fish-shell/fish-shell · GitHub
[go: up one dir, main page]

Skip to content

docs: Add copy buttons to all the codeblocks#8218

Merged
faho merged 1 commit intofish-shell:masterfrom
faho:copy-codeblocks
Aug 15, 2021
Merged

docs: Add copy buttons to all the codeblocks#8218
faho merged 1 commit intofish-shell:masterfrom
faho:copy-codeblocks

Conversation

@faho
Copy link
Member
@faho faho commented Aug 13, 2021

This uses a bit of javascript to add copy buttons, so you can directly
copy all the code in a given block to the clipboard!

For codeblocks without prompts, it just copies all the code, for
blocks with prompts, it copies all the lines after prompts, under the
assumption that that's the code to be executed.

It would give you all the lines, so the output wouldn't be
interleaved like it is in the html, but good enough.

The buttons appear on hover, so they aren't usable on phones, but
since you won't really have a clipboard on phones and I have no idea
how to make them not always in front of the text otherwise: Eh.

I'm not in love with the javascript here, but it'll do.

TODOs:

  • [N/A] Changes to fish usage are reflected in user documentation/manpages.
  • [N/A] Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.rst

Screenshots

Firefox, dark theme
Screenshot_20210813_194922

Chromium, light theme
Screenshot_20210813_194956

8000
This uses a bit of javascript to add copy buttons, so you can directly
copy all the code in a given block to the clipboard!

For codeblocks without prompts, it just copies all the code, for
blocks with prompts, it copies all the lines after prompts, under the
assumption that that's the code to be executed.

It would give you *all* the lines, so the output wouldn't be
interleaved like it is in the html, but good enough.

The buttons appear on hover, so they aren't usable on phones, but
since you won't really have a clipboard on phones and I have no idea
how to make them not always in front of the text otherwise: Eh.

I'm not in love with the javascript here, but it'll do.
@faho faho added this to the fish 3.4.0 milestone Aug 13, 2021
@mqudsi
Copy link
Contributor
mqudsi commented Aug 13, 2021

I like it. I'm not sure if the smart "prompt elision" could cause any problems (by not being obvious?) or if it's just going to be a nice surprise the first time and then appreciated thereafter.

Do we have pre blocks with multiple prompts? How are those handled?

@faho
Copy link
Member Author
faho commented Aug 13, 2021

Do we have pre blocks with multiple prompts? How are those handled?

Oh, it'll give you all the lines as one, so

> echo one
one
> echo two
two

gives you

echo one
echo two

in the clipboard. That's what I meant by output not being interleaved.

I'd do multiple buttons here, but the linebreaks aren't actually part of the <span>s, so I have no idea how to figure out a line once I drop down to the children of the <pre>. So doing all lines is a compromise (and the trim() removes any trailing newlines, and bracketed paste/fish_clipboard_paste makes it so the lines aren't executed independently).

@mqudsi
Copy link
Contributor
mqudsi commented Aug 15, 2021

I'm just worried that it's too smart. I'm fine if you want to merge it, but just want to say that KISS might apply here. (I feel like either KISS or go all the way with either per-prompt copy buttons like you mentioned or if you really want, maybe you can apply a background shading on hover that only applies to the content that would be copied at the same that the copy button becomes active - as an indication of what would be copied?)

As a different matter altogether though, it occurs to me to question if people typically copy-and-paste examples to run directly as-is in the shell? What's the benefit if it's a deterministic process and the result is the same as they see in the browser? Ideally they'd be making changes to the command, which isn't too easy to do post-facto when copy-and-pasting into the shell where it's probably faster to just retype what you want than navigate to the various parts needing changes (until we get mouse support, given that people copying-and-pasting examples are probably not going to be hard-core vim/emacs users).

@faho
Copy link
Member Author
faho commented Aug 15, 2021

What's the benefit if it's a deterministic process and the result is the same as they see in the browser?

It allows you to see for yourself, easily! And then, once you've seen that, you can alter it.

Of course you could also be typing this stuff, but having a two click option makes it just that little bit quicker and easier that it makes it more tactile.

Now, of course this isn't a massive thing, but it's a fun thing that's easily implemented.

@faho faho merged commit 7f34b8a into fish-shell:master Aug 15, 2021
@faho faho deleted the copy-codeblocks branch August 15, 2021 18:09
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0