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 | 54 | if (chap.getName() != null && !chap.getName().isEmpty()) { |
d66deb8d | 55 | System.out.println(Instance.getInstance().getTrans().getString(StringId.CHAPTER_NAMED, chap.getNumber(), |
08fe2e33 NR |
56 | chap.getName())); |
57 | } else { | |
d66deb8d NR |
58 | System.out.println( |
59 | Instance.getInstance().getTrans().getString(StringId.CHAPTER_UNNAMED, chap.getNumber())); | |
08fe2e33 NR |
60 | } |
61 | } | |
62 | } | |
63 | ||
edd46289 | 64 | public void read(int chapter) throws IOException { |
bc2ea776 NR |
65 | MetaData meta = getMeta(); |
66 | ||
67 | if (meta == null) { | |
edd46289 NR |
68 | throw new IOException("No story to read"); |
69 | } | |
70 | ||
bc2ea776 NR |
71 | // TODO: progress? |
72 | if (chapter > getStory(null).getChapters().size()) { | |
08fe2e33 NR |
73 | System.err.println("Chapter " + chapter + ": no such chapter"); |
74 | } else { | |
bc2ea776 | 75 | Chapter chap = getStory(null).getChapters().get(chapter - 1); |
08fe2e33 NR |
76 | System.out.println("Chapter " + chap.getNumber() + ": " |
77 | + chap.getName()); | |
78 | ||
79 | for (Paragraph para : chap) { | |
80 | System.out.println(para.getContent()); | |
81 | System.out.println(""); | |
82 | } | |
83 | } | |
84 | } | |
85 | ||
211f7ddb | 86 | @Override |
0bb51c9c | 87 | public void browse(String source) throws IOException { |
ef98466f | 88 | List<MetaData> stories = getLibrary().getList().filter(source, null, null); |
08fe2e33 NR |
89 | |
90 | for (MetaData story : stories) { | |
91 | String author = ""; | |
92 | if (story.getAuthor() != null && !story.getAuthor().isEmpty()) { | |
93 | author = " (" + story.getAuthor() + ")"; | |
94 | } | |
95 | ||
96 | System.out.println(story.getLuid() + ": " + story.getTitle() | |
97 | + author); | |
98 | } | |
99 | } | |
b3b9a1cd | 100 | |
b31a0db0 NR |
101 | @Override |
102 | public void search(boolean sync) throws IOException { | |
103 | for (SupportType type : SupportType.values()) { | |
104 | if (BasicSearchable.getSearchable(type) != null) { | |
105 | System.out.println(type); | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
91b82a5c | 110 | @Override |
f76de465 NR |
111 | public void search(SupportType searchOn, String keywords, int page, |
112 | int item, boolean sync) throws IOException { | |
124442f1 NR |
113 | BasicSearchable search = BasicSearchable.getSearchable(searchOn); |
114 | ||
115 | if (page == 0) { | |
116 | System.out.println(search.searchPages(keywords)); | |
117 | } else { | |
118 | List<MetaData> metas = search.search(keywords, page); | |
119 | ||
120 | if (item == 0) { | |
121 | System.out.println("Page " + page + " of stories for: " | |
122 | + keywords); | |
123 | displayStories(metas); | |
124 | } else { | |
125 | // ! 1-based index ! | |
e92e4ae3 | 126 | if (item <= 0 || item > metas.size()) { |
124442f1 NR |
127 | throw new IOException("Index out of bounds: " + item); |
128 | } | |
129 | ||
130 | MetaData meta = metas.get(item - 1); | |
131 | displayStory(meta); | |
132 | } | |
133 | } | |
91b82a5c | 134 | } |
b3b9a1cd | 135 | |
91b82a5c | 136 | @Override |
b3b9a1cd | 137 | public void searchTag(SupportType searchOn, int page, int item, |
f76de465 | 138 | boolean sync, Integer... tags) throws IOException { |
b3b9a1cd | 139 | |
aaeabf3a NR |
140 | BasicSearchable search = BasicSearchable.getSearchable(searchOn); |
141 | SearchableTag stag = search.getTag(tags); | |
b3b9a1cd | 142 | |
aaeabf3a NR |
143 | if (stag == null) { |
144 | // TODO i18n | |
145 | System.out.println("Known tags: "); | |
146 | int i = 1; | |
147 | for (SearchableTag s : search.getTags()) { | |
148 | System.out.println(String.format("%d: %s", i, s.getName())); | |
149 | i++; | |
91b82a5c | 150 | } |
aaeabf3a | 151 | } else { |
91b82a5c NR |
152 | if (page <= 0) { |
153 | if (stag.isLeaf()) { | |
81acd363 | 154 | System.out.println(search.searchPages(stag)); |
91b82a5c NR |
155 | } else { |
156 | System.out.println(stag.getCount()); | |
157 | } | |
158 | } else { | |
159 | List<MetaData> metas = null; | |
160 | List<SearchableTag> subtags = null; | |
161 | int count; | |
b3b9a1cd | 162 | |
91b82a5c NR |
163 | if (stag.isLeaf()) { |
164 | metas = search.search(stag, page); | |
165 | count = metas.size(); | |
166 | } else { | |
167 | subtags = stag.getChildren(); | |
168 | count = subtags.size(); | |
169 | } | |
b3b9a1cd | 170 | |
91b82a5c NR |
171 | if (item > 0) { |
172 | if (item <= count) { | |
173 | if (metas != null) { | |
174 | MetaData meta = metas.get(item - 1); | |
124442f1 | 175 | displayStory(meta); |
91b82a5c NR |
176 | } else { |
177 | SearchableTag subtag = subtags.get(item - 1); | |
aaeabf3a | 178 | displayTag(subtag); |
91b82a5c NR |
179 | } |
180 | } else { | |
b3b9a1cd NR |
181 | System.out.println("Invalid item: only " + count |
182 | + " items found"); | |
91b82a5c NR |
183 | } |
184 | } else { | |
185 | if (metas != null) { | |
8b153400 NR |
186 | // TODO i18n |
187 | System.out.println(String.format("Content of %s: ", | |
aaeabf3a | 188 | stag.getFqName())); |
124442f1 | 189 | displayStories(metas); |
91b82a5c | 190 | } else { |
8b153400 NR |
191 | // TODO i18n |
192 | System.out.println(String.format("Subtags of %s: ", | |
aaeabf3a NR |
193 | stag.getFqName())); |
194 | displayTags(subtags); | |
91b82a5c NR |
195 | } |
196 | } | |
197 | } | |
91b82a5c NR |
198 | } |
199 | } | |
124442f1 | 200 | |
aaeabf3a NR |
201 | private void displayTag(SearchableTag subtag) { |
202 | // TODO: i18n | |
203 | String stories = "stories"; | |
204 | String num = StringUtils.formatNumber(subtag.getCount()); | |
205 | System.out.println(String.format("%s (%s), %s %s", subtag.getName(), | |
206 | subtag.getFqName(), num, stories)); | |
207 | } | |
208 | ||
124442f1 NR |
209 | private void displayStory(MetaData meta) { |
210 | System.out.println(meta.getTitle()); | |
211 | System.out.println(); | |
212 | System.out.println(meta.getUrl()); | |
213 | System.out.println(); | |
214 | System.out.println("Tags: " + meta.getTags()); | |
215 | System.out.println(); | |
216 | for (Paragraph para : meta.getResume()) { | |
217 | System.out.println(para.getContent()); | |
218 | System.out.println(""); | |
219 | } | |
220 | } | |
221 | ||
aaeabf3a NR |
222 | private void displayTags(List<SearchableTag> subtags) { |
223 | int i = 1; | |
224 | for (SearchableTag subtag : subtags) { | |
225 | String total = ""; | |
226 | if (subtag.getCount() > 0) { | |
227 | total = StringUtils.formatNumber(subtag.getCount()); | |
228 | } | |
229 | ||
230 | if (total.isEmpty()) { | |
231 | System.out | |
232 | .println(String.format("%d: %s", i, subtag.getName())); | |
233 | } else { | |
234 | System.out.println(String.format("%d: %s (%s)", i, | |
235 | subtag.getName(), total)); | |
236 | } | |
237 | ||
238 | i++; | |
239 | } | |
240 | } | |
241 | ||
124442f1 NR |
242 | private void displayStories(List<MetaData> metas) { |
243 | int i = 1; | |
244 | for (MetaData meta : metas) { | |
245 | System.out.println(i + ": " + meta.getTitle()); | |
246 | i++; | |
247 | } | |
248 | } | |
08fe2e33 | 249 | } |