MCPcopy Create free account
hub / github.com/csskit/csskit / run

Method run

crates/csskit/src/commands/fmt.rs:35–83  ·  view source on GitHub ↗
(&self, config: GlobalConfig)

Source from the content-addressed store, hash-verified

33
34impl Fmt {
35 pub fn run(&self, config: GlobalConfig) -> CliResult {
36 let Fmt { content, output, check, expand_tab, single_quotes } = self;
37 let color = config.colors() && output.is_none() && !*check;
38 let bump = Bump::default();
39 let start = std::time::Instant::now();
40 let quotes = if *single_quotes { QuoteStyle::Single } else { QuoteStyle::Double };
41 if *check && output.is_some() {
42 eprintln!("Ignoring output option, because check was passed");
43 }
44 let mut checks = 0;
45 for (file_name, mut source) in content.sources()? {
46 let mut source_string = String::new();
47 source.read_to_string(&mut source_string)?;
48 let source_text = source_string.as_str();
49 let lexer = Lexer::new(&CssAtomSet::ATOMS, source_text);
50 let mut parser = Parser::new(&bump, source_text, lexer);
51 let result = parser.parse_entirely::<StyleSheet>();
52 if let Some(stylesheet) = result.output.as_ref() {
53 let mut str = String::new();
54 if color {
55 let mut highlighter = TokenHighlighter::new();
56 let _ = stylesheet.accept(&mut highlighter);
57 let ansi = AnsiHighlightCursorStream::new(&mut str, &highlighter, DefaultAnsiTheme);
58 let mut stream = CursorPrettyWriteSink::new(source_text, ansi, *expand_tab, quotes);
59 result.to_cursors(&mut stream);
60 } else {
61 let mut stream = CursorPrettyWriteSink::new(source_text, &mut str, *expand_tab, quotes);
62 result.to_cursors(&mut stream);
63 }
64 if *check {
65 if str != source_text {
66 println!("{str}");
67 checks += 1;
68 }
69 } else if let Some(file) = output {
70 std::fs::write(file, str.as_bytes())?;
71 } else {
72 println!("{str}");
73 }
74 } else {
75 for compact_err in result.errors {
76 let report = crate::commands::format_diagnostic_error(&compact_err, &source_string, file_name);
77 println!("{report}");
78 }
79 }
80 }
81 eprintln!("Slurped up CSS in {:?}! Neat!", start.elapsed());
82 if checks > 0 { Err(CliError::Checks(checks))? } else { Ok(()) }
83 }
84}

Callers 2

runFunction · 0.45
mainFunction · 0.45

Calls 8

format_diagnostic_errorFunction · 0.85
ErrClass · 0.85
colorsMethod · 0.80
sourcesMethod · 0.80
as_refMethod · 0.80
acceptMethod · 0.80
as_strMethod · 0.45
to_cursorsMethod · 0.45

Tested by

no test coverage detected