X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Fcli%2FCliReader.java;h=556052149d7c120305df7ab206e10b80618af703;hb=d66deb8d8b30cff6b54db352eef34a3508939f84;hp=9ec37a583d983cee46333f32f793ea1dc04dc3b1;hpb=350bc060516184774f8116e61696a8c3c45ba85d;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/reader/cli/CliReader.java b/src/be/nikiroo/fanfix/reader/cli/CliReader.java index 9ec37a5..5560521 100644 --- a/src/be/nikiroo/fanfix/reader/cli/CliReader.java +++ b/src/be/nikiroo/fanfix/reader/cli/CliReader.java @@ -10,6 +10,10 @@ import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Paragraph; import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.reader.BasicReader; +import be.nikiroo.fanfix.searchable.BasicSearchable; +import be.nikiroo.fanfix.searchable.SearchableTag; +import be.nikiroo.fanfix.supported.SupportType; +import be.nikiroo.utils.StringUtils; /** * Command line {@link Story} reader. @@ -48,12 +52,11 @@ class CliReader extends BasicReader { // TODO: progress? for (Chapter chap : getStory(null)) { if (chap.getName() != null && !chap.getName().isEmpty()) { - System.out.println(Instance.getTrans().getString( - StringId.CHAPTER_NAMED, chap.getNumber(), + System.out.println(Instance.getInstance().getTrans().getString(StringId.CHAPTER_NAMED, chap.getNumber(), chap.getName())); } else { - System.out.println(Instance.getTrans().getString( - StringId.CHAPTER_UNNAMED, chap.getNumber())); + System.out.println( + Instance.getInstance().getTrans().getString(StringId.CHAPTER_UNNAMED, chap.getNumber())); } } } @@ -81,9 +84,8 @@ class CliReader extends BasicReader { } @Override - public void browse(String source) { - List stories; - stories = getLibrary().getListBySource(source); + public void browse(String source) throws IOException { + List stories = getLibrary().getListBySource(source); for (MetaData story : stories) { String author = ""; @@ -95,4 +97,153 @@ class CliReader extends BasicReader { + author); } } + + @Override + public void search(boolean sync) throws IOException { + for (SupportType type : SupportType.values()) { + if (BasicSearchable.getSearchable(type) != null) { + System.out.println(type); + } + } + } + + @Override + public void search(SupportType searchOn, String keywords, int page, + int item, boolean sync) throws IOException { + BasicSearchable search = BasicSearchable.getSearchable(searchOn); + + if (page == 0) { + System.out.println(search.searchPages(keywords)); + } else { + List metas = search.search(keywords, page); + + if (item == 0) { + System.out.println("Page " + page + " of stories for: " + + keywords); + displayStories(metas); + } else { + // ! 1-based index ! + if (item <= 0 || item > metas.size()) { + throw new IOException("Index out of bounds: " + item); + } + + MetaData meta = metas.get(item - 1); + displayStory(meta); + } + } + } + + @Override + public void searchTag(SupportType searchOn, int page, int item, + boolean sync, Integer... tags) throws IOException { + + BasicSearchable search = BasicSearchable.getSearchable(searchOn); + SearchableTag stag = search.getTag(tags); + + if (stag == null) { + // TODO i18n + System.out.println("Known tags: "); + int i = 1; + for (SearchableTag s : search.getTags()) { + System.out.println(String.format("%d: %s", i, s.getName())); + i++; + } + } else { + if (page <= 0) { + if (stag.isLeaf()) { + System.out.println(search.searchPages(stag)); + } else { + System.out.println(stag.getCount()); + } + } else { + List metas = null; + List subtags = null; + int count; + + if (stag.isLeaf()) { + metas = search.search(stag, page); + count = metas.size(); + } else { + subtags = stag.getChildren(); + count = subtags.size(); + } + + if (item > 0) { + if (item <= count) { + if (metas != null) { + MetaData meta = metas.get(item - 1); + displayStory(meta); + } else { + SearchableTag subtag = subtags.get(item - 1); + displayTag(subtag); + } + } else { + System.out.println("Invalid item: only " + count + + " items found"); + } + } else { + if (metas != null) { + // TODO i18n + System.out.println(String.format("Content of %s: ", + stag.getFqName())); + displayStories(metas); + } else { + // TODO i18n + System.out.println(String.format("Subtags of %s: ", + stag.getFqName())); + displayTags(subtags); + } + } + } + } + } + + private void displayTag(SearchableTag subtag) { + // TODO: i18n + String stories = "stories"; + String num = StringUtils.formatNumber(subtag.getCount()); + System.out.println(String.format("%s (%s), %s %s", subtag.getName(), + subtag.getFqName(), num, stories)); + } + + private void displayStory(MetaData meta) { + System.out.println(meta.getTitle()); + System.out.println(); + System.out.println(meta.getUrl()); + System.out.println(); + System.out.println("Tags: " + meta.getTags()); + System.out.println(); + for (Paragraph para : meta.getResume()) { + System.out.println(para.getContent()); + System.out.println(""); + } + } + + private void displayTags(List subtags) { + int i = 1; + for (SearchableTag subtag : subtags) { + String total = ""; + if (subtag.getCount() > 0) { + total = StringUtils.formatNumber(subtag.getCount()); + } + + if (total.isEmpty()) { + System.out + .println(String.format("%d: %s", i, subtag.getName())); + } else { + System.out.println(String.format("%d: %s (%s)", i, + subtag.getName(), total)); + } + + i++; + } + } + + private void displayStories(List metas) { + int i = 1; + for (MetaData meta : metas) { + System.out.println(i + ": " + meta.getTitle()); + i++; + } + } }