search: cleanup
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / cli / CliReader.java
1 package be.nikiroo.fanfix.reader.cli;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import be.nikiroo.fanfix.Instance;
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;
12 import be.nikiroo.fanfix.reader.BasicReader;
13 import be.nikiroo.fanfix.searchable.BasicSearchable;
14 import be.nikiroo.fanfix.searchable.SearchableTag;
15 import be.nikiroo.fanfix.supported.SupportType;
16 import be.nikiroo.utils.StringUtils;
17
18 /**
19 * Command line {@link Story} reader.
20 * <p>
21 * Will output stories to the console.
22 *
23 * @author niki
24 */
25 class CliReader extends BasicReader {
26 @Override
27 public void read(boolean sync) throws IOException {
28 MetaData meta = getMeta();
29
30 if (meta == null) {
31 throw new IOException("No story to read");
32 }
33
34 String title = "";
35 String author = "";
36
37 if (meta.getTitle() != null) {
38 title = meta.getTitle();
39 }
40
41 if (meta.getAuthor() != null) {
42 author = "©" + meta.getAuthor();
43 if (meta.getDate() != null && !meta.getDate().isEmpty()) {
44 author = author + " (" + meta.getDate() + ")";
45 }
46 }
47
48 System.out.println(title);
49 System.out.println(author);
50 System.out.println("");
51
52 // TODO: progress?
53 for (Chapter chap : getStory(null)) {
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
65 public void read(int chapter) throws IOException {
66 MetaData meta = getMeta();
67
68 if (meta == null) {
69 throw new IOException("No story to read");
70 }
71
72 // TODO: progress?
73 if (chapter > getStory(null).getChapters().size()) {
74 System.err.println("Chapter " + chapter + ": no such chapter");
75 } else {
76 Chapter chap = getStory(null).getChapters().get(chapter - 1);
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
87 @Override
88 public void browse(String source) {
89 List<MetaData> stories;
90 stories = getLibrary().getListBySource(source);
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 }
102
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
112 @Override
113 public void search(SupportType searchOn, String keywords, int page,
114 int item, boolean sync) throws IOException {
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 !
128 if (item <= 0 || item > metas.size()) {
129 throw new IOException("Index out of bounds: " + item);
130 }
131
132 MetaData meta = metas.get(item - 1);
133 displayStory(meta);
134 }
135 }
136 }
137
138 @Override
139 public void searchTag(SupportType searchOn, int page, int item,
140 boolean sync, Integer... tags) throws IOException {
141
142 BasicSearchable search = BasicSearchable.getSearchable(searchOn);
143 SearchableTag stag = search.getTag(tags);
144
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++;
152 }
153 } else {
154 if (page <= 0) {
155 if (stag.isLeaf()) {
156 search.search(stag, 1);
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;
165
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 }
173
174 if (item > 0) {
175 if (item <= count) {
176 if (metas != null) {
177 MetaData meta = metas.get(item - 1);
178 displayStory(meta);
179 } else {
180 SearchableTag subtag = subtags.get(item - 1);
181 displayTag(subtag);
182 }
183 } else {
184 System.out.println("Invalid item: only " + count
185 + " items found");
186 }
187 } else {
188 if (metas != null) {
189 // TODO i18n
190 System.out.println(String.format("Content of %s: ",
191 stag.getFqName()));
192 displayStories(metas);
193 } else {
194 // TODO i18n
195 System.out.println(String.format("Subtags of %s: ",
196 stag.getFqName()));
197 displayTags(subtags);
198 }
199 }
200 }
201 }
202 }
203
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
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
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
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 }
252 }