Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chibat committed Dec 15, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 79b7f8b commit 7b4a996
Showing 2 changed files with 56 additions and 21 deletions.
75 changes: 55 additions & 20 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion routes/api/cli/search.ts
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 7b4a996

Please sign in to comment.