Commit | Line | Data |
---|---|---|
16a81ef7 | 1 | package be.nikiroo.fanfix.reader.cli; |
08fe2e33 NR |
2 | |
3 | import java.io.IOException; | |
08fe2e33 NR |
4 | import java.util.List; |
5 | ||
6 | import be.nikiroo.fanfix.Instance; | |
08fe2e33 NR |
7 | import be.nikiroo.fanfix.bundles.StringId; |
8 | import be.nikiroo.fanfix.data.Chapter; | |
9 | import be.nikiroo.fanfix.data.MetaData; | |
10 | import be.nikiroo.fanfix.data.Paragraph; | |
11 | import be.nikiroo.fanfix.data.Story; | |
16a81ef7 | 12 | import be.nikiroo.fanfix.reader.BasicReader; |
91b82a5c NR |
13 | import be.nikiroo.fanfix.searchable.BasicSearchable; |
14 | import be.nikiroo.fanfix.searchable.SearchableTag; | |
15 | import be.nikiroo.fanfix.supported.SupportType; | |
74a43961 | 16 | import be.nikiroo.utils.StringUtils; |
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 | } | |
b3b9a1cd | 102 | |
b31a0db0 NR |
103 | @Override |
104 | public void search(boolean sync) throws IOException { | |
105 | for (SupportType type : SupportType.values()) { | |
106 | if (BasicSearchable.getSearchable(type) != null) { | |
107 | System.out.println(type); | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
91b82a5c | 112 | @Override |
f76de465 NR |
113 | public void search(SupportType searchOn, String keywords, int page, |
114 | int item, boolean sync) throws IOException { | |
124442f1 NR |
115 | BasicSearchable search = BasicSearchable.getSearchable(searchOn); |
116 | ||
117 | if (page == 0) { | |
118 | System.out.println(search.searchPages(keywords)); | |
119 | } else { | |
120 | List<MetaData> metas = search.search(keywords, page); | |
121 | ||
122 | if (item == 0) { | |
123 | System.out.println("Page " + page + " of stories for: " | |
124 | + keywords); | |
125 | displayStories(metas); | |
126 | } else { | |
127 | // ! 1-based index ! | |
e92e4ae3 | 128 | if (item <= 0 || item > metas.size()) { |
124442f1 NR |
129 | throw new IOException("Index out of bounds: " + item); |
130 | } | |
131 | ||
132 | MetaData meta = metas.get(item - 1); | |
133 | displayStory(meta); | |
134 | } | |
135 | } | |
91b82a5c | 136 | } |
b3b9a1cd | 137 | |
91b82a5c | 138 | @Override |
b3b9a1cd | 139 | public void searchTag(SupportType searchOn, int page, int item, |
f76de465 | 140 | boolean sync, Integer... tags) throws IOException { |
b3b9a1cd | 141 | |
aaeabf3a NR |
142 | BasicSearchable search = BasicSearchable.getSearchable(searchOn); |
143 | SearchableTag stag = search.getTag(tags); | |
b3b9a1cd | 144 | |
aaeabf3a NR |
145 | if (stag == null) { |
146 | // TODO i18n | |
147 | System.out.println("Known tags: "); | |
148 | int i = 1; | |
149 | for (SearchableTag s : search.getTags()) { | |
150 | System.out.println(String.format("%d: %s", i, s.getName())); | |
151 | i++; | |
91b82a5c | 152 | } |
aaeabf3a | 153 | } else { |
91b82a5c NR |
154 | if (page <= 0) { |
155 | if (stag.isLeaf()) { | |
81acd363 | 156 | System.out.println(search.searchPages(stag)); |
91b82a5c NR |
157 | } else { |
158 | System.out.println(stag.getCount()); | |
159 | } | |
160 | } else { | |
161 | List<MetaData> metas = null; | |
162 | List<SearchableTag> subtags = null; | |
163 | int count; | |
b3b9a1cd | 164 | |
91b82a5c NR |
165 | if (stag.isLeaf()) { |
166 | metas = search.search(stag, page); | |
167 | count = metas.size(); | |
168 | } else { | |
169 | subtags = stag.getChildren(); | |
170 | count = subtags.size(); | |
171 | } | |
b3b9a1cd | 172 | |
91b82a5c NR |
173 | if (item > 0) { |
174 | if (item <= count) { | |
175 | if (metas != null) { | |
176 | MetaData meta = metas.get(item - 1); | |
124442f1 | 177 | displayStory(meta); |
91b82a5c NR |
178 | } else { |
179 | SearchableTag subtag = subtags.get(item - 1); | |
aaeabf3a | 180 | displayTag(subtag); |
91b82a5c NR |
181 | } |
182 | } else { | |
b3b9a1cd NR |
183 | System.out.println("Invalid item: only " + count |
184 | + " items found"); | |
91b82a5c NR |
185 | } |
186 | } else { | |
187 | if (metas != null) { | |
8b153400 NR |
188 | // TODO i18n |
189 | System.out.println(String.format("Content of %s: ", | |
aaeabf3a | 190 | stag.getFqName())); |
124442f1 | 191 | displayStories(metas); |
91b82a5c | 192 | } else { |
8b153400 NR |
193 | // TODO i18n |
194 | System.out.println(String.format("Subtags of %s: ", | |
aaeabf3a NR |
195 | stag.getFqName())); |
196 | displayTags(subtags); | |
91b82a5c NR |
197 | } |
198 | } | |
199 | } | |
91b82a5c NR |
200 | } |
201 | } | |
124442f1 | 202 | |
aaeabf3a NR |
203 | private void displayTag(SearchableTag subtag) { |
204 | // TODO: i18n | |
205 | String stories = "stories"; | |
206 | String num = StringUtils.formatNumber(subtag.getCount()); | |
207 | System.out.println(String.format("%s (%s), %s %s", subtag.getName(), | |
208 | subtag.getFqName(), num, stories)); | |
209 | } | |
210 | ||
124442f1 NR |
211 | private void displayStory(MetaData meta) { |
212 | System.out.println(meta.getTitle()); | |
213 | System.out.println(); | |
214 | System.out.println(meta.getUrl()); | |
215 | System.out.println(); | |
216 | System.out.println("Tags: " + meta.getTags()); | |
217 | System.out.println(); | |
218 | for (Paragraph para : meta.getResume()) { | |
219 | System.out.println(para.getContent()); | |
220 | System.out.println(""); | |
221 | } | |
222 | } | |
223 | ||
aaeabf3a NR |
224 | private void displayTags(List<SearchableTag> subtags) { |
225 | int i = 1; | |
226 | for (SearchableTag subtag : subtags) { | |
227 | String total = ""; | |
228 | if (subtag.getCount() > 0) { | |
229 | total = StringUtils.formatNumber(subtag.getCount()); | |
230 | } | |
231 | ||
232 | if (total.isEmpty()) { | |
233 | System.out | |
234 | .println(String.format("%d: %s", i, subtag.getName())); | |
235 | } else { | |
236 | System.out.println(String.format("%d: %s (%s)", i, | |
237 | subtag.getName(), total)); | |
238 | } | |
239 | ||
240 | i++; | |
241 | } | |
242 | } | |
243 | ||
124442f1 NR |
244 | private void displayStories(List<MetaData> metas) { |
245 | int i = 1; | |
246 | for (MetaData meta : metas) { | |
247 | System.out.println(i + ": " + meta.getTitle()); | |
248 | i++; | |
249 | } | |
250 | } | |
08fe2e33 | 251 | } |