1
0
Fork 0
mirror of https://github.com/lise-henry/crowbook synced 2024-05-28 14:06:29 +02:00

More emojis

This commit is contained in:
Elisabeth Henry 2017-10-05 21:24:21 +02:00
parent be391c753b
commit e57332c520
5 changed files with 108 additions and 55 deletions

1
Cargo.lock generated
View File

@ -21,6 +21,7 @@ dependencies = [
"rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)",
"simplelog 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syntect 1.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -39,7 +39,7 @@ doc = false
[features]
default = ["binary", "proofread", "syntect"]
proofread = ["caribon", "hyper", "url"]
binary = ["clap", "simplelog"]
binary = ["clap", "simplelog", "tempdir"]
[build-dependencies]
crowbook-intl = "0.2"
@ -67,6 +67,7 @@ clap = { version = "2.19", optional = true }
simplelog = { version = "0.4", optional = true }
url = { version = "1", optional = true }
syntect = { version = "1", optional = true }
tempdir = { version = "0.3", optional = true }
[dependencies.hyper]
version = "0.10"

View File

@ -1,5 +1,6 @@
use crowbook::Book;
use clap::{App, Arg, ArgMatches, AppSettings};
use console::{style, Emoji};
use std::io::{self, Write};
use std::process::exit;
@ -7,6 +8,23 @@ use std::fs;
use std::env;
static BIRD: &str = "🐦 ";
static ERROR: &str = "💣 ";
static WARNING: &str = "⚠️ ";
pub fn print_warning() {
eprint!("{}", style(WARNING).yellow());
eprintln!("{}", style(lformat!("Crowbook exited successfully, but the following errors occurred:")).yellow());
}
/// Display version number
pub fn display_header() {
eprint!("{}", style(BIRD).magenta());
eprintln!("{crowbook} {version}",
crowbook = style("CROWBOOK").magenta().bold(),
version = style(env!("CARGO_PKG_VERSION")).blue());
}
/// Return the --lang option, if it is set
pub fn get_lang() -> Option<String> {
let mut found = false;
@ -20,9 +38,17 @@ pub fn get_lang() -> Option<String> {
None
}
/// Prints an error
pub fn print_error(s: &str) {
eprint!("{}", style(ERROR).red());
eprintln!("{} {}",
style(lformat!("ERROR")).bold().red(),
s);
}
/// Prints an error on stderr and exit the program
pub fn print_error(s: &str) -> ! {
error!("{}", s);
pub fn print_error_and_exit(s: &str) -> ! {
print_error(s);
exit(0);
}
@ -32,7 +58,7 @@ pub fn get_book_options<'a>(matches: &'a ArgMatches) -> Vec<(&'a str, &'a str)>
if let Some(iter) = matches.values_of("set") {
let v: Vec<_> = iter.collect();
if v.len() % 2 != 0 {
print_error(&lformat!("An odd number of arguments was passed to --set, but it takes \
print_error_and_exit(&lformat!("An odd number of arguments was passed to --set, but it takes \
a list of key value pairs."));
}
@ -60,7 +86,7 @@ pub fn set_book_options(book: &mut Book, matches: &ArgMatches) -> String {
for (key, value) in options {
let res = book.options.set(key, value);
if let Err(err) = res {
print_error(&lformat!("Error in setting key {}: {}", key, err));
print_error_and_exit(&lformat!("Error in setting key {}: {}", key, err));
}
output.push_str(&format!("{}: {}\n", key, value));
}
@ -72,7 +98,7 @@ pub fn set_book_options(book: &mut Book, matches: &ArgMatches) -> String {
pub fn create_book(matches: &ArgMatches) -> ! {
let mut f: Box<Write> = if let Some(book) = matches.value_of("BOOK") {
if fs::metadata(book).is_ok() {
print_error(&lformat!("Could not create file {}: it already exists!", book));
print_error_and_exit(&lformat!("Could not create file {}: it already exists!", book));
}
Box::new(fs::File::create(book).unwrap())
} else {
@ -219,7 +245,7 @@ ARGS:
/// Pre-check the matches to see if there isn't illegal options not detected by clap
fn pre_check(matches: &ArgMatches) {
if matches.is_present("files") && !matches.is_present("create") {
print_error(&lformat!("A list of additional files is only valid with the --create \
print_error_and_exit(&lformat!("A list of additional files is only valid with the --create \
option."));
}
}

View File

@ -1,14 +1,16 @@
extern crate crowbook;
extern crate crowbook_intl_runtime;
extern crate indicatif;
extern crate console;
#[macro_use]
extern crate log;
#[cfg(feature= "binary")]
extern crate simplelog;
#[cfg(feature = "binary")]
extern crate clap;
#[cfg(feature = "binary")]
extern crate tempdir;
#[macro_use]
mod localize_macros;
@ -19,7 +21,6 @@ mod helpers;
#[cfg(feature = "binary")]
#[cfg(feature = "binary")]
#[macro_use]
extern crate lazy_static;

View File

@ -20,11 +20,14 @@ use helpers::*;
use crowbook::{Result, Book, BookOptions};
use crowbook_intl_runtime::set_lang;
use crowbook::Stats;
use tempdir::TempDir;
use clap::ArgMatches;
use std::process::exit;
use std::io;
use std::io::Read;
use std::env;
use simplelog::{Config, TermLogger, LogLevel, LogLevelFilter, SimpleLogger};
use std::fs::File;
use simplelog::{Config, TermLogger, LogLevel, LogLevelFilter, SimpleLogger, WriteLogger};
/// Render a book to specific format
fn render_format(book: &mut Book, matches: &ArgMatches, format: &str) -> ! {
@ -54,7 +57,7 @@ fn render_format(book: &mut Book, matches: &ArgMatches, format: &str) -> ! {
};
match result {
Err(err) => print_error(&format!("{}", err)),
Err(err) => print_error_and_exit(&format!("{}", err)),
Ok(_) => {
exit(0);
}
@ -101,7 +104,7 @@ pub fn try_main() -> Result<()> {
println!("{}", s);
exit(0);
}
Err(_) => print_error(&lformat!("{} is not a valid template name.", template)),
Err(_) => print_error_and_exit(&lformat!("{} is not a valid template name.", template)),
}
}
@ -120,7 +123,7 @@ pub fn try_main() -> Result<()> {
}
if !matches.is_present("BOOK") {
print_error(&lformat!("You must pass the file of a book configuration \
print_error_and_exit(&lformat!("You must pass the file of a book configuration \
file.\n\n{}\n\nFor more information try --help.",
matches.usage()));
}
@ -133,6 +136,7 @@ pub fn try_main() -> Result<()> {
// Initalize logger
let mut log_config = Config::default();
log_config.target = None;
log_config.location = None;
log_config.time = None;
let mut pb = false;
let verbosity = if matches.is_present("verbose") {
@ -143,65 +147,85 @@ pub fn try_main() -> Result<()> {
LogLevelFilter::Error
} else {
pb = true;
LogLevelFilter::Off
LogLevelFilter::Error
};
if TermLogger::init(verbosity, log_config).is_err() {
// If it failed, not much we can do, we just won't display log
let _ = SimpleLogger::init(verbosity, log_config);
let error_dir = TempDir::new("crowbook").unwrap();
let error_path = "error.log";
if true {
let errors = File::create(error_dir.path().join(error_path)).unwrap();
log_config.level = None;
let _ = WriteLogger::init(verbosity, log_config, errors);
} else {
if TermLogger::init(verbosity, log_config).is_err() {
// If it failed, not much we can do, we just won't display log
let _ = SimpleLogger::init(verbosity, log_config);
}
}
let mut book = Book::new();
if pb {
book.add_progress_bar();
}
book.set_options(&get_book_options(&matches));
{
let res = if matches.is_present("single") {
if s != "-" {
book.load_markdown_file(s)
} else {
book.read_markdown_config(io::stdin())
}
} else if s != "-" {
book.load_file(s)
} else {
book.read_config(io::stdin())
}.map(|_| ());
let mut book = Book::new();
if pb {
book.add_progress_bar();
}
book.set_options(&get_book_options(&matches));
match res {
Ok(..) => {},
{
let res = if matches.is_present("single") {
if s != "-" {
book.load_markdown_file(s)
} else {
book.read_markdown_config(io::stdin())
}
} else if s != "-" {
book.load_file(s)
} else {
book.read_config(io::stdin())
}.map(|_| ());
match res {
Ok(..) => {},
Err(err) => {
book.set_error(&format!("{}", err));
return Err(err);
}
}
}
set_book_options(&mut book, &matches);
if matches.is_present("stats") {
let stats = Stats::new(&book);
println!("{}", stats);
exit(0);
}
if let Some(format) = matches.value_of("to") {
render_format(&mut book, &matches, format);
} else {
book.render_all();
}
}
if true {
let mut errors = String::new();
let mut file = File::open(error_dir.path().join(error_path)).unwrap();
file.read_to_string(&mut errors).unwrap();
if !errors.is_empty() {
print_warning();
for line in errors.lines() {
print_error(line);
}
}
}
set_book_options(&mut book, &matches);
if matches.is_present("stats") {
let stats = Stats::new(&book);
println!("{}", stats);
exit(0);
}
if let Some(format) = matches.value_of("to") {
render_format(&mut book, &matches, format);
} else {
book.render_all();
}
Ok(())
}
pub fn real_main() {
display_header();
if let Err(err) = try_main() {
print_error(&format!("{}", err));
print_error_and_exit(&format!("{}", err));
}
}