Code cleanup 3 and update jexer-niki :
[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 20class CliReader extends BasicReader {
89cb07a6 21 public void read() throws IOException {
bc2ea776
NR
22 MetaData meta = getMeta();
23
24 if (meta == null) {
89cb07a6 25 throw new IOException("No story to read");
08fe2e33 26 }
08fe2e33 27
08fe2e33
NR
28 String title = "";
29 String author = "";
30
bc2ea776
NR
31 if (meta.getTitle() != null) {
32 title = meta.getTitle();
33 }
08fe2e33 34
bc2ea776
NR
35 if (meta.getAuthor() != null) {
36 author = "©" + meta.getAuthor();
37 if (meta.getDate() != null && !meta.getDate().isEmpty()) {
38 author = author + " (" + meta.getDate() + ")";
08fe2e33
NR
39 }
40 }
41
42 System.out.println(title);
43 System.out.println(author);
44 System.out.println("");
45
bc2ea776
NR
46 // TODO: progress?
47 for (Chapter chap : getStory(null)) {
08fe2e33
NR
48 if (chap.getName() != null && !chap.getName().isEmpty()) {
49 System.out.println(Instance.getTrans().getString(
50 StringId.CHAPTER_NAMED, chap.getNumber(),
51 chap.getName()));
52 } else {
53 System.out.println(Instance.getTrans().getString(
54 StringId.CHAPTER_UNNAMED, chap.getNumber()));
55 }
56 }
57 }
58
edd46289 59 public void read(int chapter) throws IOException {
bc2ea776
NR
60 MetaData meta = getMeta();
61
62 if (meta == null) {
edd46289
NR
63 throw new IOException("No story to read");
64 }
65
bc2ea776
NR
66 // TODO: progress?
67 if (chapter > getStory(null).getChapters().size()) {
08fe2e33
NR
68 System.err.println("Chapter " + chapter + ": no such chapter");
69 } else {
bc2ea776 70 Chapter chap = getStory(null).getChapters().get(chapter - 1);
08fe2e33
NR
71 System.out.println("Chapter " + chap.getNumber() + ": "
72 + chap.getName());
73
74 for (Paragraph para : chap) {
75 System.out.println(para.getContent());
76 System.out.println("");
77 }
78 }
79 }
80
b0e88ebd 81 public void browse(String source) {
08fe2e33 82 List<MetaData> stories;
b0e88ebd 83 stories = getLibrary().getListBySource(source);
08fe2e33
NR
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}