Skip to content

Commit

Permalink
fix: fix line indent issue and send correct command to server #3
Browse files Browse the repository at this point in the history
Signed-off-by: seven <zilisheng1996@gmail.com>
  • Loading branch information
Blankll committed Mar 15, 2024
1 parent afe5f18 commit 92da89d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
30 changes: 17 additions & 13 deletions src/views/ssh/components/ssh-terminal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ const terminal = new Terminal({
cursorBlink: true, // 光标闪烁
cursorStyle: 'bar',
cursorInactiveStyle: 'underline', // 光标样式
convertEol: true, // 回车换行
});
const fitAddon = new FitAddon();
const terminalContainer = ref();
const enter = () => terminal.write('\r\n');
const backspace = () => terminal.write('\b \b');
const keyActions: { [key: string]: (terminal: Terminal) => void } = {
enter,
backspace,
const sequenceMap: { [key: string]: string } = {
enter: '\r\n',
backspace: '\b \b',
};
const commands: Array<string> = [];
let command = '';
// Handle the key event
terminal.onKey(e => {
const code = e.domEvent.code.toLowerCase();
const keyAction = keyActions[code];
const sequence = sequenceMap[code];
if (keyAction) {
keyAction(terminal);
exec(command);
if (code === 'enter') {
commands.push(command);
terminal.write(sequence);
exec(command);
command = '';
return;
} else if (code === 'backspace') {
command = command.slice(0, -1);
terminal.write(sequence);
} else {
terminal.write(e.key);
command += e.key;
Expand All @@ -62,8 +62,12 @@ const exec = (command: string) => {
// eslint-disable-next-line
console.log(`exec res ${res}`);
terminal.writeln(res as string);
terminal.writeln('');
})
.catch(e => terminal.writeln(e));
.catch(e => {
terminal.writeln(e);
terminal.writeln('');
});
};
onMounted(async () => {
Expand All @@ -72,10 +76,10 @@ onMounted(async () => {
// Attach the terminal to the container
terminal.open(terminalContainer.value);
fitAddon.fit();
terminal.focus();
// Example: Write text to the terminal
terminal.write('Welcome to AnyTerm!\r\n');
// Optional: Add terminal handling logic, e.g., for executing commands
// terminal.onData((data: string) => {
// terminal.write(data);
Expand Down
2 changes: 1 addition & 1 deletion src/views/ssh/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const handleClose = (name: string) => {
<style lang="scss" scoped>
.ssh-tab-container {
width: 100%;
height: 99%;
height: 100%;
.tab-pane-container {
height: 100%;
width: 100%;
Expand Down

0 comments on commit 92da89d

Please sign in to comment.