search: fixes, CLI now working
[fanfix.git] / src / be / nikiroo / fanfix / reader / cli / CliReader.java
CommitLineData
16a81ef7 1package be.nikiroo.fanfix.reader.cli;
08fe2e33
NR
2
3import java.io.IOException;
91b82a5c 4import java.util.ArrayList;
08fe2e33
NR
5import java.util.List;
6
7import be.nikiroo.fanfix.Instance;
08fe2e33
NR
8import be.nikiroo.fanfix.bundles.StringId;
9import be.nikiroo.fanfix.data.Chapter;
10import be.nikiroo.fanfix.data.MetaData;
11import be.nikiroo.fanfix.data.Paragraph;
12import be.nikiroo.fanfix.data.Story;
16a81ef7 13import be.nikiroo.fanfix.reader.BasicReader;
91b82a5c
NR
14import be.nikiroo.fanfix.searchable.BasicSearchable;
15import be.nikiroo.fanfix.searchable.SearchableTag;
16import be.nikiroo.fanfix.supported.SupportType;
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
91b82a5c 103 @Override
b3b9a1cd
NR
104 public void search(SupportType searchOn, String keywords, int page, int item)
105 throws IOException {
106
91b82a5c 107 }
b3b9a1cd 108
91b82a5c 109 @Override
b3b9a1cd
NR
110 public void searchTag(SupportType searchOn, int page, int item,
111 String... tags) throws IOException {
91b82a5c
NR
112 BasicSearchable search = BasicSearchable.getSearchable(searchOn);
113 List<SearchableTag> stags = search.getTags();
b3b9a1cd 114
91b82a5c
NR
115 SearchableTag stag = null;
116 for (String tag : tags) {
117 stag = null;
b3b9a1cd 118 for (int i = 0; i < stags.size(); i++) {
91b82a5c
NR
119 if (stags.get(i).getName().equalsIgnoreCase(tag)) {
120 stag = stags.get(i);
121 break;
122 }
123 }
b3b9a1cd 124
91b82a5c
NR
125 if (stag != null) {
126 search.fillTag(stag);
127 stags = stag.getChildren();
128 } else {
129 stags = new ArrayList<SearchableTag>();
130 break;
131 }
132 }
133
134 if (stag != null) {
135 if (page <= 0) {
136 if (stag.isLeaf()) {
b3b9a1cd 137 search.search(stag, 1);
91b82a5c
NR
138 System.out.println(stag.getPages());
139 } else {
140 System.out.println(stag.getCount());
141 }
142 } else {
143 List<MetaData> metas = null;
144 List<SearchableTag> subtags = null;
145 int count;
b3b9a1cd 146
91b82a5c
NR
147 if (stag.isLeaf()) {
148 metas = search.search(stag, page);
149 count = metas.size();
150 } else {
151 subtags = stag.getChildren();
152 count = subtags.size();
153 }
b3b9a1cd 154
91b82a5c
NR
155 if (item > 0) {
156 if (item <= count) {
157 if (metas != null) {
158 MetaData meta = metas.get(item - 1);
b3b9a1cd
NR
159 System.out.println(page + "/" + item + ": "
160 + meta.getTitle());
161 System.out.println();
91b82a5c
NR
162 System.out.println(meta.getUrl());
163 System.out.println();
164 System.out.println("Tags: " + meta.getTags());
165 System.out.println();
166 for (Paragraph para : meta.getResume()) {
167 System.out.println(para.getContent());
168 System.out.println("");
169 }
170 } else {
171 SearchableTag subtag = subtags.get(item - 1);
b3b9a1cd
NR
172
173 String sp = "";
174 if (subtag.getParent() != null) {
175 List<String> parents = new ArrayList<String>();
176 for (SearchableTag parent = subtag.getParent(); parent != null; parent = parent
177 .getParent()) {
178 parents.add(parent.getName());
179 }
180 for (String parent : parents) {
181 if (!sp.isEmpty()) {
182 sp += " / ";
183 }
184 sp += parent;
185 }
186 }
187
188 // TODO: i18n
189 if (sp.isEmpty()) {
190 System.out.println(String.format(
191 "%d/%d: %s, %d %s", page, item,
192 subtag.getName(), subtag.getCount(),
193 "stories"));
194 } else {
195 System.out.println(String.format(
196 "%d/%d: %s (%s), %d %s", page, item,
197 subtag.getName(), sp,
198 subtag.getCount(), "stories"));
199 }
91b82a5c
NR
200 }
201 } else {
b3b9a1cd
NR
202 System.out.println("Invalid item: only " + count
203 + " items found");
91b82a5c
NR
204 }
205 } else {
206 if (metas != null) {
207 int i = 0;
208 for (MetaData meta : metas) {
b3b9a1cd
NR
209 System.out
210 .println((i + 1) + ": " + meta.getTitle());
91b82a5c
NR
211 i++;
212 }
213 } else {
b3b9a1cd 214 int i = 1;
91b82a5c 215 for (SearchableTag subtag : subtags) {
b3b9a1cd 216 String total = "";
91b82a5c 217 if (subtag.getCount() > 0) {
b3b9a1cd
NR
218 // TODO: use StringUtils fromNumber
219 total = " (" + subtag.getCount() + ")";
91b82a5c 220 }
b3b9a1cd
NR
221 System.out.println(i + ": " + subtag.getName()
222 + total);
223 i++;
91b82a5c
NR
224 }
225 }
226 }
227 }
228 } else {
229 for (SearchableTag s : stags) {
230 System.out.println(s.getName());
231 }
232 }
233 }
08fe2e33 234}