search: cleanup
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / cli / CliReader.java
CommitLineData
16a81ef7 1package be.nikiroo.fanfix.reader.cli;
08fe2e33
NR
2
3import java.io.IOException;
08fe2e33
NR
4import java.util.List;
5
6import be.nikiroo.fanfix.Instance;
08fe2e33
NR
7import be.nikiroo.fanfix.bundles.StringId;
8import be.nikiroo.fanfix.data.Chapter;
9import be.nikiroo.fanfix.data.MetaData;
10import be.nikiroo.fanfix.data.Paragraph;
11import be.nikiroo.fanfix.data.Story;
16a81ef7 12import be.nikiroo.fanfix.reader.BasicReader;
91b82a5c
NR
13import be.nikiroo.fanfix.searchable.BasicSearchable;
14import be.nikiroo.fanfix.searchable.SearchableTag;
15import be.nikiroo.fanfix.supported.SupportType;
74a43961 16import 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 25class 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()) {
b3b9a1cd 156 search.search(stag, 1);
91b82a5c
NR
157 System.out.println(stag.getPages());
158 } else {
159 System.out.println(stag.getCount());
160 }
161 } else {
162 List<MetaData> metas = null;
163 List<SearchableTag> subtags = null;
164 int count;
b3b9a1cd 165
91b82a5c
NR
166 if (stag.isLeaf()) {
167 metas = search.search(stag, page);
168 count = metas.size();
169 } else {
170 subtags = stag.getChildren();
171 count = subtags.size();
172 }
b3b9a1cd 173
91b82a5c
NR
174 if (item > 0) {
175 if (item <= count) {
176 if (metas != null) {
177 MetaData meta = metas.get(item - 1);
124442f1 178 displayStory(meta);
91b82a5c
NR
179 } else {
180 SearchableTag subtag = subtags.get(item - 1);
aaeabf3a 181 displayTag(subtag);
91b82a5c
NR
182 }
183 } else {
b3b9a1cd
NR
184 System.out.println("Invalid item: only " + count
185 + " items found");
91b82a5c
NR
186 }
187 } else {
188 if (metas != null) {
8b153400
NR
189 // TODO i18n
190 System.out.println(String.format("Content of %s: ",
aaeabf3a 191 stag.getFqName()));
124442f1 192 displayStories(metas);
91b82a5c 193 } else {
8b153400
NR
194 // TODO i18n
195 System.out.println(String.format("Subtags of %s: ",
aaeabf3a
NR
196 stag.getFqName()));
197 displayTags(subtags);
91b82a5c
NR
198 }
199 }
200 }
91b82a5c
NR
201 }
202 }
124442f1 203
aaeabf3a
NR
204 private void displayTag(SearchableTag subtag) {
205 // TODO: i18n
206 String stories = "stories";
207 String num = StringUtils.formatNumber(subtag.getCount());
208 System.out.println(String.format("%s (%s), %s %s", subtag.getName(),
209 subtag.getFqName(), num, stories));
210 }
211
124442f1
NR
212 private void displayStory(MetaData meta) {
213 System.out.println(meta.getTitle());
214 System.out.println();
215 System.out.println(meta.getUrl());
216 System.out.println();
217 System.out.println("Tags: " + meta.getTags());
218 System.out.println();
219 for (Paragraph para : meta.getResume()) {
220 System.out.println(para.getContent());
221 System.out.println("");
222 }
223 }
224
aaeabf3a
NR
225 private void displayTags(List<SearchableTag> subtags) {
226 int i = 1;
227 for (SearchableTag subtag : subtags) {
228 String total = "";
229 if (subtag.getCount() > 0) {
230 total = StringUtils.formatNumber(subtag.getCount());
231 }
232
233 if (total.isEmpty()) {
234 System.out
235 .println(String.format("%d: %s", i, subtag.getName()));
236 } else {
237 System.out.println(String.format("%d: %s (%s)", i,
238 subtag.getName(), total));
239 }
240
241 i++;
242 }
243 }
244
124442f1
NR
245 private void displayStories(List<MetaData> metas) {
246 int i = 1;
247 for (MetaData meta : metas) {
248 System.out.println(i + ": " + meta.getTitle());
249 i++;
250 }
251 }
08fe2e33 252}