remove reader ui/tui
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / CliReader.java
similarity index 72%
rename from src/be/nikiroo/fanfix/reader/cli/CliReader.java
rename to src/be/nikiroo/fanfix/reader/CliReader.java
index 235276c8b111d890ff878d5991e2cc9e79c54de8..96ca64405bf7ced80bbb8b9c334119352acc1399 100644 (file)
@@ -1,4 +1,4 @@
-package be.nikiroo.fanfix.reader.cli;
+package be.nikiroo.fanfix.reader;
 
 import java.io.IOException;
 import java.util.List;
@@ -9,10 +9,10 @@ import be.nikiroo.fanfix.data.Chapter;
 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.Image;
 import be.nikiroo.utils.StringUtils;
 
 /**
@@ -22,11 +22,27 @@ import be.nikiroo.utils.StringUtils;
  * 
  * @author niki
  */
-class CliReader extends BasicReader {
-       @Override
-       public void read(boolean sync) throws IOException {
-               MetaData meta = getMeta();
+public class CliReader extends BasicReader {
+       public void listBooks(String source) throws IOException {
+               List<MetaData> stories = Instance.getInstance().getLibrary().getList()
+                               .filter(source, null, null);
 
+               for (MetaData story : stories) {
+                       String author = "";
+                       if (story.getAuthor() != null && !story.getAuthor().isEmpty()) {
+                               author = " (" + story.getAuthor() + ")";
+                       }
+
+                       System.out.println(
+                                       story.getLuid() + ": " + story.getTitle() + author);
+               }
+       }
+
+       public void listChapters(Story story) throws IOException {
+               if (story == null || story.getMeta() == null) {
+                       throw new IOException("No story to read");
+               }
+               MetaData meta = story.getMeta();
                if (meta == null) {
                        throw new IOException("No story to read");
                }
@@ -49,57 +65,50 @@ class CliReader extends BasicReader {
                System.out.println(author);
                System.out.println("");
 
-               // TODO: progress?
-               for (Chapter chap : getStory(null)) {
+               for (Chapter chap : story) {
                        if (chap.getName() != null && !chap.getName().isEmpty()) {
-                               System.out.println(Instance.getInstance().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.getInstance().getTrans().getString(StringId.CHAPTER_UNNAMED, chap.getNumber()));
+                               System.out.println(Instance.getInstance().getTrans()
+                                               .getString(StringId.CHAPTER_UNNAMED, chap.getNumber()));
                        }
                }
        }
 
-       public void read(int chapter) throws IOException {
-               MetaData meta = getMeta();
-
+       public void printChapter(Story story, int chapter) throws IOException {
+               if (story == null || story.getMeta() == null) {
+                       throw new IOException("No story to read");
+               }
+               MetaData meta = story.getMeta();
                if (meta == null) {
                        throw new IOException("No story to read");
                }
 
-               // TODO: progress?
-               if (chapter > getStory(null).getChapters().size()) {
+               if (chapter <= 0 || chapter > story.getChapters().size()) {
                        System.err.println("Chapter " + chapter + ": no such chapter");
                } else {
-                       Chapter chap = getStory(null).getChapters().get(chapter - 1);
-                       System.out.println("Chapter " + chap.getNumber() + ": "
-                                       + chap.getName());
-
+                       Chapter chap = story.getChapters().get(chapter - 1);
+                       System.out.println(
+                                       "Chapter " + chap.getNumber() + ": " + chap.getName());
+                       System.out.println();
+                       
                        for (Paragraph para : chap) {
-                               System.out.println(para.getContent());
+                               Image img = para.getContentImage();
+                               if (img != null) {
+                                       String sz = StringUtils.formatNumber(img.getSize(), 1);
+                                       System.out.println("[Image: " + sz + "]");
+                               } else {
+                                       System.out.println(
+                                                       para.getContent() == null ? "" : para.getContent());
+                               }
                                System.out.println("");
                        }
                }
        }
 
-       @Override
-       public void browse(String source) throws IOException {
-               List<MetaData> stories = getLibrary().getList().filter(source, null, null);
-
-               for (MetaData story : stories) {
-                       String author = "";
-                       if (story.getAuthor() != null && !story.getAuthor().isEmpty()) {
-                               author = " (" + story.getAuthor() + ")";
-                       }
-
-                       System.out.println(story.getLuid() + ": " + story.getTitle()
-                                       + author);
-               }
-       }
-
-       @Override
-       public void search(boolean sync) throws IOException {
+       public void listSearchables() throws IOException {
                for (SupportType type : SupportType.values()) {
                        if (BasicSearchable.getSearchable(type) != null) {
                                System.out.println(type);
@@ -107,9 +116,8 @@ class CliReader extends BasicReader {
                }
        }
 
-       @Override
-       public void search(SupportType searchOn, String keywords, int page,
-                       int item, boolean sync) throws IOException {
+       public void searchBooksByKeyword(SupportType searchOn, String keywords,
+                       int page, int item) throws IOException {
                BasicSearchable search = BasicSearchable.getSearchable(searchOn);
 
                if (page == 0) {
@@ -118,8 +126,8 @@ class CliReader extends BasicReader {
                        List<MetaData> metas = search.search(keywords, page);
 
                        if (item == 0) {
-                               System.out.println("Page " + page + " of stories for: "
-                                               + keywords);
+                               System.out.println(
+                                               "Page " + page + " of stories for: " + keywords);
                                displayStories(metas);
                        } else {
                                // ! 1-based index !
@@ -133,9 +141,8 @@ class CliReader extends BasicReader {
                }
        }
 
-       @Override
-       public void searchTag(SupportType searchOn, int page, int item,
-                       boolean sync, Integer... tags) throws IOException {
+       public void searchBooksByTag(SupportType searchOn, int page, int item,
+                       Integer... tags) throws IOException {
 
                BasicSearchable search = BasicSearchable.getSearchable(searchOn);
                SearchableTag stag = search.getTag(tags);
@@ -178,8 +185,8 @@ class CliReader extends BasicReader {
                                                        displayTag(subtag);
                                                }
                                        } else {
-                                               System.out.println("Invalid item: only " + count
-                                                               + " items found");
+                                               System.out.println(
+                                                               "Invalid item: only " + count + " items found");
                                        }
                                } else {
                                        if (metas != null) {