try +1
[fanfix.git] / src / be / nikiroo / fanfix / reader / cli / CliReader.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.reader.cli;
2
3import java.io.IOException;
4import java.util.List;
5
6import be.nikiroo.fanfix.Instance;
7import be.nikiroo.fanfix.bundles.StringId;
8import be.nikiroo.fanfix.data.Chapter;
9import be.nikiroo.fanfix.data.MetaData;
10import be.nikiroo.fanfix.data.Paragraph;
11import be.nikiroo.fanfix.data.Story;
12import be.nikiroo.fanfix.reader.BasicReader;
13
14/**
15 * Command line {@link Story} reader.
16 * <p>
17 * Will output stories to the console.
18 *
19 * @author niki
20 */
21class CliReader extends BasicReader {
22 @Override
23 public void read() throws IOException {
24 MetaData meta = getMeta();
25
26 if (meta == null) {
27 throw new IOException("No story to read");
28 }
29
30 String title = "";
31 String author = "";
32
33 if (meta.getTitle() != null) {
34 title = meta.getTitle();
35 }
36
37 if (meta.getAuthor() != null) {
38 author = "©" + meta.getAuthor();
39 if (meta.getDate() != null && !meta.getDate().isEmpty()) {
40 author = author + " (" + meta.getDate() + ")";
41 }
42 }
43
44 System.out.println(title);
45 System.out.println(author);
46 System.out.println("");
47
48 // TODO: progress?
49 for (Chapter chap : getStory(null)) {
50 if (chap.getName() != null && !chap.getName().isEmpty()) {
51 System.out.println(Instance.getTrans().getString(
52 StringId.CHAPTER_NAMED, chap.getNumber(),
53 chap.getName()));
54 } else {
55 System.out.println(Instance.getTrans().getString(
56 StringId.CHAPTER_UNNAMED, chap.getNumber()));
57 }
58 }
59 }
60
61 public void read(int chapter) throws IOException {
62 MetaData meta = getMeta();
63
64 if (meta == null) {
65 throw new IOException("No story to read");
66 }
67
68 // TODO: progress?
69 if (chapter > getStory(null).getChapters().size()) {
70 System.err.println("Chapter " + chapter + ": no such chapter");
71 } else {
72 Chapter chap = getStory(null).getChapters().get(chapter - 1);
73 System.out.println("Chapter " + chap.getNumber() + ": "
74 + chap.getName());
75
76 for (Paragraph para : chap) {
77 System.out.println(para.getContent());
78 System.out.println("");
79 }
80 }
81 }
82
83 @Override
84 public void browse(String source) {
85 List<MetaData> stories;
86 stories = getLibrary().getListBySource(source);
87
88 for (MetaData story : stories) {
89 String author = "";
90 if (story.getAuthor() != null && !story.getAuthor().isEmpty()) {
91 author = " (" + story.getAuthor() + ")";
92 }
93
94 System.out.println(story.getLuid() + ": " + story.getTitle()
95 + author);
96 }
97 }
98}