From 7b4a996d56225e1a90b9359f1fd21b18d108a0ba Mon Sep 17 00:00:00 2001 From: Tomofumi Chiba Date: Fri, 15 Dec 2023 19:23:02 +0900 Subject: [PATCH] fix --- cli.ts | 75 +++++++++++++++++++++++++++++----------- routes/api/cli/search.ts | 2 +- 2 files changed, 56 insertions(+), 21 deletions(-) diff --git a/cli.ts b/cli.ts index 16ad16c..ff02e9a 100755 --- a/cli.ts +++ b/cli.ts @@ -4,35 +4,60 @@ import { Input } from "https://deno.land/x/cliffy@v1.0.0-rc.3/prompt/input.ts"; import { Select } from "https://deno.land/x/cliffy@v1.0.0-rc.3/prompt/select.ts"; import { clearScreen, + colors, } from "https://deno.land/x/cliffy@v1.0.0-rc.3/ansi/mod.ts"; -console.log(clearScreen); -const word = await Input.prompt("Search"); -if (!word) { - Deno.exit(); -} - +const COMMAND_OPTIONS = [{ value: -1, name: ":Back" }, { + value: -2, + name: ":Exit", +}]; +const keys = { next: ["j", "down"], previous: ["k", "up"] }; const API_PATH = Deno.args.at(0) === "dev" ? "http://localhost:8000/api/cli" : "https://leaves.deno.dev/api/cli"; -const url = new URL(`${API_PATH}/search`); -url.searchParams.set("q", word); +let list: Array<{ value: number; name: string }> = []; +let searchSkip = false; +let word = ""; -const res = await fetch(url); -const options: any[] = await res.json(); -if (options.length === 0) { - console.log("Not Found"); - Deno.exit(); -} +while (true) { + console.log(clearScreen); + if (!searchSkip) { + word = await Input.prompt({ message: "Search", suggestions: [word] }); + if (!word) { + Deno.exit(); + } + const url = new URL(`${API_PATH}/search`); + url.searchParams.set("q", word); + const res = await fetch(url); + list = await res.json() as Array<{ name: string; value: number }>; + if (list.length === 0) { + console.log("Not Found"); + Deno.exit(); + } + if (list.length >= 10) { + console.log( + colors.bold.yellow( + "More than 10 posts hits were found. Let's add search words!", + ), + ); + } + } + + const postId: any = await Select.prompt({ // 型推論がおかしい + message: "Select", + maxRows: 12, + options: list.concat(COMMAND_OPTIONS), + keys, + }); -const postId = await Select.prompt({ - message: "Select", - options, - keys: { next: ["j", "down"], previous: ["k", "up"] }, -}); + if (postId === -1) { + searchSkip = false; + continue; + } else if (postId === -2) { + Deno.exit(0); + } -{ const res = await fetch(`${API_PATH}/posts/${postId}`); const json = await res.json(); console.log( @@ -43,4 +68,14 @@ const postId = await Select.prompt({ "---------------------------------------------------------------------------------", ); console.log(`https://leaves.deno.dev/posts/${postId}`); + const option: any = await Select.prompt({ + message: "Select", + options: COMMAND_OPTIONS, + keys, + }); + if (option === -2) { + Deno.exit(0); + } else if (option === -1) { + searchSkip = true; + } } diff --git a/routes/api/cli/search.ts b/routes/api/cli/search.ts index b6ff883..65e2339 100644 --- a/routes/api/cli/search.ts +++ b/routes/api/cli/search.ts @@ -13,7 +13,7 @@ export default defineRoute(async (req, _ctx) => { loginUserId: null, }); const body = rows.map((row) => { - return { value: row.id, name: getTitle(row.source) }; + return { value: row.id, name: "* " + getTitle(row.source) }; }); return Response.json(body); });