556052149d7c120305df7ab206e10b80618af703
1 package be
.nikiroo
.fanfix
.reader
.cli
;
3 import java
.io
.IOException
;
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
;
19 * Command line {@link Story} reader.
21 * Will output stories to the console.
25 class CliReader
extends BasicReader
{
27 public void read(boolean sync
) throws IOException
{
28 MetaData meta
= getMeta();
31 throw new IOException("No story to read");
37 if (meta
.getTitle() != null) {
38 title
= meta
.getTitle();
41 if (meta
.getAuthor() != null) {
42 author
= "©" + meta
.getAuthor();
43 if (meta
.getDate() != null && !meta
.getDate().isEmpty()) {
44 author
= author
+ " (" + meta
.getDate() + ")";
48 System
.out
.println(title
);
49 System
.out
.println(author
);
50 System
.out
.println("");
53 for (Chapter chap
: getStory(null)) {
54 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
55 System
.out
.println(Instance
.getInstance().getTrans().getString(StringId
.CHAPTER_NAMED
, chap
.getNumber(),
59 Instance
.getInstance().getTrans().getString(StringId
.CHAPTER_UNNAMED
, chap
.getNumber()));
64 public void read(int chapter
) throws IOException
{
65 MetaData meta
= getMeta();
68 throw new IOException("No story to read");
72 if (chapter
> getStory(null).getChapters().size()) {
73 System
.err
.println("Chapter " + chapter
+ ": no such chapter");
75 Chapter chap
= getStory(null).getChapters().get(chapter
- 1);
76 System
.out
.println("Chapter " + chap
.getNumber() + ": "
79 for (Paragraph para
: chap
) {
80 System
.out
.println(para
.getContent());
81 System
.out
.println("");
87 public void browse(String source
) throws IOException
{
88 List
<MetaData
> stories
= getLibrary().getListBySource(source
);
90 for (MetaData story
: stories
) {
92 if (story
.getAuthor() != null && !story
.getAuthor().isEmpty()) {
93 author
= " (" + story
.getAuthor() + ")";
96 System
.out
.println(story
.getLuid() + ": " + story
.getTitle()
102 public void search(boolean sync
) throws IOException
{
103 for (SupportType type
: SupportType
.values()) {
104 if (BasicSearchable
.getSearchable(type
) != null) {
105 System
.out
.println(type
);
111 public void search(SupportType searchOn
, String keywords
, int page
,
112 int item
, boolean sync
) throws IOException
{
113 BasicSearchable search
= BasicSearchable
.getSearchable(searchOn
);
116 System
.out
.println(search
.searchPages(keywords
));
118 List
<MetaData
> metas
= search
.search(keywords
, page
);
121 System
.out
.println("Page " + page
+ " of stories for: "
123 displayStories(metas
);
126 if (item
<= 0 || item
> metas
.size()) {
127 throw new IOException("Index out of bounds: " + item
);
130 MetaData meta
= metas
.get(item
- 1);
137 public void searchTag(SupportType searchOn
, int page
, int item
,
138 boolean sync
, Integer
... tags
) throws IOException
{
140 BasicSearchable search
= BasicSearchable
.getSearchable(searchOn
);
141 SearchableTag stag
= search
.getTag(tags
);
145 System
.out
.println("Known tags: ");
147 for (SearchableTag s
: search
.getTags()) {
148 System
.out
.println(String
.format("%d: %s", i
, s
.getName()));
154 System
.out
.println(search
.searchPages(stag
));
156 System
.out
.println(stag
.getCount());
159 List
<MetaData
> metas
= null;
160 List
<SearchableTag
> subtags
= null;
164 metas
= search
.search(stag
, page
);
165 count
= metas
.size();
167 subtags
= stag
.getChildren();
168 count
= subtags
.size();
174 MetaData meta
= metas
.get(item
- 1);
177 SearchableTag subtag
= subtags
.get(item
- 1);
181 System
.out
.println("Invalid item: only " + count
187 System
.out
.println(String
.format("Content of %s: ",
189 displayStories(metas
);
192 System
.out
.println(String
.format("Subtags of %s: ",
194 displayTags(subtags
);
201 private void displayTag(SearchableTag subtag
) {
203 String stories
= "stories";
204 String num
= StringUtils
.formatNumber(subtag
.getCount());
205 System
.out
.println(String
.format("%s (%s), %s %s", subtag
.getName(),
206 subtag
.getFqName(), num
, stories
));
209 private void displayStory(MetaData meta
) {
210 System
.out
.println(meta
.getTitle());
211 System
.out
.println();
212 System
.out
.println(meta
.getUrl());
213 System
.out
.println();
214 System
.out
.println("Tags: " + meta
.getTags());
215 System
.out
.println();
216 for (Paragraph para
: meta
.getResume()) {
217 System
.out
.println(para
.getContent());
218 System
.out
.println("");
222 private void displayTags(List
<SearchableTag
> subtags
) {
224 for (SearchableTag subtag
: subtags
) {
226 if (subtag
.getCount() > 0) {
227 total
= StringUtils
.formatNumber(subtag
.getCount());
230 if (total
.isEmpty()) {
232 .println(String
.format("%d: %s", i
, subtag
.getName()));
234 System
.out
.println(String
.format("%d: %s (%s)", i
,
235 subtag
.getName(), total
));
242 private void displayStories(List
<MetaData
> metas
) {
244 for (MetaData meta
: metas
) {
245 System
.out
.println(i
+ ": " + meta
.getTitle());