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