@eightshone/sshman
Version:
A simple cli ssh manager
76 lines • 3.53 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import search from "@inquirer/search";
import colors from "yoctocolors-cjs";
import stringPadding from "../../../utils/stringPadding.js";
import { inquirerTheme } from "../../../utils/themes.js";
const defaultChoice = [
{
name: "↩️ Servers list",
value: "ssh-list",
description: "Return to servers list",
},
{
name: colors.dim(` # ${stringPadding("Name")} Config`),
disabled: " ",
value: null,
},
];
const debounceFunc = () => { };
function searchConnections(servers) {
return __awaiter(this, void 0, void 0, function* () {
let options = null;
console.log("Search servers:");
let answer = yield search({
message: "Search for server",
source: (input_1, _a) => __awaiter(this, [input_1, _a], void 0, function* (input, { signal }) {
yield setTimeout(debounceFunc, 300);
if (signal.aborted)
return defaultChoice;
let filteredServers = (input === null || input === void 0 ? void 0 : input.length)
? servers
.map((srv, index) => (Object.assign(Object.assign({}, srv), { index })))
.filter((srv) => srv.name.includes(input) ||
srv.host.includes(input) ||
srv.username.includes(input) ||
`${srv.port}`.includes(input))
.map((srv) => ({
name: `${stringPadding(`${srv.index + 1}`, 3, "start", "0")} ${stringPadding(srv.name)} ${srv.username}:[redacted]@${srv.host}:${srv.port}`,
value: `ssh-display#-_-#${JSON.stringify(srv)}#-_-#${srv.index}`,
description: "Connect to server",
}))
: [];
return [
...defaultChoice,
...(filteredServers.length
? filteredServers
: [
{
name: (input === null || input === void 0 ? void 0 : input.length)
? "📭 No server matched your search results"
: "⌨️ Start typing to show results matching your search",
value: null,
disabled: " ",
},
]),
];
}),
theme: inquirerTheme,
pageSize: 10,
});
if (answer.startsWith("ssh-display")) {
[answer, ...options] = answer.split("#-_-#");
}
console.clear();
return [answer, options];
});
}
export default searchConnections;
//# sourceMappingURL=search.js.map