use std::path::PathBuf;
use structopt::{clap::arg_enum, StructOpt};
use stylua_lib::{CallParenType, IndentType, LineEndings, QuoteStyle};
lazy_static::lazy_static! {
static ref NUM_CPUS: String = num_cpus::get().to_string();
}
#[derive(StructOpt, Debug)]
#[structopt(name = "stylua", about = "A utility to format Lua code")]
pub struct Opt {
#[structopt(long = "config-path", short = "f", parse(from_os_str))]
pub config_path: Option<PathBuf>,
#[structopt(long = "stdin-filepath", parse(from_os_str))]
pub stdin_filepath: Option<PathBuf>,
#[structopt(short, long)]
pub search_parent_directories: bool,
#[structopt(short, long)]
pub check: bool,
#[structopt(long)]
pub verify: bool,
#[structopt(short, long)]
pub verbose: bool,
#[structopt(long, possible_values = &Color::variants(), case_insensitive = true, default_value = "auto")]
pub color: Color,
#[structopt(short, long)]
pub glob: Option<Vec<String>>,
#[structopt(long, default_value = &NUM_CPUS)]
pub num_threads: usize,
#[structopt(long)]
pub range_start: Option<usize>,
#[structopt(long)]
pub range_end: Option<usize>,
#[structopt(flatten)]
pub format_opts: FormatOpts,
#[structopt(parse(from_os_str))]
pub files: Vec<PathBuf>,
}
structopt::clap::arg_enum! {
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Color {
Always,
Auto,
Never,
}
}
impl Color {
pub fn should_use_color(&self) -> bool {
match self {
Color::Always => true,
Color::Never => false,
Color::Auto => {
let terminal = console::Term::stdout();
let features = terminal.features();
features.is_attended() && features.colors_supported()
}
}
}
}
#[derive(StructOpt, Debug)]
pub struct FormatOpts {
#[structopt(long)]
pub column_width: Option<usize>,
#[structopt(long, possible_values = &ArgLineEndings::variants(), case_insensitive = true, )]
pub line_endings: Option<ArgLineEndings>,
#[structopt(long, possible_values = &ArgIndentType::variants(), case_insensitive = true, )]
pub indent_type: Option<ArgIndentType>,
#[structopt(long)]
pub indent_width: Option<usize>,
#[structopt(long, possible_values = &ArgQuoteStyle::variants(), case_insensitive = true, )]
pub quote_style: Option<ArgQuoteStyle>,
#[structopt(long, possible_values = &ArgCallParenType::variants(), case_insensitive = true, )]
pub call_parentheses: Option<ArgCallParenType>,
}
macro_rules! convert_enum {
($from:tt, $arg:tt, { $($enum_name:ident,)+ }) => {
structopt::clap::arg_enum! {
#[derive(Clone, Copy, Debug)]
pub enum $arg {
$(
$enum_name,
)+
}
}
impl From<$arg> for $from {
fn from(other: $arg) -> $from {
match other {
$(
$arg::$enum_name => $from::$enum_name,
)+
}
}
}
impl From<$from> for $arg {
fn from(other: $from) -> $arg {
match other {
$(
$from::$enum_name => $arg::$enum_name,
)+
}
}
}
};
}
convert_enum!(LineEndings, ArgLineEndings, {
Unix,
Windows,
});
convert_enum!(IndentType, ArgIndentType, {
Tabs,
Spaces,
});
convert_enum!(QuoteStyle, ArgQuoteStyle, {
AutoPreferDouble,
AutoPreferSingle,
ForceDouble,
ForceSingle,
});
convert_enum!(CallParenType, ArgCallParenType, {
Always,
NoSingleString,
NoSingleTable,
None,
});