Add configurable color options

This commit is contained in:
Lucien Greathouse
2020-03-18 12:03:07 -07:00
parent 0b0fe01a7c
commit 9f0a6101b8
3 changed files with 77 additions and 14 deletions

View File

@@ -16,6 +16,7 @@ use std::{
};
use structopt::StructOpt;
use thiserror::Error;
pub use self::build::*;
pub use self::doc::*;
@@ -27,16 +28,73 @@ pub use self::upload::*;
#[derive(Debug, StructOpt)]
#[structopt(name = "Rojo", about, author)]
pub struct Options {
/// Sets verbosity level. Can be specified multiple times.
#[structopt(long = "verbose", short, global(true), parse(from_occurrences))]
pub verbosity: u8,
#[structopt(flatten)]
pub global: GlobalOptions,
/// Subcommand to run in this invocation.
#[structopt(subcommand)]
pub subcommand: Subcommand,
}
/// All of Rojo's subcommands.
#[derive(Debug, StructOpt)]
pub struct GlobalOptions {
/// Sets verbosity level. Can be specified multiple times.
#[structopt(long("verbose"), short, global(true), parse(from_occurrences))]
pub verbosity: u8,
/// Set color behavior. Valid values are auto, always, and never.
#[structopt(long("color"), global(true), default_value("auto"))]
pub color: ColorChoice,
}
#[derive(Debug, Clone, Copy)]
pub enum ColorChoice {
Auto,
Always,
Never,
}
impl FromStr for ColorChoice {
type Err = ColorChoiceParseError;
fn from_str(source: &str) -> Result<Self, Self::Err> {
match source {
"auto" => Ok(ColorChoice::Auto),
"always" => Ok(ColorChoice::Always),
"never" => Ok(ColorChoice::Never),
_ => Err(ColorChoiceParseError {
attempted: source.to_owned(),
}),
}
}
}
impl From<ColorChoice> for termcolor::ColorChoice {
fn from(value: ColorChoice) -> Self {
match value {
ColorChoice::Auto => termcolor::ColorChoice::Auto,
ColorChoice::Always => termcolor::ColorChoice::Always,
ColorChoice::Never => termcolor::ColorChoice::Never,
}
}
}
impl From<ColorChoice> for env_logger::WriteStyle {
fn from(value: ColorChoice) -> Self {
match value {
ColorChoice::Auto => env_logger::WriteStyle::Auto,
ColorChoice::Always => env_logger::WriteStyle::Always,
ColorChoice::Never => env_logger::WriteStyle::Never,
}
}
}
#[derive(Debug, Error)]
#[error("Invalid color choice '{attempted}'. Valid values are: auto, always, never")]
pub struct ColorChoiceParseError {
attempted: String,
}
#[derive(Debug, StructOpt)]
pub enum Subcommand {
/// Creates a new Rojo project.

View File

@@ -7,11 +7,15 @@ use anyhow::Result;
use memofs::Vfs;
use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};
use crate::{cli::ServeCommand, serve_session::ServeSession, web::LiveServer};
use crate::{
cli::{GlobalOptions, ServeCommand},
serve_session::ServeSession,
web::LiveServer,
};
const DEFAULT_PORT: u16 = 34872;
pub fn serve(options: ServeCommand) -> Result<()> {
pub fn serve(global: GlobalOptions, options: ServeCommand) -> Result<()> {
let vfs = Vfs::new_default();
let session = Arc::new(ServeSession::new(vfs, &options.absolute_project()));
@@ -23,14 +27,14 @@ pub fn serve(options: ServeCommand) -> Result<()> {
let server = LiveServer::new(session);
let _ = show_start_message(port);
let _ = show_start_message(port, global.color.into());
server.start(port);
Ok(())
}
fn show_start_message(port: u16) -> io::Result<()> {
let writer = BufferWriter::stdout(ColorChoice::Auto);
fn show_start_message(port: u16, color: ColorChoice) -> io::Result<()> {
let writer = BufferWriter::stdout(color);
let mut buffer = writer.buffer();
writeln!(&mut buffer, "Rojo server listening:")?;