Revert "fix some default paths"
[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) throws IOException {
89 List<MetaData> stories = getLibrary().getListBySource(source);
90
91 for (MetaData story : stories) {
92 String author = "";
93 if (story.getAuthor() != null && !story.getAuthor().isEmpty()) {
94 author = " (" + story.getAuthor() + ")";
95 }
96
97 System.out.println(story.getLuid() + ": " + story.getTitle()
98 + author);
99 }
100 }
101
102 @Override
103 public void search(boolean sync) throws IOException {
104 for (SupportType type : SupportType.values()) {
105 if (BasicSearchable.getSearchable(type) != null) {
106 System.out.println(type);
107 }
108 }
109 }
110
111 @Override
112 public void search(SupportType searchOn, String keywords, int page,
113 int item, boolean sync) throws IOException {
114 BasicSearchable search = BasicSearchable.getSearchable(searchOn);
115
116 if (page == 0) {
117 System.out.println(search.searchPages(keywords));
118 } else {
119 List<MetaData> metas = search.search(keywords, page);
120
121 if (item == 0) {
122 System.out.println("Page " + page + " of stories for: "
123 + keywords);
124 displayStories(metas);
125 } else {
126 // ! 1-based index !
127 if (item <= 0 || item > metas.size()) {
128 throw new IOException("Index out of bounds: " + item);
129 }
130
131 MetaData meta = metas.get(item - 1);
132 displayStory(meta);
133 }
134 }
135 }
136
137 @Override
138 public void searchTag(SupportType searchOn, int page, int item,
139 boolean sync, Integer... tags) throws IOException {
140
141 BasicSearchable search = BasicSearchable.getSearchable(searchOn);
142 SearchableTag stag = search.getTag(tags);
143
144 if (stag == null) {
145 // TODO i18n
146 System.out.println("Known tags: ");
147 int i = 1;
148 for (SearchableTag s : search.getTags()) {
149 System.out.println(String.format("%d: %s", i, s.getName()));
150 i++;
151 }
152 } else {
153 if (page <= 0) {
154 if (stag.isLeaf()) {
155 System.out.println(search.searchPages(stag));
156 } else {
157 System.out.println(stag.getCount());
158 }
159 } else {
160 List<MetaData> metas = null;
161 List<SearchableTag> subtags = null;
162 int count;
163
164 if (stag.isLeaf()) {
165 metas = search.search(stag, page);
166 count = metas.size();
167 } else {
168 subtags = stag.getChildren();
169 count = subtags.size();
170 }
171
172 if (item > 0) {
173 if (item <= count) {
174 if (metas != null) {
175 MetaData meta = metas.get(item - 1);
176 displayStory(meta);
177 } else {
178 SearchableTag subtag = subtags.get(item - 1);
179 displayTag(subtag);
180 }
181 } else {
182 System.out.println("Invalid item: only " + count
183 + " items found");
184 }
185 } else {
186 if (metas != null) {
187 // TODO i18n
188 System.out.println(String.format("Content of %s: ",
189 stag.getFqName()));
190 displayStories(metas);
191 } else {
192 // TODO i18n
193 System.out.println(String.format("Subtags of %s: ",
194 stag.getFqName()));
195 displayTags(subtags);
196 }
197 }
198 }
199 }
200 }
201
202 private void displayTag(SearchableTag subtag) {
203 // TODO: i18n
204 String stories = "stories";
205 String num = StringUtils.formatNumber(subtag.getCount());
206 System.out.println(String.format("%s (%s), %s %s", subtag.getName(),
207 subtag.getFqName(), num, stories));
208 }
209
210 private void displayStory(MetaData meta) {
211 System.out.println(meta.getTitle());
212 System.out.println();
213 System.out.println(meta.getUrl());
214 System.out.println();
215 System.out.println("Tags: " + meta.getTags());
216 System.out.println();
217 for (Paragraph para : meta.getResume()) {
218 System.out.println(para.getContent());
219 System.out.println("");
220 }
221 }
222
223 private void displayTags(List<SearchableTag> subtags) {
224 int i = 1;
225 for (SearchableTag subtag : subtags) {
226 String total = "";
227 if (subtag.getCount() > 0) {
228 total = StringUtils.formatNumber(subtag.getCount());
229 }
230
231 if (total.isEmpty()) {
232 System.out
233 .println(String.format("%d: %s", i, subtag.getName()));
234 } else {
235 System.out.println(String.format("%d: %s (%s)", i,
236 subtag.getName(), total));
237 }
238
239 i++;
240 }
241 }
242
243 private void displayStories(List<MetaData> metas) {
244 int i = 1;
245 for (MetaData meta : metas) {
246 System.out.println(i + ": " + meta.getTitle());
247 i++;
248 }
249 }
250 }