Commit | Line | Data |
---|---|---|
16a81ef7 | 1 | package be.nikiroo.fanfix.reader.cli; |
08fe2e33 NR |
2 | |
3 | import java.io.IOException; | |
91b82a5c | 4 | import java.util.ArrayList; |
08fe2e33 NR |
5 | import java.util.List; |
6 | ||
7 | import be.nikiroo.fanfix.Instance; | |
08fe2e33 NR |
8 | import be.nikiroo.fanfix.bundles.StringId; |
9 | import be.nikiroo.fanfix.data.Chapter; | |
10 | import be.nikiroo.fanfix.data.MetaData; | |
11 | import be.nikiroo.fanfix.data.Paragraph; | |
12 | import be.nikiroo.fanfix.data.Story; | |
16a81ef7 | 13 | import be.nikiroo.fanfix.reader.BasicReader; |
91b82a5c NR |
14 | import be.nikiroo.fanfix.searchable.BasicSearchable; |
15 | import be.nikiroo.fanfix.searchable.SearchableTag; | |
16 | import be.nikiroo.fanfix.supported.SupportType; | |
08fe2e33 NR |
17 | |
18 | /** | |
19 | * Command line {@link Story} reader. | |
20 | * <p> | |
21 | * Will output stories to the console. | |
22 | * | |
23 | * @author niki | |
24 | */ | |
3727aae2 | 25 | class CliReader extends BasicReader { |
211f7ddb | 26 | @Override |
350bc060 | 27 | public void read(boolean sync) throws IOException { |
bc2ea776 NR |
28 | MetaData meta = getMeta(); |
29 | ||
30 | if (meta == null) { | |
89cb07a6 | 31 | throw new IOException("No story to read"); |
08fe2e33 | 32 | } |
08fe2e33 | 33 | |
08fe2e33 NR |
34 | String title = ""; |
35 | String author = ""; | |
36 | ||
bc2ea776 NR |
37 | if (meta.getTitle() != null) { |
38 | title = meta.getTitle(); | |
39 | } | |
08fe2e33 | 40 | |
bc2ea776 NR |
41 | if (meta.getAuthor() != null) { |
42 | author = "©" + meta.getAuthor(); | |
43 | if (meta.getDate() != null && !meta.getDate().isEmpty()) { | |
44 | author = author + " (" + meta.getDate() + ")"; | |
08fe2e33 NR |
45 | } |
46 | } | |
47 | ||
48 | System.out.println(title); | |
49 | System.out.println(author); | |
50 | System.out.println(""); | |
51 | ||
bc2ea776 NR |
52 | // TODO: progress? |
53 | for (Chapter chap : getStory(null)) { | |
08fe2e33 NR |
54 | if (chap.getName() != null && !chap.getName().isEmpty()) { |
55 | System.out.println(Instance.getTrans().getString( | |
56 | StringId.CHAPTER_NAMED, chap.getNumber(), | |
57 | chap.getName())); | |
58 | } else { | |
59 | System.out.println(Instance.getTrans().getString( | |
60 | StringId.CHAPTER_UNNAMED, chap.getNumber())); | |
61 | } | |
62 | } | |
63 | } | |
64 | ||
edd46289 | 65 | public void read(int chapter) throws IOException { |
bc2ea776 NR |
66 | MetaData meta = getMeta(); |
67 | ||
68 | if (meta == null) { | |
edd46289 NR |
69 | throw new IOException("No story to read"); |
70 | } | |
71 | ||
bc2ea776 NR |
72 | // TODO: progress? |
73 | if (chapter > getStory(null).getChapters().size()) { | |
08fe2e33 NR |
74 | System.err.println("Chapter " + chapter + ": no such chapter"); |
75 | } else { | |
bc2ea776 | 76 | Chapter chap = getStory(null).getChapters().get(chapter - 1); |
08fe2e33 NR |
77 | System.out.println("Chapter " + chap.getNumber() + ": " |
78 | + chap.getName()); | |
79 | ||
80 | for (Paragraph para : chap) { | |
81 | System.out.println(para.getContent()); | |
82 | System.out.println(""); | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
211f7ddb | 87 | @Override |
b0e88ebd | 88 | public void browse(String source) { |
08fe2e33 | 89 | List<MetaData> stories; |
b0e88ebd | 90 | stories = getLibrary().getListBySource(source); |
08fe2e33 NR |
91 | |
92 | for (MetaData story : stories) { | |
93 | String author = ""; | |
94 | if (story.getAuthor() != null && !story.getAuthor().isEmpty()) { | |
95 | author = " (" + story.getAuthor() + ")"; | |
96 | } | |
97 | ||
98 | System.out.println(story.getLuid() + ": " + story.getTitle() | |
99 | + author); | |
100 | } | |
101 | } | |
91b82a5c NR |
102 | |
103 | @Override | |
104 | public void search(SupportType searchOn, String keywords, int page, int item) throws IOException { | |
105 | ||
106 | } | |
107 | ||
108 | @Override | |
109 | public void searchTag(SupportType searchOn, int page, int item, String... tags) throws IOException { | |
110 | BasicSearchable search = BasicSearchable.getSearchable(searchOn); | |
111 | List<SearchableTag> stags = search.getTags(); | |
112 | ||
113 | ||
114 | SearchableTag stag = null; | |
115 | for (String tag : tags) { | |
116 | stag = null; | |
117 | for (int i = 0 ; i < stags.size() ; i++) { | |
118 | if (stags.get(i).getName().equalsIgnoreCase(tag)) { | |
119 | stag = stags.get(i); | |
120 | break; | |
121 | } | |
122 | } | |
123 | ||
124 | if (stag != null) { | |
125 | search.fillTag(stag); | |
126 | stags = stag.getChildren(); | |
127 | } else { | |
128 | stags = new ArrayList<SearchableTag>(); | |
129 | break; | |
130 | } | |
131 | } | |
132 | ||
133 | if (stag != null) { | |
134 | if (page <= 0) { | |
135 | if (stag.isLeaf()) { | |
136 | System.out.println(stag.getPages()); | |
137 | } else { | |
138 | System.out.println(stag.getCount()); | |
139 | } | |
140 | } else { | |
141 | List<MetaData> metas = null; | |
142 | List<SearchableTag> subtags = null; | |
143 | int count; | |
144 | ||
145 | if (stag.isLeaf()) { | |
146 | metas = search.search(stag, page); | |
147 | count = metas.size(); | |
148 | } else { | |
149 | subtags = stag.getChildren(); | |
150 | count = subtags.size(); | |
151 | } | |
152 | ||
153 | if (item > 0) { | |
154 | if (item <= count) { | |
155 | if (metas != null) { | |
156 | MetaData meta = metas.get(item - 1); | |
157 | System.out.println(item + ": " + meta.getTitle()); | |
158 | System.out.println(meta.getUrl()); | |
159 | System.out.println(); | |
160 | System.out.println("Tags: " + meta.getTags()); | |
161 | System.out.println(); | |
162 | for (Paragraph para : meta.getResume()) { | |
163 | System.out.println(para.getContent()); | |
164 | System.out.println(""); | |
165 | } | |
166 | } else { | |
167 | SearchableTag subtag = subtags.get(item - 1); | |
168 | // TODO: display fixed info, not debug | |
169 | System.out.println(subtag); | |
170 | } | |
171 | } else { | |
172 | System.out.println("Invalid item: only " + count + " items found"); | |
173 | } | |
174 | } else { | |
175 | if (metas != null) { | |
176 | int i = 0; | |
177 | for (MetaData meta : metas) { | |
178 | System.out.println((i + 1) + ": " + meta.getTitle()); | |
179 | i++; | |
180 | } | |
181 | } else { | |
182 | for (SearchableTag subtag : subtags) { | |
183 | if (subtag.getCount() > 0) { | |
184 | System.out.println(subtag.getName() + " (" + subtag.getCount() + ")"); | |
185 | } else { | |
186 | System.out.println(subtag.getName()); | |
187 | } | |
188 | } | |
189 | } | |
190 | } | |
191 | } | |
192 | } else { | |
193 | for (SearchableTag s : stags) { | |
194 | System.out.println(s.getName()); | |
195 | } | |
196 | } | |
197 | } | |
08fe2e33 | 198 | } |