From: Niki Roo Date: Sun, 14 Apr 2019 15:57:13 +0000 (+0200) Subject: search cli: code cleanup X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=aaeabf3a939ce517e3044a0a8ee782f19f3f930d;p=nikiroo-utils.git search cli: code cleanup --- diff --git a/src/be/nikiroo/fanfix/reader/cli/CliReader.java b/src/be/nikiroo/fanfix/reader/cli/CliReader.java index d8d3a6c..3795cd8 100644 --- a/src/be/nikiroo/fanfix/reader/cli/CliReader.java +++ b/src/be/nikiroo/fanfix/reader/cli/CliReader.java @@ -1,7 +1,6 @@ package be.nikiroo.fanfix.reader.cli; import java.io.IOException; -import java.util.ArrayList; import java.util.List; import be.nikiroo.fanfix.Instance; @@ -130,32 +129,19 @@ class CliReader extends BasicReader { @Override public void searchTag(SupportType searchOn, int page, int item, boolean sync, Integer... tags) throws IOException { - BasicSearchable search = BasicSearchable.getSearchable(searchOn); - List stags = search.getTags(); - String fqnTag = ""; - SearchableTag stag = null; - for (Integer tagIndex : tags) { - // ! 1-based index ! - if (tagIndex == null || tagIndex <= 0 | tagIndex > stags.size()) { - throw new IOException("Index out of bounds: " + tagIndex); - } + BasicSearchable search = BasicSearchable.getSearchable(searchOn); + SearchableTag stag = search.getTag(tags); - stag = stags.get(tagIndex - 1); - if (stag != null) { - search.fillTag(stag); - stags = stag.getChildren(); - if (!fqnTag.isEmpty()) { - fqnTag += " / "; - } - fqnTag += stag.getName(); - } else { - stags = new ArrayList(); - break; + 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++; } - } - - if (stag != null) { + } else { if (page <= 0) { if (stag.isLeaf()) { search.search(stag, 1); @@ -183,13 +169,7 @@ class CliReader extends BasicReader { displayStory(meta); } else { SearchableTag subtag = subtags.get(item - 1); - - // TODO: i18n - String stories = "stories"; - String num = StringUtils.formatNumber(subtag - .getCount()); - System.out.println(String.format("%s (%s), %s %s", - subtag.getName(), fqnTag, num, stories)); + displayTag(subtag); } } else { System.out.println("Invalid item: only " + count @@ -199,44 +179,27 @@ class CliReader extends BasicReader { if (metas != null) { // TODO i18n System.out.println(String.format("Content of %s: ", - fqnTag)); + stag.getFqName())); displayStories(metas); } else { // TODO i18n System.out.println(String.format("Subtags of %s: ", - fqnTag)); - 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++; - } + stag.getFqName())); + displayTags(subtags); } } } - } else { - // TODO i18n - System.out.println("Known tags: "); - int i = 1; - for (SearchableTag s : stags) { - System.out.println(String.format("%d: %s", i, s.getName())); - i++; - } } } + 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(); @@ -250,6 +213,26 @@ class CliReader extends BasicReader { } } + 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) { diff --git a/src/be/nikiroo/fanfix/searchable/BasicSearchable.java b/src/be/nikiroo/fanfix/searchable/BasicSearchable.java index 0449b81..d8076fa 100644 --- a/src/be/nikiroo/fanfix/searchable/BasicSearchable.java +++ b/src/be/nikiroo/fanfix/searchable/BasicSearchable.java @@ -34,6 +34,38 @@ public abstract class BasicSearchable { support = BasicSupport.getSupport(getType(), null); } + /** + * Find the given tag by its hierarchical IDs. + *

+ * I.E., it will take the tag A, subtag B, subsubtag C... + * + * @param ids + * the IDs to look for + * + * @return the appropriate tag fully filled, or NULL if not found + * + * @throws IOException + * in case of I/O error + */ + public SearchableTag getTag(Integer... ids) throws IOException { + SearchableTag tag = null; + List tags = getTags(); + + for (Integer tagIndex : ids) { + // ! 1-based index ! + if (tagIndex == null || tags == null || tagIndex <= 0 + || tagIndex > tags.size()) { + return null; + } + + tag = tags.get(tagIndex - 1); + fillTag(tag); + tags = tag.getChildren(); + } + + return tag; + } + /** * The support type. * @@ -189,7 +221,7 @@ public abstract class BasicSearchable { * * @return an implementation that supports it, or NULL */ - public static BasicSearchable getSearchable(SupportType type) { + static public BasicSearchable getSearchable(SupportType type) { BasicSearchable support = null; switch (type) { diff --git a/src/be/nikiroo/fanfix/searchable/SearchableTag.java b/src/be/nikiroo/fanfix/searchable/SearchableTag.java index c12b3c6..f73dd15 100644 --- a/src/be/nikiroo/fanfix/searchable/SearchableTag.java +++ b/src/be/nikiroo/fanfix/searchable/SearchableTag.java @@ -43,7 +43,7 @@ public class SearchableTag { * the tag is a leaf tag, that is, it will not return subtags * with {@link BasicSearchable#fillTag(SearchableTag)} but will * return stories with - * {@link BasicSearchable#search(SearchableTag)} + * {@link BasicSearchable#search(SearchableTag, int)} */ public SearchableTag(String id, String name, boolean leaf) { this(id, name, leaf, true); @@ -60,7 +60,7 @@ public class SearchableTag { * the tag is a leaf tag, that is, it will not return subtags * with {@link BasicSearchable#fillTag(SearchableTag)} but will * return stories with - * {@link BasicSearchable#search(SearchableTag)} + * {@link BasicSearchable#search(SearchableTag, int)} * @param complete * the tag {@link SearchableTag#isComplete()} or not */ @@ -92,6 +92,21 @@ public class SearchableTag { return name; } + /** + * The fully qualified tag name, which can be displayed to the user. + *

+ * It will display all the tags that lead to this one as well as this one. + * + * @return the fully qualified name + */ + public String getFqName() { + if (parent != null) { + return parent.getFqName() + " / " + name; + } + + return name; + } + /** * Non-complete, non-leaf tags can still be completed via a * {@link BasicSearchable#fillTag(SearchableTag)} operation from a @@ -171,7 +186,7 @@ public class SearchableTag { /** * This tag is a leaf tag, that is, it will not return other subtags with * {@link BasicSearchable#fillTag(SearchableTag)} but will return stories - * with {@link BasicSearchable#search(SearchableTag)}. + * with {@link BasicSearchable#search(SearchableTag, int)}. * * @return TRUE if it is */ @@ -182,7 +197,7 @@ public class SearchableTag { /** * This tag is a leaf tag, that is, it will not return other subtags with * {@link BasicSearchable#fillTag(SearchableTag)} but will return stories - * with {@link BasicSearchable#search(SearchableTag)}. + * with {@link BasicSearchable#search(SearchableTag, int)}. *

* Will reset the number of pages to -1. *