Skip to content

Commit

Permalink
2.0.7 命令与参数自动补全
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaobaidadada committed Jan 17, 2025
1 parent 5734072 commit 4c9b746
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "filecat",
"version": "2.0.6",
"version": "2.0.7",
"description": "filecat 文件管理器",
"author": "xiaobaidadada",
"scripts": {
Expand Down
184 changes: 184 additions & 0 deletions src/common/word_detection_js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
class Node {
char; // 也是key
children;
is_end;

constructor() {
this.children = new Map();
this.char = "";
this.is_end = false;
}
}


export class word_detection_js {
root;
size;

constructor() {
this.root = new Node(); // 根节点的字符是空的
this.size = 0;
}

add(str) {
if (!str) {
return;
}
str = str.trim();
let node = this.root;
for (const char of str) {
// 不断的给子节点添加子节点
let ret = node.children.get(char);
if (ret == null) {
ret = new Node();
ret.char = char;
node.children.set(char, ret);
}
node = ret;
}
node.is_end = true;
}

clear() {
this.root = undefined;
}

/**
* 与detection_next_one_word作用一样但是是返回一个数组多个
* @param word
* @param dif_char 允许有多个不一样的,但是这多个不一样的字符是从这个字符开始的 一般设置为 . 设置为空的话会全部返回
*/
detection_next_list_word(word,dif_char = undefined) {
const r_list = [];
if (!word || !this.root) r_list;
let now_node = this.root;
for (let i = 0; i < word.length; i++) {
const v = now_node.children.get(word[i]);
if (v == null) {
continue;
}
now_node = v;
}
// 基本字符都匹配上了现在来看看有没有多余的
if (now_node.is_end) {
r_list.push(word);
}
if(!now_node.children) {
return r_list;
}
const ok_children_list = [];// 前面的都匹配上了
if(dif_char!==undefined) {
const dif_char_node = now_node.children.get(dif_char);
if(dif_char_node !== undefined && dif_char_node.children !== undefined) {
for (const node of dif_char_node.children.values()) {
node.word = word+dif_char;
ok_children_list.push(node);
}
}
} else {
for (const node of now_node.children.values()) {
node.word = word;
ok_children_list.push(node);
}
}
// 剩下的必须唯一 不然就太多了
for (let i = 0; i < ok_children_list.length; i++) {
let node = ok_children_list[i];
let str = node.word;
while (node != null) {
if (node.is_end) {
str += node.char;
r_list.push(str);
break;
}
if (node.children !== undefined) {
// 还有子节点
if (node.children.size === 1) {
str += node.char;
node = node.children.values().next().value; // 获取唯一的元素值
} else {
break; // 不唯一返回吧
}
} else {
if (node.is_end) {
// 没有子节点了 is_end 也是true 不做判断了 直接返回吧
str += node.char;
r_list.push(str);
break;
} else {
// 既没有子节点 也不是最后一个字节 数据是有问题的只能忽略了
break;
}
}
}
}

return r_list;
}

/**
* 如果接下来只剩一个匹配(尽可能的往前匹配) 会把这个直接返回 探测接下来是不是就剩一个单词了
* 如果结果一样就不返回了
* @param word 单词 而不是被检测的文本
* @param prefer_char 优先字符 添加这个词后 最后匹配到多个 会优先匹配一下这样的词 再看看是否唯一 不支持词语 一般设置为 .
* @return str or undefined
*/
detection_next_one_word(word, prefer_char=undefined) {
if (!word || !this.root) return;
let now_node = this.root;
for (let i = 0; i < word.length; i++) {
const v = now_node.children.get(word[i]);
if (v == null) {
return;
}
now_node = v;
}
if (now_node.is_end) {
return undefined; // 一样的话就不返回了
}
if (now_node.children !== undefined) {
if (now_node.children.size === 1) {
// 只有一个元素 跳过最后一个字符
now_node = now_node.children.values().next().value;
} else if (prefer_char !== undefined) {
// 有优先字符查找是否有优先字符
now_node = now_node.children.get(prefer_char);
}
}
// 前面的都匹配的有了
while (now_node != null) {
if (now_node.is_end) {
word += now_node.char;
return word;
}
if (now_node.children !== undefined) {
// 还有子节点
if (now_node.children.size === 1) {
word += now_node.char;
now_node = now_node.children.values().next().value; // 获取唯一的元素值
} else {
return; // 不唯一返回吧
}
} else {
if (now_node.is_end) {
// 没有子节点了 is_end 也是true 不做判断了 直接返回吧
return word;
} else {
// 既没有子节点 也不是最后一个字节 数据是有问题的只能忽略了
return;
}
}

}
// 返回特殊的return
}

}

// const test = new word_detection_js();
// test.add("node.exe")
// test.add("node")
// test.add("cmd.exe")
// test.add("powershell.exe")
//
// console.log(test.detection_next_list_word("node",))
13 changes: 6 additions & 7 deletions src/main/domain/setting/setting.prefile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ export const router_pre_file =
* pre import
* 1. fs path cache_map( a map object use to cache)
* 2. needle (to http https://www.npmjs.com/package/needle)
* 3. user_login is function params is { username, password } It is best to enable custom AUTH to use custrom token
* 4. create_user is function
* 3. user_login is async function params is { username, password } It is best to enable custom AUTH to use custrom token
*/
class Api {
Expand All @@ -16,9 +15,9 @@ class Api {
* customer api
* @params headers
* @params body
* @params ctx: express ctx
* @params req: express req
*/
async handler(headers,body,ctx) {
async handler(headers,body,req) {
// todo 处理
return null;
Expand All @@ -33,13 +32,13 @@ export const self_auth_open_js_code_file =
* pre import
* 1. fs path cache_map( a map object use to cache)
* 2. needle (to http https://www.npmjs.com/package/needle)
* 3. user_login is function params is { username, password } It is best to enable custom AUTH to use custrom token
* 3. user_login is async function params is { username, password } It is best to enable custom AUTH to use custrom token
* 4. create_user is function
*/
class Api {
/*
* only use to first login 只用于登录
* only use to first login to set token for username (只用于登录 为登录的用户设置 token 这个token 拥有这个用户的全部权限)
* @params token: token
* return boolen
*/
Expand All @@ -57,7 +56,7 @@ export const self_shell_cmd_check_js_code_file =
* pre import
* 1. fs path cache_map( a map object use to cache)
* 2. needle (to http https://www.npmjs.com/package/needle)
* 3. user_login is function params is { username, password } It is best to enable custom AUTH to use custrom token
* 3. user_login is async function params is { username, password } It is best to enable custom AUTH to use custrom token
* 4. create_user is function
*/
class Api {
Expand Down
4 changes: 2 additions & 2 deletions src/main/domain/setting/setting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {data_common_key, data_dir_tem_name} from "../data/data_type";
import * as vm from "node:vm";
import {userController} from "../user/user.controller";
import {userService} from "../user/user.service";
import {sysType} from "../shell/shell.service";
import {shellServiceImpl, sysType} from "../shell/shell.service";

const needle = require('needle');

Expand All @@ -31,7 +31,6 @@ const customer_cache_map = new Map(); // 用于用户自定义缓存的map对象
const sandbox = {
needle: needle , // needle http 请求工具
user_login: userController.login,
create_user: userService.create_user,
fs:fs,
path: path,
cache_map:customer_cache_map
Expand Down Expand Up @@ -335,6 +334,7 @@ export class SettingService {

setEnvPath(paths: any[]) {
DataUtil.set(data_common_key.extra_env_path_list_key, paths);
shellServiceImpl.path_init();
return Sucess("1");
}

Expand Down
Loading

0 comments on commit 4c9b746

Please sign in to comment.