Instance: use getInstance()
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / cli / CliReader.java
index 89eb3ac034b0ef1c9e5d338d6528a7050a200eac..556052149d7c120305df7ab206e10b80618af703 100644 (file)
@@ -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;
@@ -53,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()));
                        }
                }
        }
@@ -86,9 +84,8 @@ class CliReader extends BasicReader {
        }
 
        @Override
-       public void browse(String source) {
-               List<MetaData> stories;
-               stories = getLibrary().getListBySource(source);
+       public void browse(String source) throws IOException {
+               List<MetaData> stories = getLibrary().getListBySource(source);
 
                for (MetaData story : stories) {
                        String author = "";
@@ -102,41 +99,59 @@ class CliReader extends BasicReader {
        }
 
        @Override
-       public void search(SupportType searchOn, String keywords, int page, int item)
-                       throws IOException {
-
+       public void search(boolean sync) throws IOException {
+               for (SupportType type : SupportType.values()) {
+                       if (BasicSearchable.getSearchable(type) != null) {
+                               System.out.println(type);
+                       }
+               }
        }
 
        @Override
-       public void searchTag(SupportType searchOn, int page, int item,
-                       String... tags) throws IOException {
+       public void search(SupportType searchOn, String keywords, int page,
+                       int item, boolean sync) throws IOException {
                BasicSearchable search = BasicSearchable.getSearchable(searchOn);
-               List<SearchableTag> stags = search.getTags();
-
-               SearchableTag stag = null;
-               for (String tag : tags) {
-                       stag = null;
-                       for (int i = 0; i < stags.size(); i++) {
-                               if (stags.get(i).getName().equalsIgnoreCase(tag)) {
-                                       stag = stags.get(i);
-                                       break;
-                               }
-                       }
 
-                       if (stag != null) {
-                               search.fillTag(stag);
-                               stags = stag.getChildren();
+               if (page == 0) {
+                       System.out.println(search.searchPages(keywords));
+               } else {
+                       List<MetaData> metas = search.search(keywords, page);
+
+                       if (item == 0) {
+                               System.out.println("Page " + page + " of stories for: "
+                                               + keywords);
+                               displayStories(metas);
                        } else {
-                               stags = new ArrayList<SearchableTag>();
-                               break;
+                               // ! 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 {
 
-               if (stag != null) {
+               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()) {
-                                       search.search(stag, 1);
-                                       System.out.println(stag.getPages());
+                                       System.out.println(search.searchPages(stag));
                                } else {
                                        System.out.println(stag.getCount());
                                }
@@ -157,47 +172,10 @@ class CliReader extends BasicReader {
                                        if (item <= count) {
                                                if (metas != null) {
                                                        MetaData meta = metas.get(item - 1);
-                                                       System.out.println(page + "/" + item + ": "
-                                                                       + 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("");
-                                                       }
+                                                       displayStory(meta);
                                                } else {
                                                        SearchableTag subtag = subtags.get(item - 1);
-
-                                                       String sp = "";
-                                                       if (subtag.getParent() != null) {
-                                                               List<String> parents = new ArrayList<String>();
-                                                               for (SearchableTag parent = subtag.getParent(); parent != null; parent = parent
-                                                                               .getParent()) {
-                                                                       parents.add(parent.getName());
-                                                               }
-                                                               for (String parent : parents) {
-                                                                       if (!sp.isEmpty()) {
-                                                                               sp += " / ";
-                                                                       }
-                                                                       sp += parent;
-                                                               }
-                                                       }
-
-                                                       // TODO: i18n
-                                                       if (sp.isEmpty()) {
-                                                               System.out.println(String.format(
-                                                                               "%d/%d: %s, %d %s", page, item,
-                                                                               subtag.getName(), subtag.getCount(),
-                                                                               "stories"));
-                                                       } else {
-                                                               System.out.println(String.format(
-                                                                               "%d/%d: %s (%s), %d %s", page, item,
-                                                                               subtag.getName(), sp,
-                                                                               subtag.getCount(), "stories"));
-                                                       }
+                                                       displayTag(subtag);
                                                }
                                        } else {
                                                System.out.println("Invalid item: only " + count
@@ -205,38 +183,67 @@ class CliReader extends BasicReader {
                                        }
                                } else {
                                        if (metas != null) {
-                                               int i = 0;
-                                               for (MetaData meta : metas) {
-                                                       System.out
-                                                                       .println((i + 1) + ": " + meta.getTitle());
-                                                       i++;
-                                               }
+                                               // TODO i18n
+                                               System.out.println(String.format("Content of %s: ",
+                                                               stag.getFqName()));
+                                               displayStories(metas);
                                        } else {
-                                               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++;
-                                               }
+                                               // TODO i18n
+                                               System.out.println(String.format("Subtags of %s: ",
+                                                               stag.getFqName()));
+                                               displayTags(subtags);
                                        }
                                }
                        }
-               } else {
-                       for (SearchableTag s : stags) {
-                               System.out.println(s.getName());
+               }
+       }
+
+       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<SearchableTag> 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<MetaData> metas) {
+               int i = 1;
+               for (MetaData meta : metas) {
+                       System.out.println(i + ": " + meta.getTitle());
+                       i++;
                }
        }
 }