heyduck 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +23 -0
  2. package/script.js +85 -0
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "heyduck",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "bin": {
11
+ "heyduck": "./script.js"
12
+ },
13
+ "keywords": [],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "packageManager": "pnpm@10.30.3",
17
+ "dependencies": {
18
+ "axios": "^1.13.6",
19
+ "chalk": "^5.6.2",
20
+ "cheerio": "^1.2.0",
21
+ "wrap-ansi": "^10.0.0"
22
+ }
23
+ }
package/script.js ADDED
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env node
2
+
3
+ import axios from "axios";
4
+ import * as cheerio from "cheerio";
5
+ import chalk from "chalk";
6
+ import wrapAnsi from "wrap-ansi";
7
+
8
+ const AXIOS_CFG = { headers: { "User-Agent": "Mozilla/5.0" }, timeout: 8000 };
9
+
10
+ async function searchMetaTUI(keyword, max = 10, mode = "chalk") {
11
+ const { data } = await axios.get(`https://duckduckgo.com/html/?q=${encodeURIComponent(keyword)}`, AXIOS_CFG);
12
+
13
+ const $ = cheerio.load(data);
14
+ const results = [];
15
+
16
+ $(".result").each((_, el) => {
17
+ const title = $(el).find(".result__a").text().trim();
18
+ const rawHref = $(el).find(".result__a").attr("href");
19
+
20
+ let url = null;
21
+ if (rawHref?.includes("uddg=")) {
22
+ const u = new URL("https:" + rawHref);
23
+ url = decodeURIComponent(u.searchParams.get("uddg"));
24
+ }
25
+
26
+ const description = $(el).find(".result__snippet").text().trim();
27
+
28
+ if (title && url) {
29
+ results.push({ title, url, description });
30
+ }
31
+ });
32
+
33
+ const terminalWidth = process.stdout.columns || 80;
34
+ const paddingLeft = 4;
35
+ const paddingRight = 4;
36
+
37
+ function padAndWrap(text) {
38
+ const innerWidth = terminalWidth - paddingLeft - paddingRight;
39
+ const wrapped = wrapAnsi(text, innerWidth, { hard: true });
40
+ const pad = " ".repeat(paddingLeft);
41
+ return wrapped.split("\n").map(line => pad + line + " ".repeat(paddingRight)).join("\n");
42
+ }
43
+
44
+ const slicedResults = results.slice(0, max);
45
+
46
+ if (mode === "json") {
47
+ console.log(JSON.stringify(slicedResults, null, 2));
48
+ return;
49
+ }
50
+
51
+ console.log("\n");
52
+
53
+ slicedResults.forEach((r, i) => {
54
+ if (mode === "text") {
55
+ console.log(`${r.title}\n${r.url}\n${r.description}\n`);
56
+ } else {
57
+ const mainTitle = `${chalk.green.bold((i + 1).toString().padStart(2, "0"))}. ${chalk.yellow.bold(r.title)} [${chalk.blue.bold(r.url)}]`;
58
+ console.log(padAndWrap(mainTitle));
59
+ console.log(padAndWrap(r.description));
60
+ console.log("\n");
61
+ }
62
+ });
63
+ }
64
+
65
+ const args = process.argv.slice(2);
66
+
67
+ // Determine mode
68
+ const mode = args.includes("-j") ? "json" : args.includes("-t") ? "text" : "chalk";
69
+
70
+ // Determine number of results with -r flag
71
+ let maxResults = 10;
72
+ const rIndex = args.indexOf("-r");
73
+ if (rIndex !== -1 && args[rIndex + 1] && !isNaN(args[rIndex + 1])) {
74
+ maxResults = parseInt(args[rIndex + 1], 10);
75
+ }
76
+
77
+ // Combine all other args as keyword
78
+ const keyword = args.filter(arg => !arg.startsWith("-") && arg !== args[rIndex + 1]).join(" ");
79
+
80
+ if (!keyword) {
81
+ console.log("Usage: node script.js <search keyword> [-j|-t] [-r number]");
82
+ process.exit(1);
83
+ }
84
+
85
+ searchMetaTUI(keyword, maxResults, mode);