@@ -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+
123174const 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 ) ;
0 commit comments