-
Notifications
You must be signed in to change notification settings - Fork 10
feat: Allow copying text from the terminal #427
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
Open
adameska
wants to merge
11
commits into
podman-desktop:main
Choose a base branch
from
adameska:AllowCopyPaste
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+75
−1
Open
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fb08e5c
feat: Allow copying text from the terminal
adameska fd34f9b
All sigterm kill signal to be caught still
adameska f2df29e
Utilize platform for copy shortcut
adameska e4adfca
Bug fix
adameska 451e6b8
Make sure linux includes the shift key
adameska 63a106d
Merge remote-tracking branch 'remotes/upstream/main' into AllowCopyPaste
aaf69c0
Fix formatting
fcbdd26
Fix tests
adameska b7bb030
Fix tests
adameska 8f5ebaf
Merge branch 'AllowCopyPaste' of https://github.com/adameska/extensio…
adameska eaccf77
Add copy/paste support to terminal
adameska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
218 changes: 139 additions & 79 deletions
218
packages/webview/src/component/terminal/TerminalWindow.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,87 +1,147 @@ | ||
| <script lang="ts"> | ||
| import '@xterm/xterm/css/xterm.css'; | ||
|
|
||
| import { FitAddon } from '@xterm/addon-fit'; | ||
| import { Terminal } from '@xterm/xterm'; | ||
| import { onDestroy, onMount } from 'svelte'; | ||
|
|
||
| import { getTerminalTheme } from './terminal-theme'; | ||
| import TerminalSearchControls from './TerminalSearchControls.svelte'; | ||
|
|
||
| interface Props { | ||
| terminal?: Terminal; | ||
| convertEol?: boolean; | ||
| disableStdIn?: boolean; | ||
| screenReaderMode?: boolean; | ||
| showCursor?: boolean; | ||
| search?: boolean; | ||
| class?: string; | ||
| } | ||
|
|
||
| let { | ||
| terminal = $bindable(), | ||
| convertEol, | ||
| disableStdIn = true, | ||
| screenReaderMode, | ||
| showCursor = false, | ||
| search = false, | ||
| class: className, | ||
| }: Props = $props(); | ||
|
|
||
| let logsXtermDiv: HTMLDivElement | undefined; | ||
| let resizeHandler: () => void; | ||
|
|
||
| async function refreshTerminal(): Promise<void> { | ||
| // missing element, return | ||
| if (!logsXtermDiv) { | ||
| return; | ||
| } | ||
| // grab font size | ||
| const fontSize = 10; // TODO: get from configuration | ||
| const lineHeight = 1; // TODO: get from configuration | ||
|
|
||
| terminal = new Terminal({ | ||
| fontSize, | ||
| lineHeight, | ||
| disableStdin: disableStdIn, | ||
| theme: getTerminalTheme(), | ||
| convertEol: convertEol, | ||
| screenReaderMode: screenReaderMode, | ||
| }); | ||
| const fitAddon = new FitAddon(); | ||
| terminal.loadAddon(fitAddon); | ||
|
|
||
| terminal.open(logsXtermDiv); | ||
| if (!showCursor) { | ||
| // disable cursor | ||
| terminal.write('\x1b[?25l'); | ||
| } | ||
|
|
||
| // call fit addon each time we resize the window | ||
| resizeHandler = (): void => { | ||
| fitAddon.fit(); | ||
| }; | ||
| window.addEventListener('resize', resizeHandler); | ||
|
|
||
| fitAddon.fit(); | ||
| } | ||
|
|
||
| onMount(async () => { | ||
| await refreshTerminal(); | ||
| }); | ||
|
|
||
| onDestroy(() => { | ||
| window.removeEventListener('resize', resizeHandler); | ||
| terminal?.dispose(); | ||
| }); | ||
| import '@xterm/xterm/css/xterm.css'; | ||
|
|
||
| import { API_SYSTEM } from '@kubernetes-dashboard/channels'; | ||
| import { FitAddon } from '@xterm/addon-fit'; | ||
| import { Terminal } from '@xterm/xterm'; | ||
| import { getContext, onDestroy, onMount } from 'svelte'; | ||
| import { Remote } from '/@/remote/remote'; | ||
|
|
||
| import { getTerminalTheme } from './terminal-theme'; | ||
| import TerminalSearchControls from './TerminalSearchControls.svelte'; | ||
|
|
||
| interface Props { | ||
| terminal?: Terminal; | ||
| convertEol?: boolean; | ||
| disableStdIn?: boolean; | ||
| screenReaderMode?: boolean; | ||
| showCursor?: boolean; | ||
| search?: boolean; | ||
| class?: string; | ||
| } | ||
|
|
||
| let { | ||
| terminal = $bindable(), | ||
| convertEol, | ||
| disableStdIn = true, | ||
| screenReaderMode, | ||
| showCursor = false, | ||
| search = false, | ||
| class: className, | ||
| }: Props = $props(); | ||
|
|
||
| let logsXtermDiv: HTMLDivElement | undefined; | ||
| let resizeHandler: () => void; | ||
| let contextMenuHandler: (event: MouseEvent) => void; | ||
|
|
||
| const remote = getContext<Remote>(Remote); | ||
| const systemApi = remote.getProxy(API_SYSTEM); | ||
| let platformName = $state<string>(); | ||
|
|
||
| function copySelectionToClipboard(): boolean { | ||
| let copied = false; | ||
| const selection = terminal?.getSelection(); | ||
| if (selection) { | ||
| //We don't have permissions to the clipboard so instead we can use a text area with copy command to get around that | ||
| const textarea = document.createElement('textarea'); | ||
| textarea.value = selection; | ||
| textarea.style.position = 'fixed'; | ||
| textarea.style.opacity = '0'; | ||
| document.body.appendChild(textarea); | ||
| textarea.select(); | ||
| try { | ||
| document.execCommand('copy'); | ||
| copied = true; | ||
| } catch (err) { | ||
| console.error('Failed to copy:', err); | ||
| } | ||
| document.body.removeChild(textarea); | ||
| } | ||
| return copied; | ||
| } | ||
|
|
||
| async function refreshTerminal(): Promise<void> { | ||
| // missing element, return | ||
| if (!logsXtermDiv) { | ||
| return; | ||
| } | ||
| // grab font size | ||
| const fontSize = 10; // TODO: get from configuration | ||
| const lineHeight = 1; // TODO: get from configuration | ||
|
|
||
| terminal = new Terminal({ | ||
| fontSize, | ||
| lineHeight, | ||
| disableStdin: disableStdIn, | ||
| theme: getTerminalTheme(), | ||
| convertEol: convertEol, | ||
| screenReaderMode: screenReaderMode, | ||
| rightClickSelectsWord: true, | ||
| }); | ||
| const fitAddon = new FitAddon(); | ||
| terminal.loadAddon(fitAddon); | ||
|
|
||
| terminal.open(logsXtermDiv); | ||
| if (!showCursor) { | ||
| // disable cursor | ||
| terminal.write('\x1b[?25l'); | ||
| } | ||
|
|
||
| //copy behavior | ||
| terminal.attachCustomKeyEventHandler((event: KeyboardEvent): boolean => { | ||
| const isCopyShortcut = platformName === 'darwin' | ||
| ? event.metaKey && event.key === 'c' | ||
| : event.ctrlKey && event.key === 'c'; | ||
|
|
||
| if (isCopyShortcut) { | ||
| const handled = copySelectionToClipboard(); | ||
| if (handled) { | ||
| terminal?.clearSelection(); | ||
| event.preventDefault(); | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| }); | ||
|
|
||
| contextMenuHandler = (event: MouseEvent): boolean => { | ||
| const handled = copySelectionToClipboard(); | ||
| if (handled) { | ||
| terminal?.clearSelection(); | ||
| event.preventDefault(); | ||
| return false; | ||
| } | ||
| return true; | ||
| }; | ||
| logsXtermDiv.addEventListener('contextmenu', contextMenuHandler); | ||
|
|
||
| // call fit addon each time we resize the window | ||
| resizeHandler = (): void => { | ||
| fitAddon.fit(); | ||
| }; | ||
| window.addEventListener('resize', resizeHandler); | ||
|
|
||
| fitAddon.fit(); | ||
| } | ||
|
|
||
| onMount(async () => { | ||
| platformName = await systemApi.getPlatformName(); | ||
| await refreshTerminal(); | ||
| }); | ||
|
|
||
| onDestroy(() => { | ||
| window.removeEventListener('resize', resizeHandler); | ||
| logsXtermDiv?.removeEventListener('contextmenu', contextMenuHandler); | ||
| terminal?.dispose(); | ||
| }); | ||
| </script> | ||
|
|
||
| {#if search && terminal} | ||
| <TerminalSearchControls terminal={terminal} /> | ||
| <TerminalSearchControls terminal={terminal} /> | ||
| {/if} | ||
|
|
||
| <div | ||
| class="{className} overflow-hidden p-[5px] pr-0 bg-[var(--pd-terminal-background)]" | ||
| role="term" | ||
| bind:this={logsXtermDiv}> | ||
| class="{className} overflow-hidden p-[5px] pr-0 bg-[var(--pd-terminal-background)]" | ||
| role="term" | ||
| bind:this={logsXtermDiv}> | ||
| </div> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.