allow pagination for search keywords
[fanfix.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.ArrayList;
5 import java.util.List;
6
7 import be.nikiroo.fanfix.Instance;
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;
13 import be.nikiroo.fanfix.reader.BasicReader;
14 import be.nikiroo.fanfix.searchable.BasicSearchable;
15 import be.nikiroo.fanfix.searchable.SearchableTag;
16 import be.nikiroo.fanfix.supported.SupportType;
17 import be.nikiroo.utils.StringUtils;
18
19 /**
20 * Command line {@link Story} reader.
21 * <p>
22 * Will output stories to the console.
23 *
24 * @author niki
25 */
26 class CliReader extends BasicReader {
27 @Override
28 public void read(boolean sync) throws IOException {
29 MetaData meta = getMeta();
30
31 if (meta == null) {
32 throw new IOException("No story to read");
33 }
34
35 String title = "";
36 String author = "";
37
38 if (meta.getTitle() != null) {
39 title = meta.getTitle();
40 }
41
42 if (meta.getAuthor() != null) {
43 author = "©" + meta.getAuthor();
44 if (meta.getDate() != null && !meta.getDate().isEmpty()) {
45 author = author + " (" + meta.getDate() + ")";
46 }
47 }
48
49 System.out.println(title);
50 System.out.println(author);
51 System.out.println("");
52
53 // TODO: progress?
54 for (Chapter chap : getStory(null)) {
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
66 public void read(int chapter) throws IOException {
67 MetaData meta = getMeta();
68
69 if (meta == null) {
70 throw new IOException("No story to read");
71 }
72
73 // TODO: progress?
74 if (chapter > getStory(null).getChapters().size()) {
75 System.err.println("Chapter " + chapter + ": no such chapter");
76 } else {
77 Chapter chap = getStory(null).getChapters().get(chapter - 1);
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
88 @Override
89 public void browse(String source) {
90 List<MetaData> stories;
91 stories = getLibrary().getListBySource(source);
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 }
103
104 @Override
105 public void search(SupportType searchOn, String keywords, int page,
106 int item, boolean sync) throws IOException {
107 // TODO
108 }
109
110 @Override
111 public void searchTag(SupportType searchOn, int page, int item,
112 boolean sync, Integer... tags) throws IOException {
113 BasicSearchable search = BasicSearchable.getSearchable(searchOn);
114 List<SearchableTag> stags = search.getTags();
115 String fqnTag = "";
116
117 SearchableTag stag = null;
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);
122 }
123
124 stag = stags.get(tagIndex - 1);
125 if (stag != null) {
126 search.fillTag(stag);
127 stags = stag.getChildren();
128 if (!fqnTag.isEmpty()) {
129 fqnTag += " / ";
130 }
131 fqnTag += stag.getName();
132 } else {
133 stags = new ArrayList<SearchableTag>();
134 break;
135 }
136 }
137
138 if (stag != null) {
139 if (page <= 0) {
140 if (stag.isLeaf()) {
141 search.search(stag, 1);
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;
150
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 }
158
159 if (item > 0) {
160 if (item <= count) {
161 if (metas != null) {
162 MetaData meta = metas.get(item - 1);
163 System.out.println(page + "/" + item + ": "
164 + meta.getTitle());
165 System.out.println();
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);
176
177 // TODO: i18n
178 String stories = "stories";
179 String num = StringUtils.formatNumber(subtag
180 .getCount());
181 System.out.println(String.format("%s (%s), %s %s",
182 subtag.getName(), fqnTag, num, stories));
183 }
184 } else {
185 System.out.println("Invalid item: only " + count
186 + " items found");
187 }
188 } else {
189 if (metas != null) {
190 // TODO i18n
191 System.out.println(String.format("Content of %s: ",
192 fqnTag));
193 int i = 1;
194 for (MetaData meta : metas) {
195 System.out.println(i + ": " + meta.getTitle());
196 i++;
197 }
198 } else {
199 // TODO i18n
200 System.out.println(String.format("Subtags of %s: ",
201 fqnTag));
202 int i = 1;
203 for (SearchableTag subtag : subtags) {
204 String total = "";
205 if (subtag.getCount() > 0) {
206 total = StringUtils.formatNumber(subtag
207 .getCount());
208 }
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
218 i++;
219 }
220 }
221 }
222 }
223 } else {
224 // TODO i18n
225 System.out.println("Known tags: ");
226 int i = 1;
227 for (SearchableTag s : stags) {
228 System.out.println(String.format("%d: %s", i, s.getName()));
229 i++;
230 }
231 }
232 }
233 }