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