Skip to content

Commit

Permalink
Merge pull request #94 from caorushizi/dev/terminal
Browse files Browse the repository at this point in the history
Dev/terminal
  • Loading branch information
caorushizi authored Feb 25, 2024
2 parents 55f538d + d9f36ef commit 12ca2d9
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 40 deletions.
2 changes: 1 addition & 1 deletion internal
1 change: 1 addition & 0 deletions packages/main/src/services/DownloadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export default class DownloadService extends EventEmitter {
if (onMessage) {
ptyProcess.onData((data) => {
try {
this.emit("download-message", data);
const message = stripAnsi(data);
this.logger.debug("download message: ", message);
onMessage(message);
Expand Down
5 changes: 5 additions & 0 deletions packages/main/src/windows/MainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class MainWindow extends Window {
this.downloadService.on("download-failed", this.onDownloadFailed);
this.downloadService.on("download-start", this.onDownloadStart);
this.downloadService.on("download-stop", this.onDownloadStart);
this.downloadService.on("download-message", this.receiveMessage);
this.store.onDidAnyChange(this.storeChange);
app.on("second-instance", this.secondInstance);
}
Expand Down Expand Up @@ -140,6 +141,10 @@ export default class MainWindow extends Window {
this.send("download-stop", id);
};

receiveMessage = (message: any) => {
this.send("download-message", message);
};

send(channel: string, ...args: any[]) {
if (!this.window) return;

Expand Down
6 changes: 4 additions & 2 deletions packages/renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@reduxjs/toolkit": "^2.0.1",
"ahooks": "^3.7.8",
"antd": "^5.13.2",
"antd-style": "^3.6.1",
"axios": "^1.6.5",
"classnames": "^2.5.1",
"dayjs": "^1.11.10",
Expand All @@ -28,7 +29,8 @@
"react-redux": "^9.1.0",
"react-router-dom": "^6.21.3",
"sort-by": "^1.2.0",
"xgplayer": "^3.0.11"
"xgplayer": "^3.0.11",
"xterm": "^5.3.0"
},
"devDependencies": {
"@types/node": "^20.11.5",
Expand All @@ -53,4 +55,4 @@
"vite": "^5.0.12"
},
"license": "MIT"
}
}
52 changes: 52 additions & 0 deletions packages/renderer/src/components/Terminal/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { FC, useEffect, useRef } from "react";
import { useStyles } from "./style";
import "xterm/css/xterm.css";
import { Terminal as XTerminal } from "xterm";
import classNames from "classnames";
import { Button } from "antd";
import { CloseOutlined } from "@ant-design/icons";
import useElectron from "../../hooks/electron";

interface TerminalProps {
className?: string;
onClose?: () => void;
}

const Terminal: FC<TerminalProps> = ({ className, onClose }) => {
const { styles } = useStyles();
const terminalRef = useRef<HTMLDivElement | null>(null);
const { addIpcListener, removeIpcListener } = useElectron();

useEffect(() => {
const terminal = new XTerminal({
rows: 10,
cols: 100,
fontFamily: "Consolas, 'Courier New', monospace",
});
terminal.open(terminalRef.current);

const onDownloadMessage = (_: unknown, message: string) => {
terminal.write(message);
};

addIpcListener("download-message", onDownloadMessage);

return () => {
removeIpcListener("download-message", onDownloadMessage);
terminal.dispose();
};
}, []);

return (
<div ref={terminalRef} className={classNames(className, styles.container)}>
<Button
type="text"
icon={<CloseOutlined />}
className={styles.closeBtn}
onClick={onClose}
/>
</div>
);
};

export default Terminal;
18 changes: 18 additions & 0 deletions packages/renderer/src/components/Terminal/style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createStyles } from "antd-style";

export const useStyles = createStyles(({ css }) => ({
container: css`
position: relative;
`,
closeBtn: css`
position: absolute;
top: 5px;
right: 5px;
z-index: 10000;
color: #fff;
&:hover {
color: #fff !important;
}
`,
}));
2 changes: 2 additions & 0 deletions packages/renderer/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ http://example.com/xxx.m3u8`,
Origin: http://www.example.com
Referer: http://www.example.com`,
cancle: "Cancle",
terminal: "Open Terminal",
},
},
zh: {
Expand Down Expand Up @@ -229,6 +230,7 @@ Referer: http://www.example.com`,
light: "浅色",
pleaseSelectTheme: "请选择主题",
cancle: "取消",
terminal: "打开控制台",
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions packages/renderer/src/nodes/HomePage/index.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.home-page {
.home-page-inner {
padding-bottom: 10px;
display: flex;
flex-direction: column;
.download-list {
height: 100%;
overflow: hidden;
Expand Down Expand Up @@ -49,4 +51,7 @@
}
}
}
.home-page-terminal {
margin-top: 10px;
}
}
29 changes: 28 additions & 1 deletion packages/renderer/src/nodes/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import {
SyncOutlined,
MobileOutlined,
MoreOutlined,
CodeOutlined,
} from "@ant-design/icons";
import { useSelector } from "react-redux";
import { selectAppStore } from "../../store";
import DownloadFrom from "../../components/DownloadForm";
import dayjs from "dayjs";
import classNames from "classnames";
import { Trans, useTranslation } from "react-i18next";
import Terminal from "../../components/Terminal";

const { Text } = Typography;

Expand Down Expand Up @@ -81,8 +83,8 @@ const HomePage: FC<Props> = ({ filter = DownloadFilter.list }) => {
{},
);
const [messageApi, contextHolder] = message.useMessage();

const [baseUrl, setBaseUrl] = useState("");
const [terminalVisible, setTerminalVisible] = useState(false);

useAsyncEffect(async () => {
const localIP = await getLocalIP();
Expand Down Expand Up @@ -211,6 +213,14 @@ const HomePage: FC<Props> = ({ filter = DownloadFilter.list }) => {
);
};

const showTerminal = () => {
setTerminalVisible(true);
};

const closeTerminal = () => {
setTerminalVisible(false);
};

const renderActionButtons = (
dom: ReactNode,
item: DownloadItem,
Expand All @@ -229,6 +239,13 @@ const HomePage: FC<Props> = ({ filter = DownloadFilter.list }) => {
}
if (item.status === DownloadStatus.Downloading) {
return [
<Button
type="text"
key="terminal"
title={t("terminal")}
icon={<CodeOutlined />}
onClick={showTerminal}
/>,
<Button
type="text"
key="stop"
Expand All @@ -240,6 +257,13 @@ const HomePage: FC<Props> = ({ filter = DownloadFilter.list }) => {
}
if (item.status === DownloadStatus.Failed) {
return [
<Button
type="text"
key="terminal"
title={t("terminal")}
icon={<CodeOutlined />}
onClick={showTerminal}
/>,
renderEditForm(item),
<Button
type="text"
Expand Down Expand Up @@ -533,6 +557,9 @@ const HomePage: FC<Props> = ({ filter = DownloadFilter.list }) => {
);
}}
/>
{terminalVisible && (
<Terminal className="home-page-terminal" onClose={closeTerminal} />
)}
</PageContainer>
);
};
Expand Down
Loading

0 comments on commit 12ca2d9

Please sign in to comment.