rattler_build_core 0.1.0

Core library for rattler-build
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
//! This module contains utilities for logging and progress bar handling.
use std::{
    borrow::Cow,
    collections::hash_map::DefaultHasher,
    future::Future,
    hash::{Hash, Hasher},
    io,
    str::FromStr,
    sync::{Arc, Mutex},
    time::{Duration, Instant},
};

use clap_verbosity_flag::{InfoLevel, Verbosity};
use console::{Style, style};
use indicatif::{
    HumanBytes, HumanDuration, MultiProgress, ProgressBar, ProgressState, ProgressStyle,
};
use tracing::{Level, field};
use tracing_core::{Event, Field, Subscriber, span::Id};
use tracing_subscriber::{
    EnvFilter, Layer,
    filter::{Directive, ParseError},
    fmt::{
        self, FmtContext, FormatEvent, FormatFields, MakeWriter,
        format::{self, Format},
    },
    layer::{Context, SubscriberExt},
    registry::LookupSpan,
    util::SubscriberInitExt,
};

use crate::consts;

/// A palette of colors used for different package builds.
/// These are chosen to be visually distinct and readable on both light and dark terminals.
const SPAN_COLOR_PALETTE: &[console::Color] = &[
    console::Color::Cyan,
    console::Color::Green,
    console::Color::Yellow,
    console::Color::Blue,
    console::Color::Magenta,
    console::Color::Color256(208), // Orange
    console::Color::Color256(141), // Light purple
    console::Color::Color256(43),  // Teal
];

/// Returns a deterministic color for a given package identifier.
/// The color is chosen by hashing the identifier and selecting from the palette.
fn get_span_color(identifier: &str) -> Style {
    let mut hasher = DefaultHasher::new();
    identifier.hash(&mut hasher);
    let hash = hasher.finish();
    let color_index = (hash as usize) % SPAN_COLOR_PALETTE.len();
    Style::new().fg(SPAN_COLOR_PALETTE[color_index])
}

/// A visitor that extracts the message field without Debug-escaping,
/// preserving ANSI escape codes from subprocess output.
struct PlainMessageVisitor<'a> {
    writer: &'a mut dyn std::fmt::Write,
    result: std::fmt::Result,
}

impl<'a> PlainMessageVisitor<'a> {
    fn new(writer: &'a mut dyn std::fmt::Write) -> Self {
        Self {
            writer,
            result: Ok(()),
        }
    }
}

impl field::Visit for PlainMessageVisitor<'_> {
    fn record_str(&mut self, field: &Field, value: &str) {
        if self.result.is_err() {
            return;
        }
        if field.name() == "message" {
            self.result = write!(self.writer, "{}", value);
        }
    }

    fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
        if self.result.is_err() {
            return;
        }
        if field.name() == "message" {
            self.result = write!(self.writer, "{:?}", value);
        }
    }
}

/// A custom formatter for tracing events.
pub struct TracingFormatter;

impl<S, N> FormatEvent<S, N> for TracingFormatter
where
    S: Subscriber + for<'a> LookupSpan<'a>,
    N: for<'a> FormatFields<'a> + 'static,
{
    fn format_event(
        &self,
        ctx: &FmtContext<'_, S, N>,
        mut writer: format::Writer<'_>,
        event: &Event<'_>,
    ) -> std::fmt::Result {
        let metadata = event.metadata();
        if *metadata.level() == tracing_core::metadata::Level::INFO
            && metadata.target().starts_with("rattler_build")
        {
            // Format the message directly instead of using ctx.format_fields(),
            // which uses DefaultFields and Debug-escapes ANSI escape bytes.
            let mut visitor = PlainMessageVisitor::new(&mut writer);
            event.record(&mut visitor);
            visitor.result?;
            writeln!(writer)
        } else {
            let default_format = Format::default();
            default_format.format_event(ctx, writer, event)
        }
    }
}

struct SpanInfo {
    id: Id,
    start_time: Instant,
    header: String,
    header_printed: bool,
    /// The color style used for this span's tree characters.
    /// This is inherited from parent spans or computed from the package identifier.
    color: Style,
}

#[derive(Default)]
struct SharedState {
    span_stack: Vec<SpanInfo>,
    warnings: Vec<String>,
}

impl std::fmt::Debug for SharedState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SharedState")
            .field("span_stack_len", &self.span_stack.len())
            .field("warnings", &self.warnings)
            .finish()
    }
}

struct CustomVisitor<'a> {
    writer: &'a mut dyn io::Write,
    result: io::Result<()>,
    /// Captures the span_color field for deterministic color computation.
    span_color: Option<String>,
}

impl<'a> CustomVisitor<'a> {
    fn new(writer: &'a mut dyn io::Write) -> Self {
        Self {
            writer,
            result: Ok(()),
            span_color: None,
        }
    }
}

impl field::Visit for CustomVisitor<'_> {
    fn record_str(&mut self, field: &Field, value: &str) {
        if self.result.is_err() {
            return;
        }

        // Capture span_color field for deterministic color computation
        if field.name() == "span_color" {
            self.span_color = Some(value.to_string());
        }

        self.record_debug(field, &format_args!("{}", value))
    }

    fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
        if self.result.is_err() {
            return;
        }

        self.result = match field.name() {
            "message" => write!(self.writer, "{:?}", value),
            "recipe" => write!(self.writer, " recipe: {:?}", value),
            "package" => write!(self.writer, " package: {:?}", value),
            _ => Ok(()),
        };
    }
}

fn chunk_string_without_ansi(input: &str, max_chunk_length: usize) -> Vec<String> {
    let mut chunks: Vec<String> = vec![];
    let mut current_chunk = String::new();
    let mut current_length = 0;
    let mut chars = input.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '\x1B' {
            // Beginning of an ANSI escape sequence
            current_chunk.push(c);
            while let Some(&next_char) = chars.peek() {
                // Add to current chunk
                current_chunk.push(chars.next().unwrap());
                if next_char.is_ascii_alphabetic() {
                    // End of ANSI escape sequence
                    break;
                }
            }
        } else {
            // Regular character
            current_length += 1;
            current_chunk.push(c);
            if current_length == max_chunk_length {
                // Current chunk is full
                chunks.push(current_chunk);
                current_chunk = String::new();
                current_length = 0;
            }
        }
    }

    // Add the last chunk if it's not empty
    if !current_chunk.is_empty() {
        chunks.push(current_chunk);
    }

    chunks
}

/// Creates an indentation string with vertical bars colored according to each span's color.
fn indent_levels_colored(span_stack: &[SpanInfo]) -> String {
    let mut s = String::new();
    for span_info in span_stack {
        s.push_str(&format!(" {}", span_info.color.apply_to("")));
    }
    s
}

impl<S> Layer<S> for LoggingOutputHandler
where
    S: Subscriber + for<'a> LookupSpan<'a>,
{
    fn on_new_span(
        &self,
        attrs: &tracing_core::span::Attributes<'_>,
        id: &Id,
        ctx: Context<'_, S>,
    ) {
        let mut state = self.state.lock().unwrap();

        if let Some(span) = ctx.span(id) {
            let mut s = Vec::new();
            let color_key = {
                let mut w = io::Cursor::new(&mut s);
                let mut visitor = CustomVisitor::new(&mut w);
                attrs.record(&mut visitor);
                visitor.span_color
            };
            let s = String::from_utf8_lossy(&s);

            let name = if s.is_empty() {
                span.name().to_string()
            } else {
                format!("{}{}", span.name(), s)
            };

            // Determine the color for this span:
            // - If there's a span_color field, compute color from it
            // - Otherwise, inherit from parent span
            // - If no parent, use gray (for initial/setup output)
            let span_color = if let Some(ref key) = color_key {
                get_span_color(key)
            } else if let Some(parent) = state.span_stack.last() {
                parent.color.clone()
            } else {
                Style::new().dim()
            };

            let header = if self.simple {
                format!("\n{} {}", span_color.apply_to("==>"), name)
            } else {
                let indent = indent_levels_colored(&state.span_stack);
                format!("{indent}\n{indent} {} {}", span_color.apply_to("╭─"), name)
            };

            state.span_stack.push(SpanInfo {
                id: id.clone(),
                start_time: Instant::now(),
                header,
                header_printed: false,
                color: span_color,
            });
        }
    }

    fn on_exit(&self, id: &Id, _ctx: Context<'_, S>) {
        let mut state = self.state.lock().unwrap();

        if let Some(pos) = state.span_stack.iter().position(|info| &info.id == id) {
            let elapsed = state.span_stack[pos].start_time.elapsed();
            let header_printed = state.span_stack[pos].header_printed;
            let span_color = state.span_stack[pos].color.clone();

            state.span_stack.truncate(pos);

            if !header_printed {
                return;
            }

            if self.simple {
                eprintln!(
                    "{} (took {})",
                    span_color.apply_to("<=="),
                    HumanDuration(elapsed)
                );
            } else {
                // Get the indent before truncating (parent spans only)
                let indent = indent_levels_colored(&state.span_stack);
                // For indent_plus_one, we need to include this span's color too
                let indent_plus_one = format!("{} {}", indent, span_color.apply_to(""));

                eprintln!(
                    "{indent_plus_one}\n{indent} {} (took {})",
                    span_color.apply_to("╰───────────────────"),
                    HumanDuration(elapsed)
                );
            }
        }
    }

    fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
        let mut state = self.state.lock().unwrap();

        // Print pending headers
        for span_info in &mut state.span_stack {
            if !span_info.header_printed {
                eprintln!("{}", span_info.header);
                span_info.header_printed = true;
            }
        }

        let mut s = Vec::new();
        event.record(&mut CustomVisitor::new(&mut s));
        let message = String::from_utf8_lossy(&s);

        if self.simple {
            let prefix = if event.metadata().level() <= &Level::WARN {
                state.warnings.push(message.to_string());
                if event.metadata().level() == &Level::ERROR {
                    format!("[{}] ", style("ERROR").red().bold())
                } else {
                    format!("[{}] ", style("WARN").yellow().bold())
                }
            } else {
                String::new()
            };

            self.progress_bars.suspend(|| {
                for line in message.lines() {
                    eprintln!("{}{}", prefix, line);
                }
            });
        } else {
            let indent = indent_levels_colored(&state.span_stack);

            let (prefix, prefix_len) = if event.metadata().level() <= &Level::WARN {
                state.warnings.push(message.to_string());
                if event.metadata().level() == &Level::ERROR {
                    (style("× error ").red().bold(), 7)
                } else {
                    (style("⚠ warning ").yellow().bold(), 9)
                }
            } else {
                (style(""), 0)
            };

            self.progress_bars.suspend(|| {
                if !self.wrap_lines {
                    for line in message.lines() {
                        eprintln!("{} {}{}", indent, prefix, line);
                    }
                } else {
                    let width = terminal_size::terminal_size()
                        .map(|(w, _)| w.0)
                        .unwrap_or(160) as usize;
                    let max_width = width - (state.span_stack.len() * 2) - 1 - prefix_len;

                    for line in message.lines() {
                        if line.len() <= max_width {
                            eprintln!("{} {}{}", indent, prefix, line);
                        } else {
                            for chunk in chunk_string_without_ansi(line, max_width) {
                                eprintln!("{} {}{}", indent, prefix, chunk);
                            }
                        }
                    }
                }
            });
        }
    }
}

/// A custom output handler for fancy logging.
#[derive(Debug)]
pub struct LoggingOutputHandler {
    state: Arc<Mutex<SharedState>>,
    wrap_lines: bool,
    /// When true, use simple formatting (no box-drawing, simple section headers).
    simple: bool,
    progress_bars: MultiProgress,
    writer: io::Stderr,
}

impl Clone for LoggingOutputHandler {
    fn clone(&self) -> Self {
        Self {
            wrap_lines: self.wrap_lines,
            simple: self.simple,
            state: self.state.clone(),
            progress_bars: self.progress_bars.clone(),
            writer: io::stderr(),
        }
    }
}

impl Default for LoggingOutputHandler {
    /// Creates a new output handler.
    fn default() -> Self {
        Self {
            wrap_lines: true,
            simple: false,
            state: Arc::new(Mutex::new(SharedState::default())),
            progress_bars: MultiProgress::new(),
            writer: io::stderr(),
        }
    }
}

impl LoggingOutputHandler {
    /// Return a string with the current indentation level (bars added to the
    /// front of the string), colored according to each span's color.
    pub fn with_indent_levels(&self, template: &str) -> String {
        let state = self.state.lock().unwrap();
        let indent_str = indent_levels_colored(&state.span_stack);
        format!("{} {}", indent_str, template)
    }

    /// Return the multi-progress instance.
    pub fn multi_progress(&self) -> &MultiProgress {
        &self.progress_bars
    }

    /// Set the multi-progress instance.
    pub fn with_multi_progress(mut self, multi_progress: MultiProgress) -> Self {
        self.progress_bars = multi_progress;
        self
    }

    /// Returns the style to use for a progressbar that is currently in
    /// progress.
    pub fn default_bytes_style(&self) -> indicatif::ProgressStyle {
        let template_str = self.with_indent_levels(
            "{spinner:.green} {prefix:20!} [{elapsed_precise}] [{bar:40!.bright.yellow/dim.white}] {bytes:>8} @ {smoothed_bytes_per_sec:8}"
        );

        indicatif::ProgressStyle::default_bar()
            .template(&template_str)
            .unwrap()
            .progress_chars("━━╾─")
            .with_key(
                "smoothed_bytes_per_sec",
                |s: &ProgressState, w: &mut dyn std::fmt::Write| match (
                    s.pos(),
                    s.elapsed().as_millis(),
                ) {
                    (pos, elapsed_ms) if elapsed_ms > 0 => {
                        // TODO: log with tracing?
                        _ = write!(
                            w,
                            "{}/s",
                            HumanBytes((pos as f64 * 1000_f64 / elapsed_ms as f64) as u64)
                        );
                    }
                    _ => {
                        _ = write!(w, "-");
                    }
                },
            )
    }

    /// Returns the style to use for a progressbar that is currently in
    /// progress.
    pub fn default_progress_style(&self) -> indicatif::ProgressStyle {
        let template_str = self.with_indent_levels(
            "{spinner:.green} {prefix:20!} [{elapsed_precise}] [{bar:40!.bright.yellow/dim.white}] {pos:>7}/{len:7}"
        );
        indicatif::ProgressStyle::default_bar()
            .template(&template_str)
            .unwrap()
            .progress_chars("━━╾─")
    }

    /// Returns the style to use for a progressbar that is in Deserializing
    /// state.
    pub fn deserializing_progress_style(&self) -> indicatif::ProgressStyle {
        let template_str =
            self.with_indent_levels("{spinner:.green} {prefix:20!} [{elapsed_precise}] {wide_msg}");
        indicatif::ProgressStyle::default_bar()
            .template(&template_str)
            .unwrap()
            .progress_chars("━━╾─")
    }

    /// Returns the style to use for a progressbar that is finished.
    pub fn finished_progress_style(&self) -> indicatif::ProgressStyle {
        let template_str = self.with_indent_levels(&format!(
            "{} {{prefix:20!}} [{{elapsed_precise}}] {{msg:.bold.green}}",
            console::style(console::Emoji("", " ")).green()
        ));

        indicatif::ProgressStyle::default_bar()
            .template(&template_str)
            .unwrap()
            .progress_chars("━━╾─")
    }

    /// Returns the style to use for a progressbar that is in error state.
    pub fn errored_progress_style(&self) -> indicatif::ProgressStyle {
        let template_str = self.with_indent_levels(&format!(
            "{} {{prefix:20!}} [{{elapsed_precise}}] {{msg:.bold.red}}",
            console::style(console::Emoji("×", " ")).red()
        ));

        indicatif::ProgressStyle::default_bar()
            .template(&template_str)
            .unwrap()
            .progress_chars("━━╾─")
    }

    /// Returns the style to use for a progressbar that is indeterminate and
    /// simply shows a spinner.
    pub fn long_running_progress_style(&self) -> indicatif::ProgressStyle {
        let template_str = self.with_indent_levels("{spinner:.green} {msg}");
        ProgressStyle::with_template(&template_str).unwrap()
    }

    /// Adds a progress bar to the handler.
    pub fn add_progress_bar(&self, progress_bar: indicatif::ProgressBar) -> indicatif::ProgressBar {
        self.progress_bars.add(progress_bar)
    }

    /// Set progress bars to hidden
    pub fn set_progress_bars_hidden(&self, hidden: bool) {
        self.progress_bars.set_draw_target(if hidden {
            indicatif::ProgressDrawTarget::hidden()
        } else {
            indicatif::ProgressDrawTarget::stderr()
        });
    }

    /// Displays a spinner with the given message while running the specified
    /// function to completion.
    pub fn wrap_in_progress<T, F: FnOnce() -> T>(
        &self,
        msg: impl Into<Cow<'static, str>>,
        func: F,
    ) -> T {
        let pb = self.add_progress_bar(
            ProgressBar::hidden()
                .with_style(self.long_running_progress_style())
                .with_message(msg),
        );
        pb.enable_steady_tick(Duration::from_millis(100));
        let result = func();
        pb.finish_and_clear();
        result
    }

    /// Displays a spinner with the given message while running the specified
    /// function to completion.
    pub async fn wrap_in_progress_async<T, Fut: Future<Output = T>>(
        &self,
        msg: impl Into<Cow<'static, str>>,
        future: Fut,
    ) -> T {
        self.wrap_in_progress_async_with_progress(msg, |_pb| future)
            .await
    }

    /// Displays a spinner with the given message while running the specified
    /// function to completion.
    pub async fn wrap_in_progress_async_with_progress<
        T,
        Fut: Future<Output = T>,
        F: FnOnce(ProgressBar) -> Fut,
    >(
        &self,
        msg: impl Into<Cow<'static, str>>,
        f: F,
    ) -> T {
        let pb = self.add_progress_bar(
            ProgressBar::hidden()
                .with_style(self.long_running_progress_style())
                .with_message(msg),
        );
        pb.enable_steady_tick(Duration::from_millis(100));
        let result = f(pb.clone()).await;
        pb.finish_and_clear();
        result
    }
}

impl io::Write for LoggingOutputHandler {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.progress_bars.suspend(|| self.writer.write(buf))
    }

    fn flush(&mut self) -> io::Result<()> {
        self.progress_bars.suspend(|| self.writer.flush())
    }
}

impl<'a> MakeWriter<'a> for LoggingOutputHandler {
    type Writer = LoggingOutputHandler;

    fn make_writer(&'a self) -> Self::Writer {
        self.clone()
    }
}

///////////////////////
// LOGGING CLI utils //
///////////////////////

/// The style to use for logging output.
#[derive(clap::ValueEnum, Clone, Eq, PartialEq, Debug, Copy)]
pub enum LogStyle {
    /// Use fancy logging output.
    Fancy,
    /// Use JSON logging output.
    Json,
    /// Use plain logging output.
    Plain,
    /// Use simple logging output (colored, no box-drawing frames).
    Simple,
}

/// Constructs a default [`EnvFilter`] that is used when the user did not
/// specify a custom RUST_LOG.
pub fn get_default_env_filter(
    verbose: clap_verbosity_flag::log::LevelFilter,
) -> Result<EnvFilter, ParseError> {
    let mut result = EnvFilter::new(format!("rattler_build={verbose}"));

    if verbose >= clap_verbosity_flag::log::LevelFilter::Trace {
        result = result.add_directive(Directive::from_str("resolvo=info")?);
        result = result.add_directive(Directive::from_str("rattler=info")?);
        result = result.add_directive(Directive::from_str(
            "rattler_networking::authentication_storage=info",
        )?);
    } else {
        result = result.add_directive(Directive::from_str("resolvo=warn")?);
        result = result.add_directive(Directive::from_str("rattler=warn")?);
        result = result.add_directive(Directive::from_str("rattler_repodata_gateway::fetch=off")?);
        result = result.add_directive(Directive::from_str(
            "rattler_networking::authentication_storage=off",
        )?);
    }

    Ok(result)
}

/// Strip ANSI escape codes from a string.
fn strip_ansi_codes(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\x1B' {
            // Skip the escape sequence
            while let Some(&next_char) = chars.peek() {
                chars.next();
                if next_char.is_ascii_alphabetic() {
                    break;
                }
            }
        } else {
            result.push(c);
        }
    }
    result
}

/// A visitor that formats messages without debug-style quoting for GitHub Actions output.
struct GitHubActionsVisitor<'a> {
    writer: &'a mut dyn io::Write,
    result: io::Result<()>,
}

impl<'a> GitHubActionsVisitor<'a> {
    fn new(writer: &'a mut dyn io::Write) -> Self {
        Self {
            writer,
            result: Ok(()),
        }
    }
}

impl field::Visit for GitHubActionsVisitor<'_> {
    fn record_str(&mut self, field: &Field, value: &str) {
        if self.result.is_err() {
            return;
        }
        if field.name() == "message" {
            self.result = write!(self.writer, "{}", value);
        }
    }

    fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
        if self.result.is_err() {
            return;
        }
        if field.name() == "message" {
            // Use Display-style formatting to avoid extra quotes around strings
            self.result = write!(self.writer, "{:?}", value);
        }
    }
}

struct GitHubActionsLayer(bool);

impl<S: Subscriber> Layer<S> for GitHubActionsLayer {
    fn on_event(&self, event: &tracing::Event<'_>, _ctx: Context<'_, S>) {
        if !self.0 {
            return;
        }
        let metadata = event.metadata();

        let mut message = Vec::new();
        event.record(&mut GitHubActionsVisitor::new(&mut message));
        let message = String::from_utf8_lossy(&message);
        let message = strip_ansi_codes(&message);

        match *metadata.level() {
            Level::ERROR => println!("::error ::{}", message),
            Level::WARN => println!("::warning ::{}", message),
            _ => {}
        }
    }
}

/// Whether to use colors in the output.
#[derive(clap::ValueEnum, Clone, Eq, PartialEq, Debug, Copy, Default)]
pub enum Color {
    /// Always use colors.
    Always,
    /// Never use colors.
    Never,
    /// Use colors when the output is a terminal.
    #[default]
    Auto,
}

/// Initializes logging with the given style and verbosity.
///
/// If `tui_writer` is `Some`, it will be used as the log writer for TUI mode.
/// The writer must implement both `io::Write + Clone + Send + Sync` and
/// `MakeWriter` (from `tracing_subscriber`).
pub fn init_logging<W>(
    log_style: &LogStyle,
    verbosity: &Verbosity<InfoLevel>,
    color: &Color,
    wrap_lines: Option<bool>,
    tui_writer: Option<W>,
) -> Result<LoggingOutputHandler, ParseError>
where
    W: for<'a> MakeWriter<'a> + Send + Sync + 'static,
{
    let mut log_handler = LoggingOutputHandler::default();

    // Wrap lines by default, but disable it in CI
    if let Some(wrap_lines) = wrap_lines {
        log_handler.wrap_lines = wrap_lines;
    } else if std::env::var("CI").is_ok() {
        log_handler.wrap_lines = false;
    }

    let use_colors = match color {
        Color::Always => Some(true),
        Color::Never => Some(false),
        // All logging output goes to stderr, so sync stdout color detection
        // to match stderr. This ensures `console::style()` (which checks stdout)
        // produces ANSI only when stderr actually supports it.
        Color::Auto => Some(console::colors_enabled_stderr()),
    };

    // Enable/disable colors for the console crate
    if let Some(use_colors) = use_colors {
        console::set_colors_enabled(use_colors);
        console::set_colors_enabled_stderr(use_colors);
    }

    // Setup tracing subscriber
    let registry =
        tracing_subscriber::registry().with(get_default_env_filter(verbosity.log_level_filter())?);

    let log_style = if verbosity.log_level_filter() >= clap_verbosity_flag::log::LevelFilter::Debug
    {
        LogStyle::Plain
    } else {
        *log_style
    };

    let registry = registry.with(GitHubActionsLayer(github_integration_enabled()));

    if let Some(tui_writer) = tui_writer {
        log_handler.set_progress_bars_hidden(true);
        registry
            .with(
                fmt::layer()
                    .with_writer(tui_writer)
                    .without_time()
                    .with_level(false)
                    .with_target(false),
            )
            .init();
        return Ok(log_handler);
    }

    match log_style {
        LogStyle::Fancy => {
            registry.with(log_handler.clone()).init();
        }
        LogStyle::Plain => {
            // Plain mode should not produce any ANSI escape codes
            console::set_colors_enabled(false);
            console::set_colors_enabled_stderr(false);
            registry
                .with(
                    fmt::layer()
                        .with_writer(log_handler.clone())
                        .event_format(TracingFormatter),
                )
                .init();
        }
        LogStyle::Json => {
            log_handler.set_progress_bars_hidden(true);
            registry
                .with(fmt::layer().json().with_writer(io::stderr))
                .init();
        }
        LogStyle::Simple => {
            log_handler.simple = true;
            registry.with(log_handler.clone()).init();
        }
    }

    Ok(log_handler)
}

/// Checks whether we are on GitHub Actions and if the user has enabled the GitHub integration
pub fn github_integration_enabled() -> bool {
    github_action_runner()
        && std::env::var(consts::RATTLER_BUILD_ENABLE_GITHUB_INTEGRATION) == Ok("true".to_string())
}

/// Checks whether we are on GitHub Actions
pub fn github_action_runner() -> bool {
    std::env::var(consts::GITHUB_ACTIONS) == Ok("true".to_string())
}