Skip to content
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

fix: 命令参数校验优化 #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/components/yu-terminal/YuTerminal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,15 @@
</template>

<script setup lang="ts">
import { computed, onMounted, Ref, ref, StyleValue, toRefs, watchEffect } from "vue";
import {
computed,
onMounted,
Ref,
ref,
StyleValue,
toRefs,
watchEffect,
} from "vue";
import CommandOutputType = YuTerminal.CommandOutputType;
import OutputType = YuTerminal.OutputType;
import CommandInputType = YuTerminal.CommandInputType;
Expand Down Expand Up @@ -237,6 +245,7 @@ const wrapperStyle = computed(() => {
} else {
style.background = background;
}
style.backgroundSize = "cover";
return style;
});

Expand Down Expand Up @@ -325,16 +334,20 @@ const focusInput = () => {
* 获取输入框是否聚焦
*/
const isInputFocused = () => {
return (commandInputRef.value.input as HTMLInputElement) == document.activeElement
}
return (
(commandInputRef.value.input as HTMLInputElement) == document.activeElement
);
};
/**
* 设置输入框的值
*/
const setTabCompletion = () => {
if(hint.value){
inputCommand.value.text =`${hint.value.split(' ')[0]}${hint.value.split(' ').length > 1 ? ' ' : ''}`
if (hint.value) {
inputCommand.value.text = `${hint.value.split(" ")[0]}${
hint.value.split(" ").length > 1 ? " " : ""
}`;
}
}
};

/**
* 折叠 / 展开所有块
Expand Down
6 changes: 4 additions & 2 deletions src/components/yu-terminal/hint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ const useHint = () => {
}
const args = trim(inputText).split(" ");
const func = args[0];
const likeKey = Object.keys(commandMap).filter(key => key.indexOf(func) === 0)[0];
let command = commandMap[likeKey];
const likeKey = Object.keys(commandMap).filter(
(key) => key.indexOf(func) === 0
)[0];
let command = commandMap[likeKey];
if (!command) {
hint.value = "";
return;
Expand Down
17 changes: 12 additions & 5 deletions src/core/commands/fanyi/fanyiCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,26 @@ const fanyiCommand: CommandType = {
],
async action(options, terminal) {
const { _, from, to } = options;
if (_.length < 1) {
const validArgs = _.filter((i) => Boolean(i));
if (validArgs.length < 1) {
terminal.writeTextErrorResult("参数不足");
return;
}
const keywords = _[0];
const keywords = validArgs[0];
const res: any = await translate(keywords, {
from,
to,
});
if (res?.code === 0) {
terminal.writeTextSuccessResult(
`翻译结果:${res.data.trans_result[0].dst}`
);
if (res.data.error_code) {
terminal.writeTextErrorResult(
`翻译失败,错误信息:${res.data.error_msg}`
);
} else {
terminal.writeTextSuccessResult(
`翻译结果:${res.data.trans_result[0].dst}`
);
}
} else {
terminal.writeTextErrorResult(res?.message ?? "翻译失败");
}
Expand Down
5 changes: 3 additions & 2 deletions src/core/commands/gotoCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ export default {
],
action(options, terminal): void {
const { _, self } = options;
if (_.length < 1) {
const validArgs = _.filter((i) => Boolean(i));
if (validArgs.length < 1) {
terminal.writeTextErrorResult("参数不足");
return;
}
let link = _[0];
let link = validArgs[0];
// 优先找空间条目链接
let { getItem } = useSpaceStore();
const item = getItem(link);
Expand Down
5 changes: 3 additions & 2 deletions src/core/commands/pingCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ const pingCommand: CommandType = {
options: [],
async action(options, terminal) {
const { _ } = options;
if (_.length < 1) {
const validArgs = _.filter((i) => Boolean(i));
if (validArgs.length < 1) {
terminal.writeTextErrorResult("参数不足");
return;
}
var dest = _[0];
var dest = validArgs[0];
if (
dest.substr(0, 7).toLowerCase() != "http://" &&
dest.substr(0, 8).toLowerCase() != "https://"
Expand Down
5 changes: 3 additions & 2 deletions src/core/commands/relax/music/musicCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ const musicCommand: CommandType = {
collapsible: true,
action(options, terminal) {
const { _ } = options;
if (_.length < 1) {
const validArgs = _.filter((i) => Boolean(i));
if (validArgs.length < 1) {
terminal.writeTextErrorResult("参数不足");
return;
}
const name = _[0];
const name = validArgs[0];
const output: ComponentOutputType = {
type: "component",
component: defineAsyncComponent(() => import("./MusicBox.vue")),
Expand Down
5 changes: 3 additions & 2 deletions src/core/commands/space/copyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ const copyCommand: CommandType = {
],
action(options, terminal): void {
const { _, recursive = false } = options;
if (_.length < 2) {
const validArgs = _.filter((i) => Boolean(i));
if (validArgs.length < 2) {
terminal.writeTextErrorResult("参数不足");
return;
}
const spaceStore = useSpaceStore();
const [source, target] = _;
const [source, target] = validArgs;
const result = spaceStore.copyItem(source, target, recursive);
if (result) {
terminal.writeTextResult("复制成功");
Expand Down
5 changes: 3 additions & 2 deletions src/core/commands/space/mkdirCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ const mkdirCommand: CommandType = {
options: [],
action(options, terminal): void {
const { _ } = options;
if (_.length < 1) {
const validArgs = _.filter((i) => Boolean(i));
if (validArgs.length < 1) {
terminal.writeTextErrorResult("参数不足");
return;
}
const spaceStore = useSpaceStore();
const newDir = _[0];
const newDir = validArgs[0];
const item: SpaceItemType = {
dir: spaceStore.currentDir,
name: newDir,
Expand Down
5 changes: 3 additions & 2 deletions src/core/commands/space/moveCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ const moveCommand: CommandType = {
],
action(options, terminal): void {
const { _, recursive = false } = options;
if (_.length < 2) {
const validArgs = _.filter((i) => Boolean(i));
if (validArgs.length < 2) {
terminal.writeTextErrorResult("参数不足");
return;
}
const spaceStore = useSpaceStore();
const [source, target] = _;
const [source, target] = validArgs;
const result = spaceStore.moveItem(source, target, recursive);
if (result) {
terminal.writeTextResult("移动成功");
Expand Down
5 changes: 3 additions & 2 deletions src/core/commands/space/removeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ const removeCommand: CommandType = {
],
action(options: ParsedOptions, terminal): void {
const { _, recursive = false, force } = options;
if (_.length < 1) {
const validArgs = _.filter((i) => Boolean(i));
if (validArgs.length < 1) {
terminal.writeTextErrorResult("参数不足");
return;
}
const deleteKey = _[0];
const deleteKey = validArgs[0];
if (recursive && !force) {
terminal.writeTextErrorResult("请确认要强制删除");
return;
Expand Down
4 changes: 4 additions & 0 deletions src/core/commands/timing/timingCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const timingCommand: CommandType = {
terminal.writeTextErrorResult("参数不足");
return;
}
if (isNaN(Number(seconds))) {
terminal.writeTextErrorResult("参数错误");
return;
}
const output: ComponentOutputType = {
type: "component",
component: defineAsyncComponent(() => import("./TimingBox.vue")),
Expand Down