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