Add snippet text to results view
This commit is contained in:
parent
aef4f0d76a
commit
7025ccd563
22
src/main.rs
22
src/main.rs
@ -21,6 +21,7 @@ struct SearchResult {
|
|||||||
title: String,
|
title: String,
|
||||||
domain: String,
|
domain: String,
|
||||||
url: String,
|
url: String,
|
||||||
|
snippet: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
const DDG_HTML_URL: &str = "https://duckduckgo.com";
|
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_selector = Selector::parse(".web-result").unwrap();
|
||||||
let result_title_selector = Selector::parse(".result__a").unwrap();
|
let result_title_selector = Selector::parse(".result__a").unwrap();
|
||||||
let result_url_selector = Selector::parse(".result__url").unwrap();
|
let result_url_selector = Selector::parse(".result__url").unwrap();
|
||||||
|
let result_snippet_selector = Selector::parse(".result__snippet").unwrap();
|
||||||
|
|
||||||
let mut results: Vec<SearchResult> = Vec::new();
|
let mut results: Vec<SearchResult> = Vec::new();
|
||||||
for result in document.select(&result_selector) {
|
for result in document.select(&result_selector) {
|
||||||
let result_title = result.select(&result_title_selector).next().unwrap();
|
let result_title = result.select(&result_title_selector).next().unwrap();
|
||||||
let result_url = result.select(&result_url_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 {
|
results.push(SearchResult {
|
||||||
title: result_title.text().collect::<Vec<_>>().join(""),
|
title: result_title.text().collect::<Vec<_>>().join(""),
|
||||||
domain: result_url.text().collect::<Vec<_>>().join(""),
|
domain: result_url.text().collect::<Vec<_>>().join(""),
|
||||||
url: String::from(result_url.value().attr("href").unwrap()),
|
url: String::from(result_url.value().attr("href").unwrap()),
|
||||||
|
snippet: String::from(result_snippet.text().collect::<Vec<_>>().join("")),
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,12 +104,16 @@ fn build_list(results: Vec<SearchResult>) -> OnEventView<SelectView> {
|
|||||||
for (i, result) in results.into_iter().enumerate() {
|
for (i, result) in results.into_iter().enumerate() {
|
||||||
let url = format!("{}{}", DDG_HTML_URL, result.url);
|
let url = format!("{}{}", DDG_HTML_URL, result.url);
|
||||||
result_view = result_view.item(
|
result_view = result_view.item(
|
||||||
format!("{}. {}", i, result.title),
|
format!("{}. {}", i + 1, result.title),
|
||||||
url.clone()
|
url.clone()
|
||||||
)
|
)
|
||||||
.item(
|
.item(
|
||||||
format!(" {}", result.domain.replace("\n", "").trim()),
|
format!(" {}", result.domain.replace("\n", "").trim()),
|
||||||
url.clone()
|
url.clone()
|
||||||
|
)
|
||||||
|
.item(
|
||||||
|
format!(" {}", result.snippet.replace("\n", "").trim()),
|
||||||
|
url.clone()
|
||||||
)
|
)
|
||||||
.item(
|
.item(
|
||||||
" ",
|
" ",
|
||||||
@ -118,17 +126,17 @@ fn build_list(results: Vec<SearchResult>) -> OnEventView<SelectView> {
|
|||||||
let result_view = OnEventView::new(result_view)
|
let result_view = OnEventView::new(result_view)
|
||||||
.on_pre_event_inner(Key::Up, |s| {
|
.on_pre_event_inner(Key::Up, |s| {
|
||||||
let from_bottom = (s.len() - 1) - s.selected_id().unwrap();
|
let from_bottom = (s.len() - 1) - s.selected_id().unwrap();
|
||||||
if from_bottom < 2 {
|
if from_bottom < 3 {
|
||||||
s.select_up(2 - from_bottom);
|
s.select_up(3 - from_bottom);
|
||||||
} else {
|
} else {
|
||||||
s.select_up(3);
|
s.select_up(4);
|
||||||
}
|
}
|
||||||
Some(EventResult::Consumed(None))
|
Some(EventResult::Consumed(None))
|
||||||
})
|
})
|
||||||
.on_pre_event_inner(Key::Down, |s| {
|
.on_pre_event_inner(Key::Down, |s| {
|
||||||
if s.selected_id().unwrap() != s.len() - 3 {
|
if s.selected_id().unwrap() != s.len() - 4 {
|
||||||
s.select_down(4);
|
s.select_down(6);
|
||||||
s.select_up(1);
|
s.select_up(2);
|
||||||
}
|
}
|
||||||
Some(EventResult::Consumed(None))
|
Some(EventResult::Consumed(None))
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user