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