UNPKG

@s-hiroshi/bks

Version:

Cli bookmarks application

68 lines 2.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExpandedFinder = void 0; const inquirer = require("inquirer"); const inquirerPrompt = require("inquirer-autocomplete-prompt"); const fuzzy = require("fuzzy"); const createChoices_1 = require("../service/createChoices"); class ExpandedFinder { constructor(reader) { this.reader = reader; this.controlCharactor = "efind"; const items = reader.readAll(); const contents = items.map((item, index) => { return `keyword: ${item.keyword}\n url: ${item.content}`; }); this.choices = (0, createChoices_1.createChoices)(contents, ["Exit"]); } getControlCharactor() { return this.controlCharactor; } find() { // thisを束縛するためにアロー関数を使用 return (answers, input = "") => { return new Promise((resolve) => { setTimeout(() => { const results = fuzzy .filter(input, this.choices) .map((el) => el.original); results.splice(5, 0, new inquirer.Separator()); results.push(new inquirer.Separator()); resolve(results); }, Math.random() * 470 + 30); }); }; } async execute() { // @see https://github.com/mokkabonna/inquirer-autocomplete-prompt/blob/master/example.js inquirer.registerPrompt("autocomplete", inquirerPrompt); if (this.choices) { const choiced = await inquirer .prompt([ { type: "autocomplete", name: "context", suggestOnly: false, searchText: "Finding...", emptyText: "Nothing found!", message: "Input keyword", source: this.find(), loop: false, }, ]) .then((answer) => { return answer.context; }); if (choiced !== "Exit") { return this.getChoicedItemUrl(choiced); } } } getChoicedItemUrl(choiced) { const choice = choiced.split("\n"); const url = choice[1].trim().split(" ")[1]; return url; } } exports.ExpandedFinder = ExpandedFinder; //# sourceMappingURL=ExpandedFinder.js.map