Skip to content

Commit

Permalink
feat: add default search for each source
Browse files Browse the repository at this point in the history
  • Loading branch information
Beastwick18 committed May 31, 2024
1 parent 13613e0 commit 2a6c956
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ pub trait Source {
fn default_category(config: &SourceConfig) -> usize;
fn default_sort(config: &SourceConfig) -> usize;
fn default_filter(config: &SourceConfig) -> usize;
fn default_search(config: &SourceConfig) -> String;

fn format_table(
items: &[Item],
Expand Down Expand Up @@ -235,6 +236,9 @@ impl Sources {
w.filter.selected = self.default_filter(&ctx.config.sources);
w.filter.table.select(w.filter.selected);

w.search.input.input = self.default_search(&ctx.config.sources);
w.search.input.cursor = w.search.input.input.len();

// Go back to first page when changing source
ctx.page = 1;
}
Expand Down Expand Up @@ -279,6 +283,14 @@ impl Sources {
}
}

pub fn default_search(self, config: &SourceConfig) -> String {
match self {
Sources::Nyaa => NyaaHtmlSource::default_search(config),
Sources::SubekiNyaa => SubekiHtmlSource::default_search(config),
Sources::TorrentGalaxy => TorrentGalaxyHtmlSource::default_search(config),
}
}

pub fn format_table(
self,
items: &[Item],
Expand Down
23 changes: 20 additions & 3 deletions src/source/nyaa_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,16 +421,33 @@ impl Source for NyaaHtmlSource {
}

fn default_category(cfg: &SourceConfig) -> usize {
let default = cfg.nyaa.to_owned().unwrap_or_default().default_category;
let default = cfg
.nyaa
.as_ref()
.map(|c| c.default_category.to_owned())
.unwrap_or_default();
Self::info().entry_from_cfg(&default).id
}

fn default_sort(cfg: &SourceConfig) -> usize {
cfg.nyaa.to_owned().unwrap_or_default().default_sort as usize
cfg.nyaa
.as_ref()
.map(|c| c.default_sort as usize)
.unwrap_or_default()
}

fn default_filter(cfg: &SourceConfig) -> usize {
cfg.nyaa.to_owned().unwrap_or_default().default_filter as usize
cfg.nyaa
.as_ref()
.map(|c| c.default_filter as usize)
.unwrap_or_default()
}

fn default_search(cfg: &SourceConfig) -> String {
cfg.nyaa
.as_ref()
.map(|c| c.default_search.to_owned())
.unwrap_or_default()
}

fn format_table(
Expand Down
23 changes: 20 additions & 3 deletions src/source/sukebei_nyaa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,33 @@ impl Source for SubekiHtmlSource {
}

fn default_category(cfg: &SourceConfig) -> usize {
let default = cfg.sukebei.to_owned().unwrap_or_default().default_category;
let default = cfg
.sukebei
.as_ref()
.map(|c| c.default_category.to_owned())
.unwrap_or_default();
Self::info().entry_from_cfg(&default).id
}

fn default_sort(cfg: &SourceConfig) -> usize {
cfg.sukebei.to_owned().unwrap_or_default().default_sort as usize
cfg.sukebei
.as_ref()
.map(|c| c.default_sort as usize)
.unwrap_or_default()
}

fn default_filter(cfg: &SourceConfig) -> usize {
cfg.sukebei.to_owned().unwrap_or_default().default_filter as usize
cfg.sukebei
.as_ref()
.map(|c| c.default_filter as usize)
.unwrap_or_default()
}

fn default_search(cfg: &SourceConfig) -> String {
cfg.sukebei
.as_ref()
.map(|c| c.default_search.to_owned())
.unwrap_or_default()
}

fn format_table(
Expand Down
23 changes: 20 additions & 3 deletions src/source/torrent_galaxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,16 +455,33 @@ impl Source for TorrentGalaxyHtmlSource {
}

fn default_category(cfg: &SourceConfig) -> usize {
let default = cfg.tgx.to_owned().unwrap_or_default().default_category;
let default = cfg
.tgx
.as_ref()
.map(|c| c.default_category.to_owned())
.unwrap_or_default();
Self::info().entry_from_cfg(&default).id
}

fn default_sort(cfg: &SourceConfig) -> usize {
cfg.tgx.to_owned().unwrap_or_default().default_sort as usize
cfg.tgx
.as_ref()
.map(|c| c.default_sort as usize)
.unwrap_or_default()
}

fn default_filter(cfg: &SourceConfig) -> usize {
cfg.tgx.to_owned().unwrap_or_default().default_filter as usize
cfg.tgx
.as_ref()
.map(|c| c.default_filter as usize)
.unwrap_or_default()
}

fn default_search(cfg: &SourceConfig) -> String {
cfg.tgx
.as_ref()
.map(|c| c.default_search.to_owned())
.unwrap_or_default()
}

fn format_table(
Expand Down

0 comments on commit 2a6c956

Please sign in to comment.