Skip to content

Commit 048d6b6

Browse files
authored
Merge pull request #29 from cs01/cs01/copy-paste
add copy+paste support
2 parents ec8d143 + dc9186c commit 048d6b6

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.0.2
2+
3+
- support copy and paste in terminal (#27)
4+
15
## 0.5.0.1
26

37
- Do not fail on unicode decode errors

pyxtermjs/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

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

18-
__version__ = "0.5.0.1"
18+
__version__ = "0.5.0.2"
1919

2020
app = Flask(__name__, template_folder=".", static_folder=".", static_url_path="")
2121
app.config["SECRET_KEY"] = "secret!"

pyxtermjs/index.html

+41-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
/>
1414
</head>
1515
<body>
16-
<span style="font-size: 1.4em">pyxterm.js</span>&nbsp;&nbsp;&nbsp;
16+
<a href="https://github.com/cs01/pyxtermjs" target="_blank" style="font-size: 1.4em; text-decoration: none; color:black">pyxterm.js</a
17+
>&nbsp;&nbsp;&nbsp;
18+
</a>
1719
<span style="font-size: small"
1820
>status:
1921
<span style="font-size: small" id="status">connecting...</span></span
@@ -39,6 +41,7 @@
3941
macOptionIsMeta: true,
4042
scrollback: true,
4143
});
44+
term.attachCustomKeyEventHandler(customKeyEventHandler);
4245
// https://github.com/xtermjs/xterm.js/issues/2941
4346
const fit = new FitAddon.FitAddon();
4447
term.loadAddon(fit);
@@ -52,8 +55,12 @@
5255
fit.fit();
5356
term.writeln("Welcome to pyxterm.js!");
5457
term.writeln("https://github.com/cs01/pyxterm.js");
58+
term.writeln('')
59+
term.writeln("You can copy with ctrl+shift+x");
60+
term.writeln("You can paste with ctrl+shift+v");
61+
term.writeln('')
5562
term.onData((data) => {
56-
console.log("key pressed in browser:", data);
63+
console.log("browser terminal received new data:", data);
5764
socket.emit("pty-input", { input: data });
5865
});
5966

@@ -92,6 +99,38 @@
9299
};
93100
}
94101

102+
/**
103+
* Handle copy and paste events
104+
*/
105+
function customKeyEventHandler(e) {
106+
if (e.type !== "keydown") {
107+
return true;
108+
}
109+
if (e.ctrlKey && e.shiftKey) {
110+
const key = e.key.toLowerCase();
111+
if (key === "v") {
112+
// ctrl+shift+v: paste whatever is in the clipboard
113+
navigator.clipboard.readText().then((toPaste) => {
114+
term.writeText(toPaste);
115+
});
116+
return false;
117+
} else if (key === "c" || key === "x") {
118+
// ctrl+shift+x: copy whatever is highlighted to clipboard
119+
120+
// 'x' is used as an alternate to 'c' because ctrl+c is taken
121+
// by the terminal (SIGINT) and ctrl+shift+c is taken by the browser
122+
// (open devtools).
123+
// I'm not aware of ctrl+shift+x being used by anything in the terminal
124+
// or browser
125+
const toCopy = term.getSelection();
126+
navigator.clipboard.writeText(toCopy);
127+
term.focus();
128+
return false;
129+
}
130+
}
131+
return true;
132+
}
133+
95134
const wait_ms = 50;
96135
window.onresize = debounce(fitToscreen, wait_ms);
97136
</script>

0 commit comments

Comments
 (0)