-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
88 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,33 +2,62 @@ | |
|
||
import { Input } from "https://deno.land/x/[email protected]/prompt/input.ts"; | ||
import { Select } from "https://deno.land/x/[email protected]/prompt/select.ts"; | ||
import { | ||
clearScreen, | ||
colors, | ||
} from "https://deno.land/x/[email protected]/ansi/mod.ts"; | ||
|
||
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"], previous: ["k"] }, | ||
}); | ||
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( | ||
|
@@ -39,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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,3 +89,7 @@ iframe { | |
margin-bottom: 0; | ||
} | ||
|
||
.post h1, h2 { | ||
margin-bottom: 16px; | ||
} | ||
|