Skip to content

Commit

Permalink
Merge pull request #29 from cs01/cs01/copy-paste
Browse files Browse the repository at this point in the history
add copy+paste support
  • Loading branch information
cs01 authored Oct 4, 2022
2 parents ec8d143 + dc9186c commit 048d6b6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.0.2

- support copy and paste in terminal (#27)

## 0.5.0.1

- Do not fail on unicode decode errors
Expand Down
2 changes: 1 addition & 1 deletion pyxtermjs/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

logging.getLogger("werkzeug").setLevel(logging.ERROR)

__version__ = "0.5.0.1"
__version__ = "0.5.0.2"

app = Flask(__name__, template_folder=".", static_folder=".", static_url_path="")
app.config["SECRET_KEY"] = "secret!"
Expand Down
43 changes: 41 additions & 2 deletions pyxtermjs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
/>
</head>
<body>
<span style="font-size: 1.4em">pyxterm.js</span>&nbsp;&nbsp;&nbsp;
<a href="https://github.com/cs01/pyxtermjs" target="_blank" style="font-size: 1.4em; text-decoration: none; color:black">pyxterm.js</a
>&nbsp;&nbsp;&nbsp;
</a>
<span style="font-size: small"
>status:
<span style="font-size: small" id="status">connecting...</span></span
Expand All @@ -39,6 +41,7 @@
macOptionIsMeta: true,
scrollback: true,
});
term.attachCustomKeyEventHandler(customKeyEventHandler);
// https://github.com/xtermjs/xterm.js/issues/2941
const fit = new FitAddon.FitAddon();
term.loadAddon(fit);
Expand All @@ -52,8 +55,12 @@
fit.fit();
term.writeln("Welcome to pyxterm.js!");
term.writeln("https://github.com/cs01/pyxterm.js");
term.writeln('')
term.writeln("You can copy with ctrl+shift+x");
term.writeln("You can paste with ctrl+shift+v");
term.writeln('')
term.onData((data) => {
console.log("key pressed in browser:", data);
console.log("browser terminal received new data:", data);
socket.emit("pty-input", { input: data });
});

Expand Down Expand Up @@ -92,6 +99,38 @@
};
}

/**
* Handle copy and paste events
*/
function customKeyEventHandler(e) {
if (e.type !== "keydown") {
return true;
}
if (e.ctrlKey && e.shiftKey) {
const key = e.key.toLowerCase();
if (key === "v") {
// ctrl+shift+v: paste whatever is in the clipboard
navigator.clipboard.readText().then((toPaste) => {
term.writeText(toPaste);
});
return false;
} else if (key === "c" || key === "x") {
// ctrl+shift+x: copy whatever is highlighted to clipboard

// 'x' is used as an alternate to 'c' because ctrl+c is taken
// by the terminal (SIGINT) and ctrl+shift+c is taken by the browser
// (open devtools).
// I'm not aware of ctrl+shift+x being used by anything in the terminal
// or browser
const toCopy = term.getSelection();
navigator.clipboard.writeText(toCopy);
term.focus();
return false;
}
}
return true;
}

const wait_ms = 50;
window.onresize = debounce(fitToscreen, wait_ms);
</script>
Expand Down

0 comments on commit 048d6b6

Please sign in to comment.