Skip to content

Commit a4a2a66

Browse files
committed
fix: type commands
1 parent 5cad863 commit a4a2a66

2 files changed

Lines changed: 92 additions & 2 deletions

File tree

src/commands/search.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,57 @@ const format_markdown = (data: Search_response, query: string): string=>{
120120
return lines.join('\n');
121121
};
122122

123+
const print_news_table = (data: Search_response)=>{
124+
const news = data.news ?? [];
125+
if (!news.length)
126+
{
127+
console.log(dim('No news results found.'));
128+
return;
129+
}
130+
const rows = news.map(r=>({
131+
rank: String(r.global_rank ?? ''),
132+
title: (r.title ?? '').slice(0, 50),
133+
source: (r.source ?? '').slice(0, 20),
134+
date: (r.date ?? '').slice(0, 20),
135+
url: (r.link ?? '').slice(0, 60),
136+
}));
137+
print_table(rows, ['rank', 'title', 'source', 'date', 'url']);
138+
};
139+
140+
const print_shopping_table = (data: Search_response)=>{
141+
const shopping = data.shopping ?? [];
142+
if (!shopping.length)
143+
{
144+
console.log(dim('No shopping results found.'));
145+
return;
146+
}
147+
const rows = shopping.map(r=>({
148+
rank: String(r.rank ?? ''),
149+
title: (r.title ?? '').slice(0, 40),
150+
price: (r.price ?? ''),
151+
shop: (r.shop ?? '').slice(0, 20),
152+
rating: r.rating ? String(r.rating) : '',
153+
url: (r.link ?? '').slice(0, 60),
154+
}));
155+
print_table(rows, ['rank', 'title', 'price', 'shop', 'rating', 'url']);
156+
};
157+
158+
const print_images_table = (data: Search_response)=>{
159+
const images = data.images ?? [];
160+
if (!images.length)
161+
{
162+
console.log(dim('No image results found.'));
163+
return;
164+
}
165+
const rows = images.map((r, i)=>({
166+
'#': String(i+1),
167+
title: (r.title ?? '').slice(0, 40),
168+
source: (r.source ?? '').slice(0, 20),
169+
image: (r.original_image ?? '').slice(0, 60),
170+
}));
171+
print_table(rows, ['#', 'title', 'source', 'image']);
172+
};
173+
123174
const print_google_table = (data: Search_response)=>{
124175
const organic = data.organic ?? [];
125176
if (!organic.length)
@@ -192,7 +243,14 @@ const handle_search = async(query: string, opts: Search_opts)=>{
192243
print(result, print_opts);
193244
return;
194245
}
195-
print_google_table(result as Search_response);
246+
if (opts.type == 'news')
247+
print_news_table(result as Search_response);
248+
else if (opts.type == 'images')
249+
print_images_table(result as Search_response);
250+
else if (opts.type == 'shopping')
251+
print_shopping_table(result as Search_response);
252+
else
253+
print_google_table(result as Search_response);
196254
} catch(e) {
197255
spinner.stop();
198256
console.error((e as Error).message);

src/types/search.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,35 @@ type Organic_result = {
1212
description?: string;
1313
};
1414

15+
type News_result = {
16+
link: string;
17+
title: string;
18+
source?: string;
19+
source_logo?: string;
20+
date?: string;
21+
image?: string;
22+
global_rank?: number;
23+
};
24+
25+
type Image_result = {
26+
link: string;
27+
title: string;
28+
source?: string;
29+
original_image?: string;
30+
image?: string;
31+
};
32+
33+
type Shopping_result = {
34+
title: string;
35+
link: string;
36+
price?: string;
37+
shop?: string;
38+
rating?: number;
39+
reviews_cnt?: number;
40+
rank?: number;
41+
global_rank?: number;
42+
};
43+
1544
type Search_general = {
1645
search_engine: string;
1746
query: string;
@@ -29,7 +58,9 @@ type Search_response = {
2958
people_also_ask?: unknown[];
3059
related_searches?: unknown[];
3160
maps?: unknown[];
32-
news?: unknown[];
61+
news?: News_result[];
62+
images?: Image_result[];
63+
shopping?: Shopping_result[];
3364
videos?: unknown[];
3465
recipes?: unknown[];
3566
perspectives?: unknown[];
@@ -55,6 +86,7 @@ export type {
5586
Search_type,
5687
Search_device,
5788
Organic_result,
89+
News_result,
5890
Search_general,
5991
Search_response,
6092
Search_opts,

0 commit comments

Comments
 (0)