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