Skip to content

Commit

Permalink
release 0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
kerwin612 committed Feb 19, 2024
1 parent 1af343b commit 75fef0c
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 85 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "portable_runner",
"private": true,
"version": "0.1.0",
"version": "0.1.1",
"type": "module",
"scripts": {
"tauri": "tauri"
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "portable_runner"
version = "0.1.0"
version = "0.1.1"
description = "Mount the portable directory as a consistent user directory. Then, based on this user directory, run your program."
authors = ["kerwin612 <kerwin612@qq.com>"]
license = "MIT"
Expand Down
66 changes: 35 additions & 31 deletions src-tauri/src/libs/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ pub struct Config {
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Shortcut {
pub key: String,
pub cmd: String,
pub run: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub r#type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -25,7 +27,7 @@ pub struct Shortcut {
#[serde(skip_serializing_if = "Option::is_none")]
pub arguments_required: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub with_file_drop: Option<Vec<WithFileDrop>>,
pub with_file: Option<Vec<WithFileDrop>>,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -46,19 +48,19 @@ const CFG_NAME: &str = ".pr.json";
const DEFAULT_CFG: &str = r#"{
"shortcuts": [
{
"key": "shutdown",
"cmd": "shutdown /s /f /t 0",
"cmd": "shutdown",
"run": "shutdown /s /f /t 0",
"style": "background: linear-gradient(0deg, darkred 0%, white 50%, white 50%, darkred 100%);"
},
{
"key": "reboot",
"cmd": "shutdown /r /f /t 0",
"cmd": "reboot",
"run": "shutdown /r /f /t 0",
"style": "background: linear-gradient(0deg, darkorange 0%, white 50%, white 50%, darkorange 100%);"
},
{
"key": "cmd",
"cmd": "cmd",
"withFileDrop": [
"run": "cmd",
"withFile": [
{
"pattern": ".*",
"folderRequired": true,
Expand All @@ -74,58 +76,60 @@ const DEFAULT_CFG: &str = r#"{
"style": "background-color: black; color: white;"
},
{
"key": "profile",
"cmd": "notepad %HOME%/.profile.cmd",
"cmd": "profile",
"run": "%HOME%/.profile.cmd",
"type": "file",
"style": "background-color: pink;"
},
{
"key": "configuration",
"cmd": "notepad %HOME%/.pr.json",
"cmd": "configuration",
"run": "%HOME%/.pr.json",
"type": "file",
"style": "background: linear-gradient(45deg, dodgerblue, whitesmoke);"
},
{
"key": "edge",
"cmd": "msedge",
"cmd": "edge",
"run": "msedge",
"group": "accessories",
"style": "background: linear-gradient(45deg, darkcyan, greenyellow);"
},
{
"key": "mstsc",
"cmd": "mstsc",
"run": "mstsc",
"group": "accessories",
"style": "background-color: dodgerblue;"
},
{
"key": "notepad",
"cmd": "notepad",
"run": "notepad",
"group": "accessories",
"withFileDrop": [
"withFile": [
{
"pattern": ".*\\.(txt|log|out)$",
"pattern": ".*\\.(txt|log|out|bat|cmd|json)$",
"fileRequired": true,
"parameters": "\"{0}\""
}
],
"style": "background-color: #5bc0de;"
},
{
"key": "calculator",
"cmd": "calc",
"cmd": "calculator",
"run": "calc",
"group": "accessories",
"style": "background-color: #6e98bf;"
},
{
"key": "paint",
"cmd": "mspaint",
"cmd": "paint",
"run": "mspaint",
"group": "accessories",
"style": "background: linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%), linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%), linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%);"
},
{
"key": "open",
"cmd": "explorer",
"cmd": "open",
"run": "explorer",
"argumentsRequired": true,
"group": "open",
"withFileDrop": [
"withFile": [
{
"pattern": ".*\\.(exe)$",
"fileRequired": true,
Expand All @@ -135,20 +139,20 @@ const DEFAULT_CFG: &str = r#"{
"style": "background-color: navajowhite;"
},
{
"key": "open-home",
"cmd": "explorer %HOME%",
"cmd": "open-home",
"run": "explorer %HOME%",
"group": "open",
"style": "background: linear-gradient(45deg, yellowgreen, greenyellow);"
},
{
"key": "open-link",
"cmd": "explorer %PORTABLE_RUNNER_ENV_LINK_PATH%",
"cmd": "open-link",
"run": "explorer %PORTABLE_RUNNER_ENV_LINK_PATH%",
"group": "open",
"style": "background: linear-gradient(45deg, forestgreen, whitesmoke);"
},
{
"key": "open-target",
"cmd": "explorer %PORTABLE_RUNNER_ENV_TARGET_PATH%",
"cmd": "open-target",
"run": "explorer %PORTABLE_RUNNER_ENV_TARGET_PATH%",
"group": "open",
"style": "background: linear-gradient(45deg, dodgerblue, bisque);"
}
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "PortableRunner",
"version": "0.1.0"
"version": "0.1.1"
},
"tauri": {
"allowlist": {
Expand Down
4 changes: 4 additions & 0 deletions src/libs/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Element.prototype.insertChildAtIndex = function(child, index) {
}
}

String.prototype.isValidPath = function() {
return /^(?:(?:[a-zA-Z]:[\\/])(?:[^<>:"|?*\r\n]+\\)*[^<>:"|?*\r\n]+$)|(?:\\\\?\\(?:[^<>:"|?*\r\n]+\\)*[^<>:"|?*\r\n]+$)|(?:[\\/])(?:[^<>:"|?*\r\n]+\\)*[^<>:"|?*\r\n]+$/.test(this);
}

String.prototype.isDir = async function() {
try {
let files = await readDir(this);
Expand Down
6 changes: 3 additions & 3 deletions src/libs/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { writeText } = window.__TAURI__.clipboard;

export function createItemElement(i, index, parent, idPrefix, getCMD, onClick) {
let ii = `${idPrefix}_item_${i.key}`;
let ii = `${idPrefix}_item_${i.cmd}`;
let ie = document.getElementById(ii);
if (ie == null) {
ie = document.createElement("div");
Expand All @@ -11,7 +11,7 @@ export function createItemElement(i, index, parent, idPrefix, getCMD, onClick) {
}
parent.insertChildAtIndex(ie, index);

ie.innerHTML = `<span>${i.label ? (i.label + "(" + i.key + ")") : i.key}</span>`;
ie.innerHTML = `<span>${i.label ? (i.label + "(" + i.cmd + ")") : i.cmd}</span>`;
ie.onclick = async (e) => {
if (ie.classList.contains("disabled")) return;
ie.classList.add("disabled");
Expand All @@ -36,5 +36,5 @@ export function createItemElement(i, index, parent, idPrefix, getCMD, onClick) {
}

export function getCmdlineWithFile(i, file, fileDrop) {
return i.cmd + " " + fileDrop.parameters.replaceAll("{0}", file);
return `${i.run} ` + fileDrop.parameters.replaceAll("{0}", file);
}
6 changes: 3 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ async function loaded() {

mainPage = MainPage({
onReloadRequired: async () => await refreshCmd(),
onFileDroped: async (file, matchs) => {
onWithFile: async (file, matchs) => {
tempPage.setData({ file, matchs });
await showWithFileDropCmd();
}
});

tempPage = TempPage({
onArgumentsRequired: async (cmd) => {
await mainPage.setData({ cmd });
onArgumentsRequired: async (cmdValue) => {
await mainPage.setData({ cmdValue });
await showCmd();
},
onBackClick: async () => await showCmd()
Expand Down
90 changes: 50 additions & 40 deletions src/mods/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const tmpl = `
</div>
`;

export function MainPage({ onReloadRequired, onFileDroped }) {
export function MainPage({ onReloadRequired, onWithFile }) {
let div = document.createElement("div");
div.id = "container_cmd";
div.innerHTML = tmpl;
Expand All @@ -22,30 +22,65 @@ export function MainPage({ onReloadRequired, onFileDroped }) {
let unListenFileDrop;

function cmdInput() {
let cmdStr = inputCmd.value;
let cmdStr = inputCmd.value.trim();
inputCmd.value = null;
return API.cmdRunner(cmdStr.trim());
if (cmdStr.isValidPath()) {
div.runWithFile({ file: cmdStr });
} else {
API.cmdRunner(cmdStr);
}
}

inputCmd.onkeydown = (e) => {
if (e.keyCode != 13) return;
cmdInput();
};

div.setData = function({ cmd }) {
inputCmd.value = cmd;
div.setData = function({ cmdValue }) {
inputCmd.value = cmdValue;
inputCmd.focus();
}

div.runWithFile = async function({ file }) {
let isDir = await file.isDir();
let matchs = []
div.currentCmds.forEach(i => {
if (!!!(i.withFile)) return;
i.withFile.forEach(j => {
if (j.pattern && new RegExp(j.pattern).test(file)) {
if (((j?.folderRequired??false) === true && !isDir) || ((j?.fileRequired??false) === true && isDir)) {
return;
}
matchs[matchs.length] = {...i, cmd: `${i.run} ${j.parameters}`, withFile: j};
}
});
});
if (matchs.length === 0) {
div.setData({ cmdValue: `"${file}"` });
return;
} else if (matchs.length === 1) {
let i = matchs[0];
let cmdValue = getCmdlineWithFile(i, file, i.withFile);
if (i.withFile.argumentsRequired) {
div.setData({ cmdValue: `${cmdValue} ` });
return;
} else {
await API.cmdRunner(cmdValue);
}
} else {
onWithFile(file, matchs);
}
}

div.doRefreshCmd = async function(list) {
let groups = {};
let items = {};
list.forEach(i => {
(div.currentCmds = list).forEach(i => {
let group = i.group??"default";
let value = groups[group]||[];
value[value.length] = i;
groups[group] = value;
items[i.key] = i;
items[i.cmd] = i;
});

cmdList.querySelectorAll(`.cmd_sub_list`).forEach(g => {
Expand Down Expand Up @@ -73,11 +108,13 @@ export function MainPage({ onReloadRequired, onFileDroped }) {
cmdList.insertChildAtIndex(ge, gindex++);

groups[group].forEach((i, iindex) => {
createItemElement(i, iindex, ge, gi, () => i.cmd, async () => {
createItemElement(i, iindex, ge, gi, () => i.run, async () => {
if (i.argumentsRequired) {
div.setData({ cmd: i.cmd + " " });
div.setData({ cmdValue: `${i.run} ` });
} else if (i.type === 'file') {
await div.runWithFile({ file: i.run });
} else {
await API.cmdRunner(i.cmd);
await API.cmdRunner(i.run);
}
})
});
Expand All @@ -90,41 +127,14 @@ export function MainPage({ onReloadRequired, onFileDroped }) {
if (file.endsWith('.lnk')) {
API.readLnk(file).then(async (lnkInfo) => {
API.addShortcut({
key: (lnkInfo?.name ?? '') || await lnkInfo.target.getFileNameWithoutExt(),
cmd: `"${lnkInfo.target}" ${lnkInfo?.arguments ?? ''}`,
cmd: (lnkInfo?.name ?? '') || await lnkInfo.target.getFileNameWithoutExt(),
run: `"${lnkInfo.target}" ${lnkInfo?.arguments ?? ''}`,
}).then(() => {
onReloadRequired();
});
});
} else {
let isDir = await file.isDir();
let matchs = []
list.forEach(i => {
if (!!!(i.withFileDrop)) return;
i.withFileDrop.forEach(j => {
if (j.pattern && new RegExp(j.pattern).test(file)) {
if (((j?.folderRequired??false) === true && !isDir) || ((j?.fileRequired??false) === true && isDir)) {
return;
}
matchs[matchs.length] = {...i, key: `${i.cmd} ${j.parameters}`, withFileDrop: j};
}
});
});
if (matchs.length === 0) {
div.setData({ cmd: `"${file}"` });
return;
} else if (matchs.length === 1) {
let i = matchs[0];
let cmd = getCmdlineWithFile(i, file, i.withFileDrop);
if (i.withFileDrop.argumentsRequired) {
div.setData({ cmd });
return;
} else {
await API.cmdRunner(cmd);
}
} else {
onFileDroped(file, matchs);
}
await div.runWithFile({ file });
}
});
}
Expand Down
Loading

0 comments on commit 75fef0c

Please sign in to comment.