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; | |
74a43961 | 17 | import be.nikiroo.utils.StringUtils; |
08fe2e33 NR |
18 | |
19 | /** | |
20 | * Command line {@link Story} reader. | |
21 | * <p> | |
22 | * Will output stories to the console. | |
23 | * | |
24 | * @author niki | |
25 | */ | |
3727aae2 | 26 | class CliReader extends BasicReader { |
211f7ddb | 27 | @Override |
350bc060 | 28 | public void read(boolean sync) throws IOException { |
bc2ea776 NR |
29 | MetaData meta = getMeta(); |
30 | ||
31 | if (meta == null) { | |
89cb07a6 | 32 | throw new IOException("No story to read"); |
08fe2e33 | 33 | } |
08fe2e33 | 34 | |
08fe2e33 NR |
35 | String title = ""; |
36 | String author = ""; | |
37 | ||
bc2ea776 NR |
38 | if (meta.getTitle() != null) { |
39 | title = meta.getTitle(); | |
40 | } | |
08fe2e33 | 41 | |
bc2ea776 NR |
42 | if (meta.getAuthor() != null) { |
43 | author = "©" + meta.getAuthor(); | |
44 | if (meta.getDate() != null && !meta.getDate().isEmpty()) { | |
45 | author = author + " (" + meta.getDate() + ")"; | |
08fe2e33 NR |
46 | } |
47 | } | |
48 | ||
49 | System.out.println(title); | |
50 | System.out.println(author); | |
51 | System.out.println(""); | |
52 | ||
bc2ea776 NR |
53 | // TODO: progress? |
54 | for (Chapter chap : getStory(null)) { | |
08fe2e33 NR |
55 | if (chap.getName() != null && !chap.getName().isEmpty()) { |
56 | System.out.println(Instance.getTrans().getString( | |
57 | StringId.CHAPTER_NAMED, chap.getNumber(), | |
58 | chap.getName())); | |
59 | } else { | |
60 | System.out.println(Instance.getTrans().getString( | |
61 | StringId.CHAPTER_UNNAMED, chap.getNumber())); | |
62 | } | |
63 | } | |
64 | } | |
65 | ||
edd46289 | 66 | public void read(int chapter) throws IOException { |
bc2ea776 NR |
67 | MetaData meta = getMeta(); |
68 | ||
69 | if (meta == null) { | |
edd46289 NR |
70 | throw new IOException("No story to read"); |
71 | } | |
72 | ||
bc2ea776 NR |
73 | // TODO: progress? |
74 | if (chapter > getStory(null).getChapters().size()) { | |
08fe2e33 NR |
75 | System.err.println("Chapter " + chapter + ": no such chapter"); |
76 | } else { | |
bc2ea776 | 77 | Chapter chap = getStory(null).getChapters().get(chapter - 1); |
08fe2e33 NR |
78 | System.out.println("Chapter " + chap.getNumber() + ": " |
79 | + chap.getName()); | |
80 | ||
81 | for (Paragraph para : chap) { | |
82 | System.out.println(para.getContent()); | |
83 | System.out.println(""); | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
211f7ddb | 88 | @Override |
b0e88ebd | 89 | public void browse(String source) { |
08fe2e33 | 90 | List<MetaData> stories; |
b0e88ebd | 91 | stories = getLibrary().getListBySource(source); |
08fe2e33 NR |
92 | |
93 | for (MetaData story : stories) { | |
94 | String author = ""; | |
95 | if (story.getAuthor() != null && !story.getAuthor().isEmpty()) { | |
96 | author = " (" + story.getAuthor() + ")"; | |
97 | } | |
98 | ||
99 | System.out.println(story.getLuid() + ": " + story.getTitle() | |
100 | + author); | |
101 | } | |
102 | } | |
b3b9a1cd | 103 | |
91b82a5c | 104 | @Override |
b3b9a1cd NR |
105 | public void search(SupportType searchOn, String keywords, int page, int item) |
106 | throws IOException { | |
107 | ||
91b82a5c | 108 | } |
b3b9a1cd | 109 | |
91b82a5c | 110 | @Override |
b3b9a1cd NR |
111 | public void searchTag(SupportType searchOn, int page, int item, |
112 | String... tags) throws IOException { | |
91b82a5c NR |
113 | BasicSearchable search = BasicSearchable.getSearchable(searchOn); |
114 | List<SearchableTag> stags = search.getTags(); | |
b3b9a1cd | 115 | |
91b82a5c NR |
116 | SearchableTag stag = null; |
117 | for (String tag : tags) { | |
118 | stag = null; | |
b3b9a1cd | 119 | for (int i = 0; i < stags.size(); i++) { |
91b82a5c NR |
120 | if (stags.get(i).getName().equalsIgnoreCase(tag)) { |
121 | stag = stags.get(i); | |
122 | break; | |
123 | } | |
124 | } | |
b3b9a1cd | 125 | |
91b82a5c NR |
126 | if (stag != null) { |
127 | search.fillTag(stag); | |
128 | stags = stag.getChildren(); | |
129 | } else { | |
130 | stags = new ArrayList<SearchableTag>(); | |
131 | break; | |
132 | } | |
133 | } | |
134 | ||
135 | if (stag != null) { | |
136 | if (page <= 0) { | |
137 | if (stag.isLeaf()) { | |
b3b9a1cd | 138 | search.search(stag, 1); |
91b82a5c NR |
139 | System.out.println(stag.getPages()); |
140 | } else { | |
141 | System.out.println(stag.getCount()); | |
142 | } | |
143 | } else { | |
144 | List<MetaData> metas = null; | |
145 | List<SearchableTag> subtags = null; | |
146 | int count; | |
b3b9a1cd | 147 | |
91b82a5c NR |
148 | if (stag.isLeaf()) { |
149 | metas = search.search(stag, page); | |
150 | count = metas.size(); | |
151 | } else { | |
152 | subtags = stag.getChildren(); | |
153 | count = subtags.size(); | |
154 | } | |
b3b9a1cd | 155 | |
91b82a5c NR |
156 | if (item > 0) { |
157 | if (item <= count) { | |
158 | if (metas != null) { | |
159 | MetaData meta = metas.get(item - 1); | |
b3b9a1cd NR |
160 | System.out.println(page + "/" + item + ": " |
161 | + meta.getTitle()); | |
162 | System.out.println(); | |
91b82a5c NR |
163 | System.out.println(meta.getUrl()); |
164 | System.out.println(); | |
165 | System.out.println("Tags: " + meta.getTags()); | |
166 | System.out.println(); | |
167 | for (Paragraph para : meta.getResume()) { | |
168 | System.out.println(para.getContent()); | |
169 | System.out.println(""); | |
170 | } | |
171 | } else { | |
172 | SearchableTag subtag = subtags.get(item - 1); | |
b3b9a1cd NR |
173 | |
174 | String sp = ""; | |
175 | if (subtag.getParent() != null) { | |
176 | List<String> parents = new ArrayList<String>(); | |
177 | for (SearchableTag parent = subtag.getParent(); parent != null; parent = parent | |
178 | .getParent()) { | |
179 | parents.add(parent.getName()); | |
180 | } | |
181 | for (String parent : parents) { | |
182 | if (!sp.isEmpty()) { | |
183 | sp += " / "; | |
184 | } | |
185 | sp += parent; | |
186 | } | |
187 | } | |
188 | ||
189 | // TODO: i18n | |
53f76130 NR |
190 | String stories = "stories"; |
191 | String num = StringUtils.formatNumber(subtag | |
192 | .getCount()); | |
b3b9a1cd NR |
193 | if (sp.isEmpty()) { |
194 | System.out.println(String.format( | |
53f76130 NR |
195 | "%d/%d: %s, %s %s", page, item, |
196 | subtag.getName(), num, stories)); | |
b3b9a1cd NR |
197 | } else { |
198 | System.out.println(String.format( | |
53f76130 NR |
199 | "%d/%d: %s (%s), %s %s", page, item, |
200 | subtag.getName(), sp, num, stories)); | |
b3b9a1cd | 201 | } |
91b82a5c NR |
202 | } |
203 | } else { | |
b3b9a1cd NR |
204 | System.out.println("Invalid item: only " + count |
205 | + " items found"); | |
91b82a5c NR |
206 | } |
207 | } else { | |
208 | if (metas != null) { | |
209 | int i = 0; | |
210 | for (MetaData meta : metas) { | |
b3b9a1cd NR |
211 | System.out |
212 | .println((i + 1) + ": " + meta.getTitle()); | |
91b82a5c NR |
213 | i++; |
214 | } | |
215 | } else { | |
b3b9a1cd | 216 | int i = 1; |
91b82a5c | 217 | for (SearchableTag subtag : subtags) { |
b3b9a1cd | 218 | String total = ""; |
91b82a5c | 219 | if (subtag.getCount() > 0) { |
74a43961 NR |
220 | total = StringUtils.formatNumber(subtag |
221 | .getCount()); | |
91b82a5c | 222 | } |
74a43961 NR |
223 | |
224 | if (total.isEmpty()) { | |
225 | System.out.println(String.format("%d: %s", i, | |
226 | subtag.getName())); | |
227 | } else { | |
228 | System.out.println(String.format("%d: %s (%s)", | |
229 | i, subtag.getName(), total)); | |
230 | } | |
231 | ||
b3b9a1cd | 232 | i++; |
91b82a5c NR |
233 | } |
234 | } | |
235 | } | |
236 | } | |
237 | } else { | |
238 | for (SearchableTag s : stags) { | |
239 | System.out.println(s.getName()); | |
240 | } | |
241 | } | |
242 | } | |
08fe2e33 | 243 | } |