Add some colours

This commit is contained in:
Vincent Prouillet 2017-03-25 15:52:51 +09:00
parent 90e3d3817f
commit a02d50c22a
7 changed files with 77 additions and 50 deletions

20
Cargo.lock generated
View file

@ -19,6 +19,7 @@ dependencies = [
"syntect 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntect 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"tera 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "tera 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"term-painter 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"ws 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "ws 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -820,6 +821,23 @@ dependencies = [
"url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "term"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "term-painter"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "term_size" name = "term_size"
version = "0.2.3" version = "0.2.3"
@ -1131,6 +1149,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum syntect 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6728e7e9bbd971751d17d39b0e38e3558c10b9fb32125441bb17c434a2754e7c" "checksum syntect 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6728e7e9bbd971751d17d39b0e38e3558c10b9fb32125441bb17c434a2754e7c"
"checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6" "checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6"
"checksum tera 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6f2ff83a1773a0482ddc961d0030b514f1848f592ae9612afb241e5eb455df75" "checksum tera 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6f2ff83a1773a0482ddc961d0030b514f1848f592ae9612afb241e5eb455df75"
"checksum term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d168af3930b369cfe245132550579d47dfd873d69470755a19c2c6568dbbd989"
"checksum term-painter 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ab900bf2f05175932b13d4fc12f8ff09ef777715b04998791ab2c930841e496b"
"checksum term_size 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "07b6c1ac5b3fffd75073276bca1ceed01f67a28537097a2a9539e116e50fb21a" "checksum term_size 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "07b6c1ac5b3fffd75073276bca1ceed01f67a28537097a2a9539e116e50fb21a"
"checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a" "checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a"
"checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7" "checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7"

View file

@ -29,6 +29,7 @@ slug = "0.1"
syntect = "1" syntect = "1"
chrono = "0.3" chrono = "0.3"
toml = { version = "0.3", default-features = false, features = ["serde"]} toml = { version = "0.3", default-features = false, features = ["serde"]}
term-painter = "0.2"
# Below is for the serve cmd # Below is for the serve cmd
staticfile = "0.4" staticfile = "0.4"

View file

@ -7,5 +7,6 @@ use gutenberg::Site;
pub fn build(config_file: &str) -> Result<()> { pub fn build(config_file: &str) -> Result<()> {
let mut site = Site::new(env::current_dir().unwrap(), config_file)?; let mut site = Site::new(env::current_dir().unwrap(), config_file)?;
site.load()?; site.load()?;
println!("-> Creating {} pages and {} sections", site.pages.len(), site.sections.len());
site.build() site.build()
} }

View file

@ -14,7 +14,8 @@ use gutenberg::Site;
use gutenberg::errors::{Result}; use gutenberg::errors::{Result};
use ::report_elapsed_time; use ::{report_elapsed_time, unravel_errors};
use console;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -46,20 +47,13 @@ fn rebuild_done_handling(broadcaster: &Sender, res: Result<()>, reload_path: &st
}}"#, reload_path) }}"#, reload_path)
).unwrap(); ).unwrap();
}, },
Err(e) => { Err(e) => unravel_errors("Failed to build the site", e, false)
println!("Failed to build the site");
println!("Error: {}", e);
for e in e.iter().skip(1) {
println!("Reason: {}", e)
}
}
} }
} }
// Most of it taken from mdbook // Most of it taken from mdbook
pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> { pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
println!("Building site...");
let start = Instant::now(); let start = Instant::now();
let mut site = Site::new(env::current_dir().unwrap(), config_file)?; let mut site = Site::new(env::current_dir().unwrap(), config_file)?;
let address = format!("{}:{}", interface, port); let address = format!("{}:{}", interface, port);
@ -84,7 +78,6 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
// Starts with a _ to not trigger the unused lint // Starts with a _ to not trigger the unused lint
// we need to assign to a variable otherwise it will block // we need to assign to a variable otherwise it will block
let _iron = Iron::new(mount).http(address.as_str()).unwrap(); let _iron = Iron::new(mount).http(address.as_str()).unwrap();
println!("Web server is available at http://{}", address);
// The websocket for livereload // The websocket for livereload
let ws_server = WebSocket::new(|_| { let ws_server = WebSocket::new(|_| {
@ -104,8 +97,10 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
watcher.watch("static/", RecursiveMode::Recursive).unwrap(); watcher.watch("static/", RecursiveMode::Recursive).unwrap();
watcher.watch("templates/", RecursiveMode::Recursive).unwrap(); watcher.watch("templates/", RecursiveMode::Recursive).unwrap();
let pwd = format!("{}", env::current_dir().unwrap().display()); let pwd = format!("{}", env::current_dir().unwrap().display());
println!("Listening for changes in {}/{{content, static, templates}}", pwd); println!("Listening for changes in {}/{{content, static, templates}}", pwd);
println!("Press CTRL+C to stop\n"); println!("Web server is available at http://{}", address);
println!("Press Ctrl+C to stop\n");
use notify::DebouncedEvent::*; use notify::DebouncedEvent::*;
@ -127,17 +122,17 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
let start = Instant::now(); let start = Instant::now();
match detect_change_kind(&pwd, &path) { match detect_change_kind(&pwd, &path) {
(ChangeKind::Content, _) => { (ChangeKind::Content, _) => {
println!("-> Content changed {}", path.display()); console::info(&format!("-> Content changed {}", path.display()));
// Force refresh // Force refresh
rebuild_done_handling(&broadcaster, site.rebuild_after_content_change(&path), "/x.js"); rebuild_done_handling(&broadcaster, site.rebuild_after_content_change(&path), "/x.js");
}, },
(ChangeKind::Templates, _) => { (ChangeKind::Templates, _) => {
println!("-> Template changed {}", path.display()); console::info(&format!("-> Template changed {}", path.display()));
// Force refresh // Force refresh
rebuild_done_handling(&broadcaster, site.rebuild_after_template_change(), "/x.js"); rebuild_done_handling(&broadcaster, site.rebuild_after_template_change(), "/x.js");
}, },
(ChangeKind::StaticFiles, p) => { (ChangeKind::StaticFiles, p) => {
println!("-> Static file changes detected {}", path.display()); console::info(&format!("-> Static file changes detected {}", path.display()));
rebuild_done_handling(&broadcaster, site.copy_static_directory(), &p); rebuild_done_handling(&broadcaster, site.copy_static_directory(), &p);
}, },
}; };
@ -146,14 +141,14 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
_ => {} _ => {}
} }
}, },
Err(e) => println!("Watch error: {:?}", e), Err(e) => console::error(&format!("Watch error: {:?}", e)),
}; };
} }
} }
/// Returns whether the path we received corresponds to a temp file create /// Returns whether the path we received corresponds to a temp file created
/// by an editor /// by an editor or the OS
fn is_temp_file(path: &Path) -> bool { fn is_temp_file(path: &Path) -> bool {
let ext = path.extension(); let ext = path.extension();
match ext { match ext {
@ -194,7 +189,7 @@ fn detect_change_kind(pwd: &str, path: &Path) -> (ChangeKind, String) {
} else if path_str.starts_with("/static") { } else if path_str.starts_with("/static") {
ChangeKind::StaticFiles ChangeKind::StaticFiles
} else { } else {
panic!("Got a change in an unexpected path: {}", path_str); unreachable!("Got a change in an unexpected path: {}", path_str);
}; };
(change_kind, path_str) (change_kind, path_str)
@ -221,7 +216,6 @@ mod tests {
]; ];
for t in testcases { for t in testcases {
println!("{:?}", t.display());
assert!(is_temp_file(&t)); assert!(is_temp_file(&t));
} }
} }
@ -244,7 +238,6 @@ mod tests {
]; ];
for (expected, pwd, path) in testcases { for (expected, pwd, path) in testcases {
println!("{:?}", path.display());
assert_eq!(expected, detect_change_kind(&pwd, &path)); assert_eq!(expected, detect_change_kind(&pwd, &path));
} }
} }

15
src/console.rs Normal file
View file

@ -0,0 +1,15 @@
use term_painter::ToStyle;
use term_painter::Color::*;
pub fn info(message: &str) {
println!("{}", NotSet.bold().paint(message));
}
pub fn success(message: &str) {
println!("{}", Green.bold().paint(message));
}
pub fn error(message: &str) {
println!("{}", Red.bold().paint(message));
}

View file

@ -4,6 +4,7 @@ extern crate clap;
extern crate error_chain; extern crate error_chain;
extern crate gutenberg; extern crate gutenberg;
extern crate chrono; extern crate chrono;
extern crate term_painter;
extern crate staticfile; extern crate staticfile;
extern crate iron; extern crate iron;
@ -13,20 +14,35 @@ extern crate ws;
use std::time::Instant; use std::time::Instant;
use chrono::Duration; use chrono::Duration;
use gutenberg::errors::Error;
mod cmd; mod cmd;
mod console;
// Print the time elapsed rounded to 1 decimal /// Print the time elapsed rounded to 1 decimal
fn report_elapsed_time(instant: Instant) { fn report_elapsed_time(instant: Instant) {
let duration_ms = Duration::from_std(instant.elapsed()).unwrap().num_milliseconds() as f64; let duration_ms = Duration::from_std(instant.elapsed()).unwrap().num_milliseconds() as f64;
if duration_ms < 1000.0 { if duration_ms < 1000.0 {
println!("Done in {}ms.\n", duration_ms); console::success(&format!("Done in {}ms.\n", duration_ms));
} else { } else {
let duration_sec = duration_ms / 1000.0; let duration_sec = duration_ms / 1000.0;
println!("Done in {:.1}s.\n", ((duration_sec * 10.0).round() / 10.0)); console::success(&format!("Done in {:.1}s.\n", ((duration_sec * 10.0).round() / 10.0)));
}
}
////Display an error message, the actual error and then exits if requested
fn unravel_errors(message: &str, error: Error, exit: bool) {
console::error(message);
console::error(&format!("Error: {}", error));
for e in error.iter().skip(1) {
console::error(&format!("Reason: {}", e));
}
if exit {
::std::process::exit(1);
} }
} }
@ -57,44 +73,25 @@ fn main() {
match matches.subcommand() { match matches.subcommand() {
("init", Some(matches)) => { ("init", Some(matches)) => {
match cmd::create_new_project(matches.value_of("name").unwrap()) { match cmd::create_new_project(matches.value_of("name").unwrap()) {
Ok(()) => { Ok(()) => console::success("Project created"),
println!("Project created"); Err(e) => unravel_errors("Failed to create the project", e, true),
},
Err(e) => {
println!("Error: {}", e);
::std::process::exit(1);
},
}; };
}, },
("build", Some(_)) => { ("build", Some(_)) => {
println!("Building site"); console::info("Building site...");
let start = Instant::now(); let start = Instant::now();
match cmd::build(&config_file) { match cmd::build(&config_file) {
Ok(()) => { Ok(()) => report_elapsed_time(start),
report_elapsed_time(start); Err(e) => unravel_errors("Failed to build the site", e, true),
},
Err(e) => {
println!("Failed to build the site");
println!("Error: {}", e);
for e in e.iter().skip(1) {
println!("Reason: {}", e)
}
::std::process::exit(1);
},
}; };
}, },
("serve", Some(matches)) => { ("serve", Some(matches)) => {
let interface = matches.value_of("interface").unwrap_or("127.0.0.1"); let interface = matches.value_of("interface").unwrap_or("127.0.0.1");
let port = matches.value_of("port").unwrap_or("1111"); let port = matches.value_of("port").unwrap_or("1111");
console::info("Building site...");
match cmd::serve(interface, port, &config_file) { match cmd::serve(interface, port, &config_file) {
Ok(()) => (), Ok(()) => (),
Err(e) => { Err(e) => unravel_errors("Failed to build the site", e, true),
println!("Error: {}", e);
for e in e.iter().skip(1) {
println!("Reason: {}", e)
}
::std::process::exit(1);
},
}; };
}, },
_ => unreachable!(), _ => unreachable!(),

View file

@ -206,7 +206,7 @@ impl Page {
context.add("page", self); context.add("page", self);
tera.render(&tpl_name, &context) tera.render(&tpl_name, &context)
.chain_err(|| format!("Failed to render page '{}'", self.file_name)) .chain_err(|| format!("Failed to render page '{}'", self.file_path.display()))
} }
} }