fixes pathSelector

This commit is contained in:
ThatBen 2025-04-02 10:15:32 +02:00
parent 939bf03b08
commit 02f5cd0244
No known key found for this signature in database
GPG Key ID: E020A7DDCD52E1AB
2 changed files with 61 additions and 50 deletions

View File

@ -97,7 +97,7 @@ export async function main() {
const configService = new ConfigService(); const configService = new ConfigService();
const uiService = new UiService(); const uiService = new UiService();
const fsService = new FsService(); const fsService = new FsService();
const pathSelector = new PathSelector(uiService, fsService); const pathSelector = new PathSelector(uiService, new MenuLoop(), fsService);
const numberSelector = new NumberSelector(uiService); const numberSelector = new NumberSelector(uiService);
const installMenu = new InstallMenu(uiService, configService); const installMenu = new InstallMenu(uiService, configService);
const configMenu = new ConfigMenu( const configMenu = new ConfigMenu(
@ -107,7 +107,12 @@ export async function main() {
pathSelector, pathSelector,
numberSelector, numberSelector,
); );
const mainMenu = new MainMenu(uiService, new MenuLoop(), installMenu, configMenu); const mainMenu = new MainMenu(
uiService,
new MenuLoop(),
installMenu,
configMenu,
);
await mainMenu.show(); await mainMenu.show();
return; return;

View File

@ -1,13 +1,14 @@
export class PathSelector { export class PathSelector {
constructor(uiService, fsService) { constructor(uiService, menuLoop, fsService) {
this.ui = uiService; this.ui = uiService;
this.loop = menuLoop;
this.fs = fsService; this.fs = fsService;
this.pathMustExist = true; this.pathMustExist = true;
this.loop.initialize(this.showPathSelector);
} }
show = async (startingPath, pathMustExist) => { show = async (startingPath, pathMustExist) => {
this.running = true;
this.startingPath = startingPath; this.startingPath = startingPath;
this.pathMustExist = pathMustExist; this.pathMustExist = pathMustExist;
this.roots = this.fs.getAvailableRoots(); this.roots = this.fs.getAvailableRoots();
@ -15,39 +16,42 @@ export class PathSelector {
if (!this.hasValidRoot(this.currentPath)) { if (!this.hasValidRoot(this.currentPath)) {
this.currentPath = [roots[0]]; this.currentPath = [roots[0]];
} }
while (this.running) {
this.showCurrent(); await this.loop.showLoop();
this.ui.askMultiChoice("Select an option:", [
{
label: "Enter path",
action: this.enterPath,
},
{
label: "Go up one",
action: this.upOne,
},
{
label: "Go down one",
action: this.downOne,
},
{
label: "Create new folder here",
action: this.createSubDir,
},
{
label: "Select this path",
action: this.selectThisPath,
},
{
label: "Cancel",
action: this.cancel,
},
]);
}
return this.resultingPath; return this.resultingPath;
}; };
showPathSelector = async () => {
this.showCurrent();
await this.ui.askMultipleChoice("Select an option:", [
{
label: "Enter path",
action: this.enterPath,
},
{
label: "Go up one",
action: this.upOne,
},
{
label: "Go down one",
action: this.downOne,
},
{
label: "Create new folder here",
action: this.createSubDir,
},
{
label: "Select this path",
action: this.selectThisPath,
},
{
label: "Cancel",
action: this.cancel,
},
]);
};
splitPath = (str) => { splitPath = (str) => {
return str.replaceAll("\\", "/").split("/"); return str.replaceAll("\\", "/").split("/");
}; };
@ -65,13 +69,14 @@ export class PathSelector {
combine = (parts) => { combine = (parts) => {
const toJoin = this.dropEmptyParts(parts); const toJoin = this.dropEmptyParts(parts);
if (toJoin.length == 1) return toJoin[0]; if (toJoin.length == 1) return toJoin[0];
return this.fs.pathJoin(...toJoin); const result = this.fs.pathJoin(toJoin);
return result;
}; };
combineWith = (parts, extra) => { combineWith = (parts, extra) => {
const toJoin = this.dropEmptyParts(parts); const toJoin = this.dropEmptyParts(parts);
if (toJoin.length == 1) return this.fs.pathJoin(toJoin[0], extra); if (toJoin.length == 1) return this.fs.pathJoin([toJoin[0], extra]);
return this.fs.pathJoin(...toJoin, extra); return this.fs.pathJoin([...toJoin, extra]);
}; };
showCurrent = () => { showCurrent = () => {
@ -103,14 +108,16 @@ export class PathSelector {
updateCurrentIfValidFull = (newFullPath) => { updateCurrentIfValidFull = (newFullPath) => {
if (this.pathMustExist && !this.fs.isDir(newFullPath)) { if (this.pathMustExist && !this.fs.isDir(newFullPath)) {
console.log("The path does not exist."); this.ui.showErrorMessage("The path does not exist.");
return;
} }
this.updateCurrentIfValidParts(this.splitPath(newFullPath)); this.updateCurrentIfValidParts(this.splitPath(newFullPath));
}; };
updateCurrentIfValidParts = (newParts) => { updateCurrentIfValidParts = (newParts) => {
if (!this.hasValidRoot(newParts)) { if (!this.hasValidRoot(newParts)) {
console.log("The path has no valid root."); this.ui.showErrorMessage("The path has no valid root.");
return;
} }
this.currentPath = newParts; this.currentPath = newParts;
}; };
@ -133,24 +140,19 @@ export class PathSelector {
getSubDirOptions = () => { getSubDirOptions = () => {
const fullPath = this.combine(this.currentPath); const fullPath = this.combine(this.currentPath);
const entries = this.fs.readDir(fullPath); const entries = this.fs.readDir(fullPath);
var result = []; return entries.filter(entry => this.isSubDir(entry));
entries.forEach(function (entry) {
if (this.isSubDir(entry)) {
result.push(entry);
}
});
return result;
}; };
downOne = async () => { downOne = async () => {
const options = this.getSubDirOptions(); const options = this.getSubDirOptions();
if (options.length == 0) { if (options.length == 0) {
console.log("There are no subdirectories here."); this.ui.showInfoMessage("There are no subdirectories here.");
return;
} }
var selected = ""; var selected = "";
var uiOptions = []; var uiOptions = [];
options.foreach(function (option) { options.forEach(function (option) {
uiOptions.push({ uiOptions.push({
label: option, label: option,
action: () => { action: () => {
@ -168,16 +170,20 @@ export class PathSelector {
createSubDir = async () => { createSubDir = async () => {
const name = await this.ui.askPrompt("Enter name:"); const name = await this.ui.askPrompt("Enter name:");
if (name.length < 1) return; if (name.length < 1) return;
this.updateCurrentIfValidParts([...currentPath, name]); const newPath = [...this.currentPath, name];
if (this.pathMustExist) {
this.fs.makeDir(this.combine(newPath));
}
this.updateCurrentIfValidParts(newPath);
}; };
selectThisPath = async () => { selectThisPath = async () => {
this.resultingPath = this.combine(this.currentPath); this.resultingPath = this.combine(this.currentPath);
this.running = false; this.loop.stopLoop();
}; };
cancel = async () => { cancel = async () => {
this.resultingPath = this.startingPath; this.resultingPath = this.startingPath;
this.running = false; this.loop.stopLoop();
}; };
} }