From 7025ccd563c263841b6207562806fe23d4bdf006 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Fri, 9 Feb 2018 15:52:40 -0500 Subject: [PATCH] Add snippet text to results view --- src/main.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 844ee7f..f58f02c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ struct SearchResult { title: String, domain: String, url: String, + snippet: String, } const DDG_HTML_URL: &str = "https://duckduckgo.com"; @@ -72,15 +73,18 @@ fn search(s: &mut Cursive, query: &str) { let result_selector = Selector::parse(".web-result").unwrap(); let result_title_selector = Selector::parse(".result__a").unwrap(); let result_url_selector = Selector::parse(".result__url").unwrap(); + let result_snippet_selector = Selector::parse(".result__snippet").unwrap(); let mut results: Vec = Vec::new(); for result in document.select(&result_selector) { let result_title = result.select(&result_title_selector).next().unwrap(); let result_url = result.select(&result_url_selector).next().unwrap(); + let result_snippet = result.select(&result_snippet_selector).next().unwrap(); results.push(SearchResult { title: result_title.text().collect::>().join(""), domain: result_url.text().collect::>().join(""), url: String::from(result_url.value().attr("href").unwrap()), + snippet: String::from(result_snippet.text().collect::>().join("")), } ); } @@ -100,12 +104,16 @@ fn build_list(results: Vec) -> OnEventView { for (i, result) in results.into_iter().enumerate() { let url = format!("{}{}", DDG_HTML_URL, result.url); result_view = result_view.item( - format!("{}. {}", i, result.title), + format!("{}. {}", i + 1, result.title), url.clone() ) .item( format!(" {}", result.domain.replace("\n", "").trim()), url.clone() + ) + .item( + format!(" {}", result.snippet.replace("\n", "").trim()), + url.clone() ) .item( " ", @@ -118,17 +126,17 @@ fn build_list(results: Vec) -> OnEventView { let result_view = OnEventView::new(result_view) .on_pre_event_inner(Key::Up, |s| { let from_bottom = (s.len() - 1) - s.selected_id().unwrap(); - if from_bottom < 2 { - s.select_up(2 - from_bottom); + if from_bottom < 3 { + s.select_up(3 - from_bottom); } else { - s.select_up(3); + s.select_up(4); } Some(EventResult::Consumed(None)) }) .on_pre_event_inner(Key::Down, |s| { - if s.selected_id().unwrap() != s.len() - 3 { - s.select_down(4); - s.select_up(1); + if s.selected_id().unwrap() != s.len() - 4 { + s.select_down(6); + s.select_up(2); } Some(EventResult::Consumed(None)) });