promptify.ts
Version:
A easy and modern npm package that controls users inputs to prompt the user in cmd.
1,218 lines (1,204 loc) • 46.9 kB
JavaScript
;
var readline7 = require('readline');
var path = require('path');
var promises = require('fs/promises');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var readline7__default = /*#__PURE__*/_interopDefault(readline7);
var path__default = /*#__PURE__*/_interopDefault(path);
// src/packages/input/src/input.ts
// src/packages/input/enums.ts
var Design = /* @__PURE__ */ ((Design2) => {
Design2[Design2["Simple"] = 0] = "Simple";
Design2[Design2["Modern"] = 1] = "Modern";
Design2[Design2["Colorful"] = 2] = "Colorful";
return Design2;
})(Design || {});
// src/packages/input/variables.ts
var Colors = {
reset: "\x1B[0m",
foreground: {
black: "\x1B[30m",
cyan: "\x1B[36m",
yellow: "\x1B[33m",
green: "\x1B[32m",
gray: "\x1B[90m",
white: "\x1B[37m",
red: "\x1B[31m"
},
background: {
gray: "\x1B[100m",
white: "\x1B[47"
}
};
// src/packages/input/src/designer.ts
var Designer = class {
design;
logger;
v;
q;
colors = Colors;
constructor(design, logger, v, q) {
this.design = design;
this.logger = logger;
this.v = v;
this.q = q;
}
log_header() {
switch (this.design.header) {
case 1 /* Modern */:
const questionText = `\u{1F680} ${this.q}`;
const padding = Math.max(43 - questionText.length, 0);
const paddingLeft = Math.floor(padding / 2);
const paddingRight = padding - paddingLeft;
const text = "\xA0".repeat(paddingLeft) + this.colors.foreground.cyan + questionText + this.colors.reset + "\xA0".repeat(paddingRight);
this.logger.print(" ");
this.logger.print(
`${this.design.colors.box_color}\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510`
);
this.logger.print(
`${this.design.colors.box_color}\u2502${"\xA0".repeat(43)}\u2502${this.design.colors.shadow_color}\u2591`
);
this.logger.print(
`${this.design.colors.box_color}\u2502${"\xA0".repeat(43)}\u2502${this.design.colors.shadow_color}\u2591`
);
this.logger.print(
`${this.design.colors.box_color}\u2502${this.colors.reset}${text}${this.design.colors.box_color}\u2502${this.colors.reset}${this.design.colors.shadow_color}\u2591`
);
this.logger.print(
`${this.design.colors.box_color}\u2502${"\xA0".repeat(43)}\u2502${this.design.colors.shadow_color}\u2591`
);
this.logger.print(
`${this.design.colors.box_color}\u2502 ${this.colors.reset}${this.colors.foreground.white}V${this.v} ${this.design.colors.box_color}\u2502${this.design.colors.shadow_color}\u2591`
);
this.logger.print(
`${this.design.colors.box_color}\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518${this.design.colors.shadow_color}\u2591`
);
this.logger.print(
`${this.design.colors.shadow_color} \u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591${this.colors.reset}`
);
this.logger.print(" ");
this.logger.print(" ");
break;
}
}
async get_current_position() {
return new Promise((resolve) => {
const termcodes = { cursorGetPosition: "\x1B[6n" };
if (process.stdin.isTTY) {
process.stdin.setEncoding("utf8");
process.stdin.setRawMode(true);
}
const readfx = function() {
const buf = process.stdin.read();
const str = JSON.stringify(buf);
const regex = /\[(.*)/g;
const reg = regex.exec(str);
if (reg) {
const xy = reg[0].replace(/\[|R"/g, "").split(";");
const pos = { y: xy[0], x: xy[1] };
if (process.stdin.isTTY) {
process.stdin.setRawMode(false);
}
resolve(pos);
}
};
process.stdin.once("readable", readfx);
process.stdout.write(termcodes.cursorGetPosition);
});
}
};
// src/packages/logger/src/logger.ts
var Logger = class {
colors = {
cyan: "\x1B[36m",
yellow: "\x1B[33m",
green: "\x1B[32m",
reset: "\x1B[0m"
};
constructor() {
}
print(message, updateable = false) {
try {
process.stdout.write(`\x1B[30m\x1B[46m\u2B2A${this.colors.reset} ${message}${updateable ? "" : "\n"}`);
} catch (error) {
console.error("Error loading SVG:", error);
}
}
test() {
const q = "Test";
const questionText = `\u{1F680} ${q}`;
const padding = Math.max(43 - questionText.length, 0);
const paddingLeft = Math.floor(padding / 2);
const paddingRight = padding - paddingLeft;
const text = "\xA0".repeat(paddingLeft) + this.colors.cyan + questionText + this.colors.reset + "\xA0".repeat(paddingRight);
this.print("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510");
this.print(`\u2502${"\xA0".repeat(43)}\u2502\u2591`);
this.print(`\u2502${"\xA0".repeat(43)}\u2502\u2591`);
this.print(`\u2502${text}\u2502\u2591`);
this.print(`\u2502${"\xA0".repeat(43)}\u2502\u2591`);
this.print(`\u2502 V0.0.1 \u2502\u2591`);
this.print("\x1B[90m\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2591" + this.colors.reset);
this.print("\x1B[90m \u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591\u2591" + this.colors.reset);
}
};
var Filesystem = class {
extensions;
showHiddenFolders;
showFiles;
constructor(extensions, showHiddenFolders = false, showFiles = true) {
this.extensions = extensions.split(",");
this.showHiddenFolders = showHiddenFolders;
this.showFiles = showFiles;
}
getPath(location) {
return path__default.default.resolve(location);
}
getFile(location, name) {
return path__default.default.resolve(path__default.default.join(location, name));
}
async getContentByLocation(location) {
let content = [];
let items = await promises.readdir(this.getPath(location));
for await (const item of items) {
if (item.startsWith(".") || !item.includes(".")) {
if (this.showHiddenFolders && item.startsWith(".")) content.push({ type: "folder", name: item });
else if (!this.showHiddenFolders && !item.startsWith(".")) content.push({ type: "folder", name: item });
} else if (this.showFiles && item.includes(".")) {
if (this.extensions[0] !== "*") {
if (item.split(".").length > 1) {
const fileExtensions = item.split(".").slice(1);
for await (const fe of fileExtensions) {
if (this.extensions.includes("." + fe)) {
content.push({ type: "file", name: item });
break;
}
}
} else {
if (this.extensions.includes("." + item.split(".")[1])) {
content.push({ type: "file", name: item });
}
}
} else {
content.push({ type: "file", name: item });
}
}
}
return content.sort((a, b) => (a.type === "file" ? 1 : 0) - (b.type === "file" ? 1 : 0));
}
back(location) {
return path__default.default.resolve(path__default.default.join(location, ".."));
}
go(location, item) {
return path__default.default.resolve(path__default.default.join(location, item));
}
};
// src/packages/regex/variables.ts
var REGEX = {
url: {
any: /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)$/,
http: /^http:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)$/,
https: /^https:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)$/
},
letter: /^[a-zA-Z]$/,
number: /^[0-9]$/,
symbol: /^[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]$/
};
// src/packages/keypress/src/index.ts
function format(text, toFormat, data = {}) {
if (toFormat === "json") {
if (data) data.answer = text;
return data;
} else if (toFormat === "text") {
return text;
}
return "";
}
function isLetter(text) {
return REGEX.letter.test(text);
}
function isNumber(text) {
return REGEX.number.test(text);
}
function isSymbol(text) {
return REGEX.symbol.test(text);
}
function isUrl(text, protocol) {
let val = false;
switch (protocol) {
case "http":
val = REGEX.url.http.test(text);
break;
case "https":
val = REGEX.url.https.test(text);
break;
case "http(s)":
val = REGEX.url.any.test(text);
break;
}
return val;
}
var InputPrompt = class {
input = [];
settings;
resolve;
constructor(settings, resolve) {
this.settings = settings;
this.resolve = resolve;
this.handleKeypress = this.handleKeypress.bind(this);
}
async handleKeypress(chunk, key) {
if (!this.settings.format) return;
if (!key) return;
if (key.name === "c" && key.ctrl) process.exit();
else if (key.name === "return") {
if (this.settings.required && this.input.length == 0) return;
readline7__default.default.cursorTo(process.stdout, 12);
console.log(
Colors.foreground.cyan + this.input.join("") + Colors.reset + "_".repeat(40 - this.input.length)
);
await readline7__default.default.cursorTo(process.stdout, 0);
process.stdin.removeListener("keypress", this.handleKeypress);
readline7__default.default.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
await this.resolve(format(this.input.join(""), this.settings.format));
} else {
if (key.name === "backspace") {
this.input.pop();
} else if (key.name === "space") this.input.push(" ");
else {
if (this.settings.type === "text" && (key.name ? key.name.length == 1 : key.sequence.length == 1) && (isLetter(key.name) || isNumber(key.name) || (key.sequence ? isSymbol(key.sequence) : false))) {
this.input.push(
key.sequence ? key.sequence : key.shift ? key.name.toUpperCase() : key.name
);
} else if (this.settings.type === "number" && isNumber(key.name)) {
this.input.push(key.name);
} else if (this.settings.type === "word") {
console.log("Word");
}
}
readline7__default.default.cursorTo(process.stdout, 12 + this.input.length - 1);
process.stdout.write(
(this.input.length > 0 ? this.input[this.input.length - 1] : "") + " " + "_".repeat(39 - this.input.length)
);
readline7__default.default.cursorTo(process.stdout, 12 + this.input.length);
}
}
};
var InputPwd = class {
input = [];
showPwd = false;
settings;
resolve;
constructor(settings, resolve) {
this.settings = settings;
this.resolve = resolve;
this.handleKeypress = this.handleKeypress.bind(this);
}
async handleKeypress(chunk, key) {
if (!this.settings.format) return;
if (!key) return;
if (key.name === "c" && key.ctrl) process.exit();
else if (key.name === "return") {
if (this.settings.required && this.input.length == 0) return;
readline7__default.default.cursorTo(process.stdout, 18);
console.log(
Colors.foreground.cyan + (this.showPwd ? this.input.join("") : "*".repeat(this.input.length)) + Colors.reset + "_".repeat(40 - this.input.length)
);
await readline7__default.default.cursorTo(process.stdout, 0);
process.stdin.removeListener("keypress", this.handleKeypress);
readline7__default.default.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
await this.resolve(format(this.input.join(""), this.settings.format));
} else if (key.name === "space") {
this.showPwd = !this.showPwd;
readline7__default.default.cursorTo(process.stdout, 18);
process.stdout.write(
(this.showPwd ? this.input.join("") : "*".repeat(this.input.length)) + Colors.reset + "_".repeat(40 - this.input.length)
);
readline7__default.default.cursorTo(process.stdout, 18 + this.input.length);
} else {
if (key.name === "backspace") {
this.input.pop();
} else {
this.input.push(
key.sequence ? key.sequence : key.shift ? key.name.toUpperCase() : key.name
);
}
readline7__default.default.cursorTo(process.stdout, 18 + this.input.length - 1);
process.stdout.write(
(this.input.length > 0 ? this.showPwd ? this.input[this.input.length - 1] : "*" : "") + " " + "_".repeat(39 - this.input.length)
);
readline7__default.default.cursorTo(process.stdout, 18 + this.input.length);
}
}
};
var InputBool = class {
input = [];
selected_bool;
supported = [
"yes",
"no",
"ja",
"nein",
"true",
"false",
"y",
"n",
"j"
];
positive = ["yes", "ja", "true", "y", "j"];
settings;
resolve;
constructor(settings, resolve) {
this.settings = settings;
this.resolve = resolve;
this.handleKeypress = this.handleKeypress.bind(this);
if (!this.settings.default_bool) return;
this.selected_bool = this.settings.default_bool;
}
async handleKeypress(chunk, key) {
if (!this.settings.format || !this.settings.default_bool) return;
if (!key) return;
if (key.name === "c" && key.ctrl) process.exit();
else if (key.name === "return") {
if (this.settings.required && this.input.length == 0) this.selected_bool = this.settings.default_bool;
else if (!this.supported.includes(this.input.join(""))) return;
readline7__default.default.cursorTo(process.stdout, 15);
console.log(
Colors.foreground.cyan + `${this.input.length > 0 ? this.input.join("") : this.selected_bool ? "true" : "false"} ${Colors.reset}`
);
await readline7__default.default.cursorTo(process.stdout, 0);
process.stdin.removeListener("keypress", this.handleKeypress);
readline7__default.default.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
await this.resolve(
format(
this.input.length > 0 ? this.input.join("") : `${this.selected_bool ? "true" : "false"}`,
this.settings.format,
{
bool: this.input.length > 0 ? this.positive.includes(this.input.join("")) ? true : false : this.selected_bool
}
)
);
} else {
if (key.name === "backspace") {
this.input.pop();
} else {
this.input.push(
key.sequence ? key.sequence : key.shift ? key.name.toUpperCase() : key.name
);
}
readline7__default.default.cursorTo(process.stdout, 15 + this.input.length - 1);
process.stdout.write(
(this.input.length > 0 ? this.input[this.input.length - 1] : "") + "\xA0".repeat(40)
);
readline7__default.default.cursorTo(process.stdout, 15 + this.input.length);
}
}
};
// src/packages/keypress/src/handles/selection.ts
var InputSelection = class {
text = ``;
settings;
lastChoice = 0;
curChoice = 0;
logger = new Logger();
resolve;
constructor(settings, resolve) {
this.settings = settings;
this.resolve = resolve;
this.handleKeypressSingle = this.handleKeypressSingle.bind(this);
if (this.settings.type === "single") {
this.logger.print(
`${Colors.foreground.gray}[Press enter to choose selected choice or CTRL+C to exit]${Colors.reset}`
);
for (let i = 0; i < this.settings.choices.length; i++) {
if (this.curChoice == i)
this.text += ` \u2794 ${Colors.foreground.cyan}${this.settings.choices[i]}${Colors.reset}
`;
else
this.text += `${i > 0 ? `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ` : ""} - ${this.settings.choices[i]}
`;
}
this.logger.print(this.text, true);
this.text = ``;
}
}
async handleKeypressSingle(chunk, key) {
if (!this.settings.format) return;
if (!key) return;
if (key.name === "c" && key.ctrl) process.exit();
if (key.name === "return") {
this.text = `You selected: ${Colors.foreground.cyan}${this.settings.choices[this.curChoice]}${Colors.reset}${"\xA0".repeat(70)}
${`${"\xA0".repeat(
70
)}
`.repeat(1)}`;
process.stdout.clearLine(0);
process.stdout.write("\x1B[1A\x1B[1A");
this.logger.print(this.text);
process.stdin.removeListener("keypress", this.handleKeypressSingle);
await this.resolve(
format(this.settings.choices[this.curChoice], this.settings.format, {
choices: this.settings.choices,
index: this.curChoice
})
);
} else {
if (key.name === "up") {
if (this.curChoice != 0) {
this.lastChoice = this.curChoice;
this.curChoice -= 1;
}
} else if (key.name === "down") {
if (this.curChoice != this.settings.choices.length - 1) {
this.lastChoice = this.curChoice;
this.curChoice += 1;
}
}
this.text = ``;
for (let i = 0; i < this.settings.choices.length; i++) {
if (this.curChoice == i)
this.text += `${i > 0 ? `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ` : ""} \u2794 ${Colors.foreground.cyan}${this.settings.choices[i]}${Colors.reset}
`;
else
this.text += `${i > 0 ? `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ` : ""} - ${this.settings.choices[i]}
`;
}
process.stdout.clearLine(0);
process.stdout.write("\x1B[1A\x1B[1A");
this.logger.print(this.text, true);
this.text = ``;
}
}
};
var InputFiledialog = class {
text = ``;
settings;
lastChoice = 0;
curChoice = 0;
path = "";
showFiles = 6;
index = 0;
input = [];
logger = new Logger();
filesystem;
items = [];
resolve;
constructor(settings, resolve, filesystem) {
this.settings = settings;
this.resolve = resolve;
this.filesystem = filesystem;
if (!this.settings.startPath) return;
this.path = this.filesystem.getPath(this.settings.startPath);
this.text = `${Colors.foreground.gray}${this.path}${"\xA0".repeat(40)}
`;
this.handleKeypress = this.handleKeypress.bind(this);
}
async startup() {
this.items = await this.filesystem.getContentByLocation(this.path);
for (let i = 0; i < (this.items.length < this.showFiles ? this.items.length : this.showFiles); i++) {
if (i == 0)
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${Colors.foreground.cyan}${this.items[i].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i].name}${Colors.reset}
`;
else
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.items[i].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i].name}
`;
}
if (this.items.length < this.showFiles) {
if (this.items.length == 0) {
for (let i = 0; i < this.showFiles - this.items.length - 1; i++) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${"\xA0".repeat(
30
)}
`;
}
} else {
for (let i = 0; i < this.showFiles - this.items.length; i++) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${"\xA0".repeat(
30
)}
`;
}
}
}
this.logger.print(this.text + `\x1B[30m\x1B[46m\u2B2A${Colors.reset} `, true);
this.text = ``;
}
async handleKeypress(chunk, key) {
if (!this.settings.format) return;
if (!key) return;
if (key.name === "c" && key.ctrl) process.exit();
else if (key.name === "return") {
if (this.input.length > 0) {
this.path = this.filesystem.getPath(this.input.join(""));
this.input = [];
this.curChoice = 0;
this.items = await this.filesystem.getContentByLocation(this.path);
this.text = `${Colors.foreground.gray}${this.path}${"\xA0".repeat(40)}
`;
if (this.items.length == 0) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} No content found. ${Colors.foreground.gray}[Press backspace to go back]
`;
} else {
for (let i = 0; i < (this.items.length < this.showFiles ? this.items.length : this.showFiles); i++) {
if (i == 0)
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${Colors.foreground.cyan}${this.items[i].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i].name}${"\xA0".repeat(40)}${Colors.reset}
`;
else
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.items[i].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i].name}${"\xA0".repeat(40)}
`;
}
}
if (this.items.length < this.showFiles) {
if (this.items.length == 0) {
for (let i = 0; i < this.showFiles - this.items.length - 1; i++) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${"\xA0".repeat(
30
)}
`;
}
} else {
for (let i = 0; i < this.showFiles - this.items.length; i++) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${"\xA0".repeat(
30
)}
`;
}
}
}
process.stdout.clearLine(0);
process.stdout.write(
"\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A"
);
readline7__default.default.cursorTo(process.stdout, 0);
this.logger.print(
this.text + `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.input.join("")}`,
true
);
this.text = ``;
} else {
this.path = this.filesystem.go(this.path, this.items[this.curChoice].name);
this.text = ``;
this.text = `You selected: ${Colors.foreground.cyan}${this.path}${Colors.reset}${"\xA0".repeat(70)}
${`\x1B[30m\x1B[46m\u2B2A${Colors.reset}${"\xA0".repeat(70)}
`.repeat(6)}`;
process.stdout.clearLine(0);
process.stdout.write(
"\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A"
);
process.stdout.write(this.text);
process.stdout.clearLine(0);
process.stdout.write("\x1B[1A\x1B[1A");
process.stdin.removeListener("keypress", this.handleKeypress);
await this.resolve(format(this.path, this.settings.format));
}
} else if (key.name === "right") {
if (this.items[this.curChoice].type === "folder") {
this.path = this.filesystem.go(this.path, this.items[this.curChoice].name);
this.curChoice = 0;
this.items = await this.filesystem.getContentByLocation(this.path);
this.text = `${Colors.foreground.gray}${this.path}${"\xA0".repeat(40)}
`;
if (this.items.length == 0) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} No content found. ${Colors.foreground.gray}[Press backspace to go back]
`;
} else {
for (let i = 0; i < (this.items.length < this.showFiles ? this.items.length : this.showFiles); i++) {
if (i == 0)
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${Colors.foreground.cyan}${this.items[i].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i].name}${"\xA0".repeat(40)}${Colors.reset}
`;
else
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.items[i].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i].name}${"\xA0".repeat(40)}
`;
}
}
if (this.items.length < this.showFiles) {
if (this.items.length == 0) {
for (let i = 0; i < this.showFiles - this.items.length - 1; i++) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${"\xA0".repeat(
30
)}
`;
}
} else {
for (let i = 0; i < this.showFiles - this.items.length; i++) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${"\xA0".repeat(
30
)}
`;
}
}
}
process.stdout.clearLine(0);
process.stdout.write(
"\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A"
);
readline7__default.default.cursorTo(process.stdout, 0);
this.logger.print(
this.text + `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.input.join("")}`,
true
);
this.text = ``;
}
} else if (key.name === "left") {
this.path = this.filesystem.back(this.path);
this.curChoice = 0;
this.items = await this.filesystem.getContentByLocation(this.path);
this.text = `${Colors.foreground.gray}${this.path}${"\xA0".repeat(40)}
`;
if (this.items.length == 0) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} No content found. ${Colors.foreground.gray}[Press backspace to go back]
`;
} else {
for (let i = 0; i < (this.items.length < this.showFiles ? this.items.length : this.showFiles); i++) {
if (i == 0)
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${Colors.foreground.cyan}${this.items[i].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i].name}${"\xA0".repeat(40)}${Colors.reset}
`;
else
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.items[i].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i].name}${"\xA0".repeat(40)}
`;
}
}
if (this.items.length < this.showFiles) {
for (let i = 0; i < this.showFiles - this.items.length; i++) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${"\xA0".repeat(
30
)}
`;
}
}
process.stdout.clearLine(0);
process.stdout.write("\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A");
readline7__default.default.cursorTo(process.stdout, 0);
this.logger.print(
this.text + `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.input.join("")}`,
true
);
this.text = ``;
} else {
if (key.name ? key.name.length == 1 : key.sequence.length == 1) {
this.input.push(
key.sequence ? key.sequence : key.shift ? key.name.toUpperCase() : key.name
);
this.text = `${Colors.foreground.gray}${this.path}${"\xA0".repeat(40)}
`;
if (this.items.length == 0) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} No content found. ${Colors.foreground.gray}[Press backspace to go back]
`;
} else {
for (let i = 0; i < (this.items.length < this.showFiles ? this.items.length : this.showFiles); i++) {
if (i == 0)
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${Colors.foreground.cyan}${this.items[i].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i].name}${"\xA0".repeat(40)}${Colors.reset}
`;
else
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.items[i].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i].name}${"\xA0".repeat(40)}
`;
}
}
if (this.items.length < this.showFiles) {
for (let i = 0; i < this.showFiles - this.items.length; i++) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${"\xA0".repeat(
30
)}
`;
}
}
process.stdout.clearLine(0);
process.stdout.write(
"\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A"
);
readline7__default.default.cursorTo(process.stdout, 0);
this.logger.print(
this.text + `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.input.join("")}`,
true
);
this.text = ``;
} else if (key.name === "up") {
if (this.items.length == 0) return;
if (this.curChoice != 0) {
this.lastChoice = this.curChoice;
this.curChoice -= 1;
if (this.curChoice > this.showFiles) {
let offset = 5;
this.text = `${Colors.foreground.gray}${this.path}${"\xA0".repeat(
40
)}
`;
for (let i = 0; i < (this.items.length < this.showFiles ? this.items.length : this.showFiles); i++) {
if (this.curChoice == i + offset)
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${Colors.foreground.cyan}${this.items[i + offset].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i + offset].name}${"\xA0".repeat(40)}${Colors.reset}
`;
else
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.items[i + offset].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i + offset].name}${"\xA0".repeat(40)}
`;
}
process.stdout.clearLine(0);
process.stdout.write(
"\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A"
);
readline7__default.default.cursorTo(process.stdout, 0);
this.logger.print(
this.text + `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.input.join("")}`,
true
);
this.text = ``;
} else {
if (this.curChoice + 1 < this.showFiles) this.index -= 1;
let offset = this.curChoice + 1 > this.showFiles ? this.curChoice + 1 - this.showFiles : 0;
this.text = `${Colors.foreground.gray}${this.path}${"\xA0".repeat(
40
)}
`;
for (let i = 0; i < (this.items.length < this.showFiles ? this.items.length : this.showFiles); i++) {
if (this.curChoice == i + offset)
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${Colors.foreground.cyan}${this.items[i + offset].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i + offset].name}${"\xA0".repeat(40)}${Colors.reset}
`;
else
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.items[i + offset].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i + offset].name}${"\xA0".repeat(40)}
`;
}
if (this.items.length < this.showFiles) {
for (let i = 0; i < this.showFiles - this.items.length; i++) {
this.text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${"\xA0".repeat(30)}
`;
}
}
process.stdout.clearLine(0);
process.stdout.write(
"\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A"
);
readline7__default.default.cursorTo(process.stdout, 0);
this.logger.print(
this.text + `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.input.join("")}`,
true
);
this.text = ``;
}
}
} else if (key.name === "down") {
if (this.items.length == 0) return;
if (this.curChoice != this.items.length - 1) {
this.lastChoice = this.curChoice;
this.curChoice += 1;
if (this.curChoice < this.showFiles) this.index += 1;
let offset = this.curChoice + 1 > this.showFiles ? this.curChoice + 1 - this.showFiles : 0;
let text = `${Colors.foreground.gray}${this.path}${"\xA0".repeat(40)}
`;
for (let i = 0; i < (this.items.length < this.showFiles ? this.items.length : this.showFiles); i++) {
if (this.curChoice == i + offset)
text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${Colors.foreground.cyan}${this.items[i + offset].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i + offset].name}${"\xA0".repeat(40)}${Colors.reset}
`;
else
text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.items[i + offset].type === "folder" ? " \u{1F4C1}" : " "} ${this.items[i + offset].name}${"\xA0".repeat(40)}
`;
}
if (this.items.length < this.showFiles) {
for (let i = 0; i < this.showFiles - this.items.length; i++) {
text += `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${"\xA0".repeat(
30
)}
`;
}
}
process.stdout.clearLine(0);
process.stdout.write(
"\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A\x1B[1A"
);
readline7__default.default.cursorTo(process.stdout, 0);
this.logger.print(
text + `\x1B[30m\x1B[46m\u2B2A${Colors.reset} ${this.input.join("")}`,
true
);
text = ``;
}
}
}
}
};
var InputDate = class {
date = /* @__PURE__ */ new Date();
settings;
editMode = 0;
dateData = {
day: 0,
month: 0,
year: 0,
maxDay: 0,
maxYear: 3e3,
maxMonth: 12
};
resolve;
constructor(settings, resolve) {
this.settings = settings;
if (this.settings.startDate) {
this.date = new Date(this.settings.startDate);
this.dateData = {
day: this.date.getDate(),
month: this.date.getMonth(),
year: this.date.getFullYear(),
maxDay: new Date(
this.date.getFullYear(),
this.date.getMonth() + 1,
0
).getDate(),
maxMonth: 12,
maxYear: 3e3
};
}
this.resolve = resolve;
this.handleKeypress = this.handleKeypress.bind(this);
}
async handleKeypress(chunk, key) {
if (!this.settings.format) return;
if (!key) return;
if (key.name === "c" && key.ctrl) process.exit();
else if (key.name === "return") {
readline7__default.default.cursorTo(process.stdout, 7);
console.log(
" " + Colors.foreground.cyan + this.formatDate(this.date, 3) + Colors.reset
);
await readline7__default.default.cursorTo(process.stdout, 0);
process.stdin.removeListener("keypress", this.handleKeypress);
readline7__default.default.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
await this.resolve(
format(this.formatDate(this.date, 3), this.settings.format, {
date: this.date
})
);
} else {
if (key.name === "left" && this.editMode > 0) {
this.editMode -= 1;
await this.print();
} else if (key.name === "right" && this.editMode < 2) {
this.editMode += 1;
await this.print();
} else if (key.name === "up") {
switch (this.editMode) {
case 0:
if (this.dateData.day < this.dateData.maxDay)
this.dateData.day += 1;
await this.print();
break;
case 1:
if (this.dateData.month < this.dateData.maxMonth - 1) {
this.dateData.month += 1;
this.dateData.maxDay = new Date(
this.dateData.year,
this.dateData.month + 1,
0
).getDate();
}
await this.print();
break;
case 2:
if (this.dateData.year < this.dateData.maxYear)
this.dateData.year += 1;
await this.print();
break;
}
} else if (key.name === "down") {
switch (this.editMode) {
case 0:
if (this.dateData.day > 1) this.dateData.day -= 1;
await this.print();
break;
case 1:
if (this.dateData.month > 0) {
this.dateData.month -= 1;
this.dateData.maxDay = new Date(
this.dateData.year,
this.dateData.month + 1,
0
).getDate();
}
await this.print();
break;
case 2:
if (this.dateData.year > 1) this.dateData.year -= 1;
await this.print();
break;
}
}
}
}
print() {
this.date.setFullYear(this.dateData.year);
this.date.setMonth(this.dateData.month);
this.date.setDate(this.dateData.day);
readline7__default.default.cursorTo(process.stdout, 7);
process.stdout.write(
" " + this.formatDate(this.date, this.editMode) + "\xA0".repeat(40)
);
readline7__default.default.cursorTo(process.stdout, 7);
}
formatDate(date, editMode) {
return editMode == 3 ? `${date.getDate()}.${date.getMonth()}.${date.getFullYear()}` : `${editMode == 0 ? `${Colors.foreground.cyan}` : ``}${date.getDate()}${Colors.reset}.${editMode == 1 ? `${Colors.foreground.cyan}` : ``}${date.getMonth() + 1}${Colors.reset}.${editMode == 2 ? `${Colors.foreground.cyan}` : ``}${date.getFullYear()}${Colors.reset}`;
}
};
var UrlPrompt = class {
input = [];
settings;
resolve;
error = false;
constructor(settings, resolve) {
this.settings = settings;
this.resolve = resolve;
this.handleKeypress = this.handleKeypress.bind(this);
}
async validate(text) {
const val = await isUrl(text, this.settings.protocol ? this.settings.protocol : "http(s)");
if (!val) {
this.error = true;
readline7__default.default.cursorTo(process.stdout, 12);
process.stdout.write(
Colors.foreground.red + this.input.join("") + " (Url invalid)" + Colors.reset
);
readline7__default.default.cursorTo(process.stdout, 12 + this.input.length);
}
return val;
}
async handleKeypress(chunk, key) {
if (!this.settings.format) return;
if (!key) return;
if (key.name === "c" && key.ctrl) process.exit();
else if (key.name === "return") {
const val = await this.validate(this.input.join(""));
if (this.settings.required && this.input.length == 0) return;
if (!val || this.error) return;
readline7__default.default.cursorTo(process.stdout, 12);
console.log(
Colors.foreground.cyan + this.input.join("") + Colors.reset + " ".repeat(40 - this.input.length)
);
await readline7__default.default.cursorTo(process.stdout, 0);
process.stdin.removeListener("keypress", this.handleKeypress);
readline7__default.default.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
await this.resolve(format(this.input.join(""), this.settings.format));
} else {
if (key.name === "backspace") {
this.input.pop();
} else if (key.name === "space") this.input.push(" ");
else {
if ((key.name ? key.name.length == 1 : key.sequence.length == 1) && (isLetter(key.name) || isNumber(key.name) || (key.sequence ? isSymbol(key.sequence) : false))) {
this.input.push(
key.sequence ? key.sequence : key.shift ? key.name.toUpperCase() : key.name
);
this.error = false;
}
}
readline7__default.default.cursorTo(process.stdout, 12 + this.input.length - 1);
process.stdout.write(
(this.input.length > 0 ? this.input[this.input.length - 1] : "") + " " + " ".repeat(39 - this.input.length)
);
readline7__default.default.cursorTo(process.stdout, 12 + this.input.length);
}
}
};
// src/packages/input/src/input.ts
var Input = class {
VERSION = "0.5.1";
AUTHOR = "OGMatrix";
ID = "";
logger = new Logger();
constructor() {
}
async bool({
q,
required = true,
default_bool = true,
format: format2 = "json",
design = {
header: 1 /* Modern */,
body: 1 /* Modern */,
colors: {
box_color: Colors.foreground.white,
shadow_color: Colors.foreground.gray
}
}
}) {
const designer = new Designer(design, this.logger, this.VERSION, q);
designer.log_header();
this.logger.print(
`[${default_bool ? "Y" : "y"}/${default_bool ? "n" : "N"}]? `,
true
);
readline7__default.default.cursorTo(process.stdout, 15);
readline7__default.default.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
return new Promise(async (resolve, reject) => {
const handle = new InputBool({ q, required, default_bool, format: format2, design }, resolve);
process.stdin.on("keypress", handle.handleKeypress);
});
}
async pwd({
q,
required = true,
format: format2 = "json",
design = {
header: 1 /* Modern */,
body: 1 /* Modern */,
colors: {
box_color: Colors.foreground.white,
shadow_color: Colors.foreground.gray
}
}
}) {
const designer = new Designer(design, this.logger, this.VERSION, q);
designer.log_header();
this.logger.print(
`${Colors.foreground.gray}[Press space to show/hide password]`
);
this.logger.print(`Password: ${"_".repeat(40)}`, true);
readline7__default.default.cursorTo(process.stdout, 18);
readline7__default.default.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
return new Promise(async (resolve, reject) => {
const handle = new InputPwd({ q, required, format: format2, design }, resolve);
process.stdin.on("keypress", handle.handleKeypress);
});
}
async date({
q,
startDate = /* @__PURE__ */ new Date(),
format: format2 = "json",
design = {
header: 1 /* Modern */,
body: 1 /* Modern */,
colors: {
box_color: Colors.foreground.white,
shadow_color: Colors.foreground.gray
}
}
}) {
const designer = new Designer(design, this.logger, this.VERSION, q);
designer.log_header();
this.logger.print(
`${Colors.foreground.gray}[\u2190 go left] [\u2192 go right] [\u2191 increase] [\u2193 decrease] [press enter to select] [CTRL+C to exit]${Colors.reset}`
);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
return new Promise(async (resolve, reject) => {
const handle = new InputDate({ q, startDate, format: format2, design }, resolve);
this.logger.print(`${handle.formatDate(startDate, 0)}`, true);
readline7__default.default.cursorTo(process.stdout, 7);
readline7__default.default.emitKeypressEvents(process.stdin);
process.stdin.on("keypress", handle.handleKeypress);
});
}
async prompt({
type,
q,
required = true,
format: format2 = "json",
design = {
header: 1 /* Modern */,
body: 1 /* Modern */,
colors: {
box_color: Colors.foreground.white,
shadow_color: Colors.foreground.gray
}
}
}) {
const designer = new Designer(design, this.logger, this.VERSION, q);
designer.log_header();
this.logger.print(`\xA0\xA0? ${"_".repeat(40)}`, true);
readline7__default.default.cursorTo(process.stdout, 12);
readline7__default.default.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
return new Promise(async (resolve, reject) => {
const handle = new InputPrompt({ type, q, required, format: format2, design }, resolve);
process.stdin.on("keypress", handle.handleKeypress);
});
}
async selection({
type,
choices,
q,
format: format2 = "json",
design = {
header: 1 /* Modern */,
body: 1 /* Modern */,
colors: {
box_color: Colors.foreground.white,
shadow_color: Colors.foreground.gray
}
}
}) {
const designer = new Designer(design, this.logger, this.VERSION, q);
designer.log_header();
await designer.get_current_position();
readline7__default.default.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
return new Promise(async (resolve, reject) => {
const handle = new InputSelection({ type, choices, q, format: format2, design }, resolve);
if (type === "single") {
process.stdin.on("keypress", handle.handleKeypressSingle);
}
});
}
async filedialog({
type,
q,
startPath = __dirname,
extensions = "*",
showHiddenFolders = false,
format: format2 = "json",
design = {
header: 1 /* Modern */,
body: 1 /* Modern */,
colors: {
box_color: Colors.foreground.white,
shadow_color: Colors.foreground.gray
}
}
}) {
const designer = new Designer(design, this.logger, this.VERSION, q);
const filesystem = new Filesystem(
extensions,
showHiddenFolders,
type === "file" ? true : false
);
designer.log_header();
readline7__default.default.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
return new Promise(async (resolve, reject) => {
this.logger.print(
`${Colors.foreground.gray}[\u2190 go back] [\u2192 go into folder] [\u2191 go up] [\u2193 go down] [paste path and press enter] [press enter to select] [CTRL+C to exit]${Colors.reset}`
);
const handle = new InputFiledialog({ type, q, startPath, extensions, showHiddenFolders, format: format2, design }, resolve, filesystem);
await handle.startup();
process.stdin.on("keypress", handle.handleKeypress);
});
}
async url({
protocol = "http(s)",
q,
required = true,
format: format2 = "json",
design = {
header: 1 /* Modern */,
body: 1 /* Modern */,
colors: {
box_color: Colors.foreground.white,
shadow_color: Colors.foreground.gray
}
}
}) {
const designer = new Designer(design, this.logger, this.VERSION, q);
designer.log_header();
this.logger.print(`\xA0\xA0? ${" ".repeat(40)}`, true);
readline7__default.default.cursorTo(process.stdout, 12);
readline7__default.default.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
return new Promise(async (resolve, reject) => {
const handle = new UrlPrompt({ protocol, q, required, format: format2, design }, resolve);
process.stdin.on("keypress", handle.handleKeypress);
});
}
};
exports.Colors = Colors;
exports.Design = Design;
exports.Filesystem = Filesystem;
exports.Input = Input;
exports.Logger = Logger;