console.rs

  1use super::{
  2    stack_frame_list::{StackFrameList, StackFrameListEvent},
  3    variable_list::VariableList,
  4};
  5use alacritty_terminal::vte::ansi;
  6use anyhow::Result;
  7use collections::HashMap;
  8use dap::OutputEvent;
  9use editor::{Bias, CompletionProvider, Editor, EditorElement, EditorStyle, ExcerptId};
 10use fuzzy::StringMatchCandidate;
 11use gpui::{
 12    Action as _, AppContext, Context, Corner, Entity, FocusHandle, Focusable, HighlightStyle, Hsla,
 13    Render, Subscription, Task, TextStyle, WeakEntity, actions,
 14};
 15use language::{Buffer, CodeLabel, ToOffset};
 16use menu::{Confirm, SelectNext, SelectPrevious};
 17use project::{
 18    Completion, CompletionResponse,
 19    debugger::session::{CompletionsQuery, OutputToken, Session},
 20    search_history::{SearchHistory, SearchHistoryCursor},
 21};
 22use settings::Settings;
 23use std::fmt::Write;
 24use std::{cell::RefCell, ops::Range, rc::Rc, usize};
 25use theme::{Theme, ThemeSettings};
 26use ui::{ContextMenu, Divider, PopoverMenu, SplitButton, Tooltip, prelude::*};
 27use util::ResultExt;
 28
 29actions!(
 30    console,
 31    [
 32        /// Adds an expression to the watch list.
 33        WatchExpression
 34    ]
 35);
 36
 37pub struct Console {
 38    console: Entity<Editor>,
 39    query_bar: Entity<Editor>,
 40    session: Entity<Session>,
 41    _subscriptions: Vec<Subscription>,
 42    variable_list: Entity<VariableList>,
 43    stack_frame_list: Entity<StackFrameList>,
 44    last_token: OutputToken,
 45    update_output_task: Option<Task<()>>,
 46    focus_handle: FocusHandle,
 47    history: SearchHistory,
 48    cursor: SearchHistoryCursor,
 49}
 50
 51impl Console {
 52    pub fn new(
 53        session: Entity<Session>,
 54        stack_frame_list: Entity<StackFrameList>,
 55        variable_list: Entity<VariableList>,
 56        window: &mut Window,
 57        cx: &mut Context<Self>,
 58    ) -> Self {
 59        let console = cx.new(|cx| {
 60            let mut editor = Editor::multi_line(window, cx);
 61            editor.move_to_end(&editor::actions::MoveToEnd, window, cx);
 62            editor.set_read_only(true);
 63            editor.disable_scrollbars_and_minimap(window, cx);
 64            editor.set_show_gutter(false, cx);
 65            editor.set_show_runnables(false, cx);
 66            editor.set_show_breakpoints(false, cx);
 67            editor.set_show_code_actions(false, cx);
 68            editor.set_show_line_numbers(false, cx);
 69            editor.set_show_git_diff_gutter(false, cx);
 70            editor.set_autoindent(false);
 71            editor.set_input_enabled(false);
 72            editor.set_use_autoclose(false);
 73            editor.set_show_wrap_guides(false, cx);
 74            editor.set_show_indent_guides(false, cx);
 75            editor.set_show_edit_predictions(Some(false), window, cx);
 76            editor.set_use_modal_editing(false);
 77            editor.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
 78            editor
 79        });
 80        let focus_handle = cx.focus_handle();
 81
 82        let this = cx.weak_entity();
 83        let query_bar = cx.new(|cx| {
 84            let mut editor = Editor::single_line(window, cx);
 85            editor.set_placeholder_text("Evaluate an expression", cx);
 86            editor.set_use_autoclose(false);
 87            editor.set_show_gutter(false, cx);
 88            editor.set_show_wrap_guides(false, cx);
 89            editor.set_show_indent_guides(false, cx);
 90            editor.set_completion_provider(Some(Rc::new(ConsoleQueryBarCompletionProvider(this))));
 91
 92            editor
 93        });
 94
 95        let _subscriptions = vec![
 96            cx.subscribe(&stack_frame_list, Self::handle_stack_frame_list_events),
 97            cx.on_focus(&focus_handle, window, |console, window, cx| {
 98                if console.is_running(cx) {
 99                    console.query_bar.focus_handle(cx).focus(window);
100                }
101            }),
102        ];
103
104        Self {
105            session,
106            console,
107            query_bar,
108            variable_list,
109            _subscriptions,
110            stack_frame_list,
111            update_output_task: None,
112            last_token: OutputToken(0),
113            focus_handle,
114            history: SearchHistory::new(
115                None,
116                project::search_history::QueryInsertionBehavior::ReplacePreviousIfContains,
117            ),
118            cursor: Default::default(),
119        }
120    }
121
122    #[cfg(test)]
123    pub(crate) fn editor(&self) -> &Entity<Editor> {
124        &self.console
125    }
126
127    fn is_running(&self, cx: &Context<Self>) -> bool {
128        self.session.read(cx).is_started()
129    }
130
131    fn handle_stack_frame_list_events(
132        &mut self,
133        _: Entity<StackFrameList>,
134        event: &StackFrameListEvent,
135        cx: &mut Context<Self>,
136    ) {
137        match event {
138            StackFrameListEvent::SelectedStackFrameChanged(_) => cx.notify(),
139            StackFrameListEvent::BuiltEntries => {}
140        }
141    }
142
143    pub(crate) fn show_indicator(&self, cx: &App) -> bool {
144        self.session.read(cx).has_new_output(self.last_token)
145    }
146
147    fn add_messages(
148        &mut self,
149        events: Vec<OutputEvent>,
150        window: &mut Window,
151        cx: &mut App,
152    ) -> Task<Result<()>> {
153        self.console.update(cx, |_, cx| {
154            cx.spawn_in(window, async move |console, cx| {
155                let mut len = console.update(cx, |this, cx| this.buffer().read(cx).len(cx))?;
156                let (output, spans, background_spans) = cx
157                    .background_spawn(async move {
158                        let mut all_spans = Vec::new();
159                        let mut all_background_spans = Vec::new();
160                        let mut to_insert = String::new();
161                        let mut scratch = String::new();
162
163                        for event in &events {
164                            scratch.clear();
165                            let mut ansi_handler = ConsoleHandler::default();
166                            let mut ansi_processor =
167                                ansi::Processor::<ansi::StdSyncHandler>::default();
168
169                            let trimmed_output = event.output.trim_end();
170                            let _ = writeln!(&mut scratch, "{trimmed_output}");
171                            ansi_processor.advance(&mut ansi_handler, scratch.as_bytes());
172                            let output = std::mem::take(&mut ansi_handler.output);
173                            to_insert.extend(output.chars());
174                            let mut spans = std::mem::take(&mut ansi_handler.spans);
175                            let mut background_spans =
176                                std::mem::take(&mut ansi_handler.background_spans);
177                            if ansi_handler.current_range_start < output.len() {
178                                spans.push((
179                                    ansi_handler.current_range_start..output.len(),
180                                    ansi_handler.current_color,
181                                ));
182                            }
183                            if ansi_handler.current_background_range_start < output.len() {
184                                background_spans.push((
185                                    ansi_handler.current_background_range_start..output.len(),
186                                    ansi_handler.current_background_color,
187                                ));
188                            }
189
190                            for (range, _) in spans.iter_mut() {
191                                let start_offset = len + range.start;
192                                *range = start_offset..len + range.end;
193                            }
194
195                            for (range, _) in background_spans.iter_mut() {
196                                let start_offset = len + range.start;
197                                *range = start_offset..len + range.end;
198                            }
199
200                            len += output.len();
201
202                            all_spans.extend(spans);
203                            all_background_spans.extend(background_spans);
204                        }
205                        (to_insert, all_spans, all_background_spans)
206                    })
207                    .await;
208                console.update_in(cx, |console, window, cx| {
209                    console.set_read_only(false);
210                    console.move_to_end(&editor::actions::MoveToEnd, window, cx);
211                    console.insert(&output, window, cx);
212                    console.set_read_only(true);
213
214                    struct ConsoleAnsiHighlight;
215
216                    let buffer = console.buffer().read(cx).snapshot(cx);
217
218                    for (range, color) in spans {
219                        let Some(color) = color else { continue };
220                        let start_offset = range.start;
221                        let range =
222                            buffer.anchor_after(range.start)..buffer.anchor_before(range.end);
223                        let style = HighlightStyle {
224                            color: Some(terminal_view::terminal_element::convert_color(
225                                &color,
226                                cx.theme(),
227                            )),
228                            ..Default::default()
229                        };
230                        console.highlight_text_key::<ConsoleAnsiHighlight>(
231                            start_offset,
232                            vec![range],
233                            style,
234                            cx,
235                        );
236                    }
237
238                    for (range, color) in background_spans {
239                        let Some(color) = color else { continue };
240                        let start_offset = range.start;
241                        let range =
242                            buffer.anchor_after(range.start)..buffer.anchor_before(range.end);
243                        console.highlight_background_key::<ConsoleAnsiHighlight>(
244                            start_offset,
245                            &[range],
246                            color_fetcher(color),
247                            cx,
248                        );
249                    }
250
251                    cx.notify();
252                })?;
253
254                Ok(())
255            })
256        })
257    }
258
259    pub fn watch_expression(
260        &mut self,
261        _: &WatchExpression,
262        window: &mut Window,
263        cx: &mut Context<Self>,
264    ) {
265        let expression = self.query_bar.update(cx, |editor, cx| {
266            let expression = editor.text(cx);
267            cx.defer_in(window, |editor, window, cx| {
268                editor.clear(window, cx);
269            });
270
271            expression
272        });
273        self.history.add(&mut self.cursor, expression.clone());
274        self.cursor.reset();
275        self.session.update(cx, |session, cx| {
276            session
277                .evaluate(
278                    expression.clone(),
279                    Some(dap::EvaluateArgumentsContext::Repl),
280                    self.stack_frame_list.read(cx).opened_stack_frame_id(),
281                    None,
282                    cx,
283                )
284                .detach();
285
286            if let Some(stack_frame_id) = self.stack_frame_list.read(cx).opened_stack_frame_id() {
287                session
288                    .add_watcher(expression.into(), stack_frame_id, cx)
289                    .detach();
290            }
291        });
292    }
293
294    fn previous_query(&mut self, _: &SelectPrevious, window: &mut Window, cx: &mut Context<Self>) {
295        let prev = self.history.previous(&mut self.cursor);
296        if let Some(prev) = prev {
297            self.query_bar.update(cx, |editor, cx| {
298                editor.set_text(prev, window, cx);
299            });
300        }
301    }
302
303    fn next_query(&mut self, _: &SelectNext, window: &mut Window, cx: &mut Context<Self>) {
304        let next = self.history.next(&mut self.cursor);
305        let query = next.unwrap_or_else(|| {
306            self.cursor.reset();
307            ""
308        });
309
310        self.query_bar.update(cx, |editor, cx| {
311            editor.set_text(query, window, cx);
312        });
313    }
314
315    fn evaluate(&mut self, _: &Confirm, window: &mut Window, cx: &mut Context<Self>) {
316        let expression = self.query_bar.update(cx, |editor, cx| {
317            let expression = editor.text(cx);
318            cx.defer_in(window, |editor, window, cx| {
319                editor.clear(window, cx);
320            });
321
322            expression
323        });
324
325        self.history.add(&mut self.cursor, expression.clone());
326        self.cursor.reset();
327        self.session.update(cx, |session, cx| {
328            session
329                .evaluate(
330                    expression,
331                    Some(dap::EvaluateArgumentsContext::Repl),
332                    self.stack_frame_list.read(cx).opened_stack_frame_id(),
333                    None,
334                    cx,
335                )
336                .detach();
337        });
338    }
339
340    fn render_submit_menu(
341        &self,
342        id: impl Into<ElementId>,
343        keybinding_target: Option<FocusHandle>,
344        cx: &App,
345    ) -> impl IntoElement {
346        PopoverMenu::new(id.into())
347            .trigger(
348                ui::ButtonLike::new_rounded_right("console-confirm-split-button-right")
349                    .layer(ui::ElevationIndex::ModalSurface)
350                    .size(ui::ButtonSize::None)
351                    .child(
352                        div()
353                            .px_1()
354                            .child(Icon::new(IconName::ChevronDownSmall).size(IconSize::XSmall)),
355                    ),
356            )
357            .when(
358                self.stack_frame_list
359                    .read(cx)
360                    .opened_stack_frame_id()
361                    .is_some(),
362                |this| {
363                    this.menu(move |window, cx| {
364                        Some(ContextMenu::build(window, cx, |context_menu, _, _| {
365                            context_menu
366                                .when_some(keybinding_target.clone(), |el, keybinding_target| {
367                                    el.context(keybinding_target.clone())
368                                })
369                                .action("Watch expression", WatchExpression.boxed_clone())
370                        }))
371                    })
372                },
373            )
374            .anchor(Corner::TopRight)
375    }
376
377    fn render_console(&self, cx: &Context<Self>) -> impl IntoElement {
378        EditorElement::new(&self.console, Self::editor_style(&self.console, cx))
379    }
380
381    fn editor_style(editor: &Entity<Editor>, cx: &Context<Self>) -> EditorStyle {
382        let is_read_only = editor.read(cx).read_only(cx);
383        let settings = ThemeSettings::get_global(cx);
384        let theme = cx.theme();
385        let text_style = TextStyle {
386            color: if is_read_only {
387                theme.colors().text_muted
388            } else {
389                theme.colors().text
390            },
391            font_family: settings.buffer_font.family.clone(),
392            font_features: settings.buffer_font.features.clone(),
393            font_size: settings.buffer_font_size(cx).into(),
394            font_weight: settings.buffer_font.weight,
395            line_height: relative(settings.buffer_line_height.value()),
396            ..Default::default()
397        };
398        EditorStyle {
399            background: theme.colors().editor_background,
400            local_player: theme.players().local(),
401            text: text_style,
402            ..Default::default()
403        }
404    }
405
406    fn render_query_bar(&self, cx: &Context<Self>) -> impl IntoElement {
407        EditorElement::new(&self.query_bar, Self::editor_style(&self.query_bar, cx))
408    }
409
410    pub(crate) fn update_output(&mut self, window: &mut Window, cx: &mut Context<Self>) {
411        if self.update_output_task.is_some() {
412            return;
413        }
414        let session = self.session.clone();
415        let token = self.last_token;
416        self.update_output_task = Some(cx.spawn_in(window, async move |this, cx| {
417            let Some((last_processed_token, task)) = session
418                .update_in(cx, |session, window, cx| {
419                    let (output, last_processed_token) = session.output(token);
420
421                    this.update(cx, |this, cx| {
422                        if last_processed_token == this.last_token {
423                            return None;
424                        }
425                        Some((
426                            last_processed_token,
427                            this.add_messages(output.cloned().collect(), window, cx),
428                        ))
429                    })
430                    .ok()
431                    .flatten()
432                })
433                .ok()
434                .flatten()
435            else {
436                _ = this.update(cx, |this, _| {
437                    this.update_output_task.take();
438                });
439                return;
440            };
441            _ = task.await.log_err();
442            _ = this.update(cx, |this, _| {
443                this.last_token = last_processed_token;
444                this.update_output_task.take();
445            });
446        }));
447    }
448}
449
450impl Render for Console {
451    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
452        let query_focus_handle = self.query_bar.focus_handle(cx);
453        self.update_output(window, cx);
454        v_flex()
455            .track_focus(&self.focus_handle)
456            .key_context("DebugConsole")
457            .on_action(cx.listener(Self::evaluate))
458            .on_action(cx.listener(Self::watch_expression))
459            .size_full()
460            .child(self.render_console(cx))
461            .when(self.is_running(cx), |this| {
462                this.child(Divider::horizontal()).child(
463                    h_flex()
464                        .on_action(cx.listener(Self::previous_query))
465                        .on_action(cx.listener(Self::next_query))
466                        .gap_1()
467                        .bg(cx.theme().colors().editor_background)
468                        .child(self.render_query_bar(cx))
469                        .child(SplitButton::new(
470                            ui::ButtonLike::new_rounded_all(ElementId::Name(
471                                "split-button-left-confirm-button".into(),
472                            ))
473                            .on_click(move |_, window, cx| {
474                                window.dispatch_action(Box::new(Confirm), cx)
475                            })
476                            .tooltip({
477                                let query_focus_handle = query_focus_handle.clone();
478
479                                move |window, cx| {
480                                    Tooltip::for_action_in(
481                                        "Evaluate",
482                                        &Confirm,
483                                        &query_focus_handle,
484                                        window,
485                                        cx,
486                                    )
487                                }
488                            })
489                            .layer(ui::ElevationIndex::ModalSurface)
490                            .size(ui::ButtonSize::Compact)
491                            .child(Label::new("Evaluate")),
492                            self.render_submit_menu(
493                                ElementId::Name("split-button-right-confirm-button".into()),
494                                Some(query_focus_handle.clone()),
495                                cx,
496                            )
497                            .into_any_element(),
498                        )),
499                )
500            })
501            .border_2()
502    }
503}
504
505impl Focusable for Console {
506    fn focus_handle(&self, _cx: &App) -> gpui::FocusHandle {
507        self.focus_handle.clone()
508    }
509}
510
511struct ConsoleQueryBarCompletionProvider(WeakEntity<Console>);
512
513impl CompletionProvider for ConsoleQueryBarCompletionProvider {
514    fn completions(
515        &self,
516        _excerpt_id: ExcerptId,
517        buffer: &Entity<Buffer>,
518        buffer_position: language::Anchor,
519        _trigger: editor::CompletionContext,
520        _window: &mut Window,
521        cx: &mut Context<Editor>,
522    ) -> Task<Result<Vec<CompletionResponse>>> {
523        let Some(console) = self.0.upgrade() else {
524            return Task::ready(Ok(Vec::new()));
525        };
526
527        let support_completions = console
528            .read(cx)
529            .session
530            .read(cx)
531            .capabilities()
532            .supports_completions_request
533            .unwrap_or_default();
534
535        if support_completions {
536            self.client_completions(&console, buffer, buffer_position, cx)
537        } else {
538            self.variable_list_completions(&console, buffer, buffer_position, cx)
539        }
540    }
541
542    fn apply_additional_edits_for_completion(
543        &self,
544        _buffer: Entity<Buffer>,
545        _completions: Rc<RefCell<Box<[Completion]>>>,
546        _completion_index: usize,
547        _push_to_history: bool,
548        _cx: &mut Context<Editor>,
549    ) -> gpui::Task<anyhow::Result<Option<language::Transaction>>> {
550        Task::ready(Ok(None))
551    }
552
553    fn is_completion_trigger(
554        &self,
555        buffer: &Entity<Buffer>,
556        position: language::Anchor,
557        text: &str,
558        _trigger_in_words: bool,
559        menu_is_open: bool,
560        cx: &mut Context<Editor>,
561    ) -> bool {
562        let snapshot = buffer.read(cx).snapshot();
563        if !menu_is_open && !snapshot.settings_at(position, cx).show_completions_on_input {
564            return false;
565        }
566
567        self.0
568            .read_with(cx, |console, cx| {
569                console
570                    .session
571                    .read(cx)
572                    .capabilities()
573                    .completion_trigger_characters
574                    .as_ref()
575                    .map(|triggers| triggers.contains(&text.to_string()))
576            })
577            .ok()
578            .flatten()
579            .unwrap_or(true)
580    }
581}
582
583impl ConsoleQueryBarCompletionProvider {
584    fn variable_list_completions(
585        &self,
586        console: &Entity<Console>,
587        buffer: &Entity<Buffer>,
588        buffer_position: language::Anchor,
589        cx: &mut Context<Editor>,
590    ) -> Task<Result<Vec<CompletionResponse>>> {
591        let (variables, string_matches) = console.update(cx, |console, cx| {
592            let mut variables = HashMap::default();
593            let mut string_matches = Vec::default();
594
595            for variable in console.variable_list.update(cx, |variable_list, cx| {
596                variable_list.completion_variables(cx)
597            }) {
598                if let Some(evaluate_name) = &variable.evaluate_name {
599                    variables.insert(evaluate_name.clone(), variable.value.clone());
600                    string_matches.push(StringMatchCandidate {
601                        id: 0,
602                        string: evaluate_name.clone(),
603                        char_bag: evaluate_name.chars().collect(),
604                    });
605                }
606
607                variables.insert(variable.name.clone(), variable.value.clone());
608
609                string_matches.push(StringMatchCandidate {
610                    id: 0,
611                    string: variable.name.clone(),
612                    char_bag: variable.name.chars().collect(),
613                });
614            }
615
616            (variables, string_matches)
617        });
618
619        let snapshot = buffer.read(cx).text_snapshot();
620        let query = snapshot.text();
621        let replace_range = {
622            let buffer_offset = buffer_position.to_offset(&snapshot);
623            let reversed_chars = snapshot.reversed_chars_for_range(0..buffer_offset);
624            let mut word_len = 0;
625            for ch in reversed_chars {
626                if ch.is_alphanumeric() || ch == '_' {
627                    word_len += 1;
628                } else {
629                    break;
630                }
631            }
632            let word_start_offset = buffer_offset - word_len;
633            let start_anchor = snapshot.anchor_at(word_start_offset, Bias::Left);
634            start_anchor..buffer_position
635        };
636        cx.spawn(async move |_, cx| {
637            const LIMIT: usize = 10;
638            let matches = fuzzy::match_strings(
639                &string_matches,
640                &query,
641                true,
642                true,
643                LIMIT,
644                &Default::default(),
645                cx.background_executor().clone(),
646            )
647            .await;
648
649            let completions = matches
650                .iter()
651                .filter_map(|string_match| {
652                    let variable_value = variables.get(&string_match.string)?;
653
654                    Some(project::Completion {
655                        replace_range: replace_range.clone(),
656                        new_text: string_match.string.clone(),
657                        label: CodeLabel {
658                            filter_range: 0..string_match.string.len(),
659                            text: format!("{} {}", string_match.string, variable_value),
660                            runs: Vec::new(),
661                        },
662                        icon_path: None,
663                        documentation: None,
664                        confirm: None,
665                        source: project::CompletionSource::Custom,
666                        insert_text_mode: None,
667                    })
668                })
669                .collect::<Vec<_>>();
670
671            Ok(vec![project::CompletionResponse {
672                is_incomplete: completions.len() >= LIMIT,
673                completions,
674            }])
675        })
676    }
677
678    fn client_completions(
679        &self,
680        console: &Entity<Console>,
681        buffer: &Entity<Buffer>,
682        buffer_position: language::Anchor,
683        cx: &mut Context<Editor>,
684    ) -> Task<Result<Vec<CompletionResponse>>> {
685        let completion_task = console.update(cx, |console, cx| {
686            console.session.update(cx, |state, cx| {
687                let frame_id = console.stack_frame_list.read(cx).opened_stack_frame_id();
688
689                state.completions(
690                    CompletionsQuery::new(buffer.read(cx), buffer_position, frame_id),
691                    cx,
692                )
693            })
694        });
695        let snapshot = buffer.read(cx).text_snapshot();
696        cx.background_executor().spawn(async move {
697            let completions = completion_task.await?;
698
699            let completions = completions
700                .into_iter()
701                .map(|completion| {
702                    let new_text = completion
703                        .text
704                        .as_ref()
705                        .unwrap_or(&completion.label)
706                        .to_owned();
707                    let buffer_text = snapshot.text();
708                    let buffer_bytes = buffer_text.as_bytes();
709                    let new_bytes = new_text.as_bytes();
710
711                    let mut prefix_len = 0;
712                    for i in (0..new_bytes.len()).rev() {
713                        if buffer_bytes.ends_with(&new_bytes[0..i]) {
714                            prefix_len = i;
715                            break;
716                        }
717                    }
718
719                    let buffer_offset = buffer_position.to_offset(&snapshot);
720                    let start = buffer_offset - prefix_len;
721                    let start = snapshot.clip_offset(start, Bias::Left);
722                    let start = snapshot.anchor_before(start);
723                    let replace_range = start..buffer_position;
724
725                    project::Completion {
726                        replace_range,
727                        new_text,
728                        label: CodeLabel {
729                            filter_range: 0..completion.label.len(),
730                            text: completion.label,
731                            runs: Vec::new(),
732                        },
733                        icon_path: None,
734                        documentation: None,
735                        confirm: None,
736                        source: project::CompletionSource::BufferWord {
737                            word_range: buffer_position..language::Anchor::MAX,
738                            resolved: false,
739                        },
740                        insert_text_mode: None,
741                    }
742                })
743                .collect();
744
745            Ok(vec![project::CompletionResponse {
746                completions,
747                is_incomplete: false,
748            }])
749        })
750    }
751}
752
753#[derive(Default)]
754struct ConsoleHandler {
755    output: String,
756    spans: Vec<(Range<usize>, Option<ansi::Color>)>,
757    background_spans: Vec<(Range<usize>, Option<ansi::Color>)>,
758    current_range_start: usize,
759    current_background_range_start: usize,
760    current_color: Option<ansi::Color>,
761    current_background_color: Option<ansi::Color>,
762    pos: usize,
763}
764
765impl ConsoleHandler {
766    fn break_span(&mut self, color: Option<ansi::Color>) {
767        self.spans.push((
768            self.current_range_start..self.output.len(),
769            self.current_color,
770        ));
771        self.current_color = color;
772        self.current_range_start = self.pos;
773    }
774
775    fn break_background_span(&mut self, color: Option<ansi::Color>) {
776        self.background_spans.push((
777            self.current_background_range_start..self.output.len(),
778            self.current_background_color,
779        ));
780        self.current_background_color = color;
781        self.current_background_range_start = self.pos;
782    }
783}
784
785impl ansi::Handler for ConsoleHandler {
786    fn input(&mut self, c: char) {
787        self.output.push(c);
788        self.pos += c.len_utf8();
789    }
790
791    fn linefeed(&mut self) {
792        self.output.push('\n');
793        self.pos += 1;
794    }
795
796    fn put_tab(&mut self, count: u16) {
797        self.output
798            .extend(std::iter::repeat('\t').take(count as usize));
799        self.pos += count as usize;
800    }
801
802    fn terminal_attribute(&mut self, attr: ansi::Attr) {
803        match attr {
804            ansi::Attr::Foreground(color) => {
805                self.break_span(Some(color));
806            }
807            ansi::Attr::Background(color) => {
808                self.break_background_span(Some(color));
809            }
810            ansi::Attr::Reset => {
811                self.break_span(None);
812                self.break_background_span(None);
813            }
814            _ => {}
815        }
816    }
817}
818
819fn color_fetcher(color: ansi::Color) -> fn(&Theme) -> Hsla {
820    let color_fetcher: fn(&Theme) -> Hsla = match color {
821        // Named and theme defined colors
822        ansi::Color::Named(n) => match n {
823            ansi::NamedColor::Black => |theme| theme.colors().terminal_ansi_black,
824            ansi::NamedColor::Red => |theme| theme.colors().terminal_ansi_red,
825            ansi::NamedColor::Green => |theme| theme.colors().terminal_ansi_green,
826            ansi::NamedColor::Yellow => |theme| theme.colors().terminal_ansi_yellow,
827            ansi::NamedColor::Blue => |theme| theme.colors().terminal_ansi_blue,
828            ansi::NamedColor::Magenta => |theme| theme.colors().terminal_ansi_magenta,
829            ansi::NamedColor::Cyan => |theme| theme.colors().terminal_ansi_cyan,
830            ansi::NamedColor::White => |theme| theme.colors().terminal_ansi_white,
831            ansi::NamedColor::BrightBlack => |theme| theme.colors().terminal_ansi_bright_black,
832            ansi::NamedColor::BrightRed => |theme| theme.colors().terminal_ansi_bright_red,
833            ansi::NamedColor::BrightGreen => |theme| theme.colors().terminal_ansi_bright_green,
834            ansi::NamedColor::BrightYellow => |theme| theme.colors().terminal_ansi_bright_yellow,
835            ansi::NamedColor::BrightBlue => |theme| theme.colors().terminal_ansi_bright_blue,
836            ansi::NamedColor::BrightMagenta => |theme| theme.colors().terminal_ansi_bright_magenta,
837            ansi::NamedColor::BrightCyan => |theme| theme.colors().terminal_ansi_bright_cyan,
838            ansi::NamedColor::BrightWhite => |theme| theme.colors().terminal_ansi_bright_white,
839            ansi::NamedColor::Foreground => |theme| theme.colors().terminal_foreground,
840            ansi::NamedColor::Background => |theme| theme.colors().terminal_background,
841            ansi::NamedColor::Cursor => |theme| theme.players().local().cursor,
842            ansi::NamedColor::DimBlack => |theme| theme.colors().terminal_ansi_dim_black,
843            ansi::NamedColor::DimRed => |theme| theme.colors().terminal_ansi_dim_red,
844            ansi::NamedColor::DimGreen => |theme| theme.colors().terminal_ansi_dim_green,
845            ansi::NamedColor::DimYellow => |theme| theme.colors().terminal_ansi_dim_yellow,
846            ansi::NamedColor::DimBlue => |theme| theme.colors().terminal_ansi_dim_blue,
847            ansi::NamedColor::DimMagenta => |theme| theme.colors().terminal_ansi_dim_magenta,
848            ansi::NamedColor::DimCyan => |theme| theme.colors().terminal_ansi_dim_cyan,
849            ansi::NamedColor::DimWhite => |theme| theme.colors().terminal_ansi_dim_white,
850            ansi::NamedColor::BrightForeground => |theme| theme.colors().terminal_bright_foreground,
851            ansi::NamedColor::DimForeground => |theme| theme.colors().terminal_dim_foreground,
852        },
853        // 'True' colors
854        ansi::Color::Spec(_) => |theme| theme.colors().editor_background,
855        // 8 bit, indexed colors
856        ansi::Color::Indexed(i) => {
857            match i {
858                // 0-15 are the same as the named colors above
859                0 => |theme| theme.colors().terminal_ansi_black,
860                1 => |theme| theme.colors().terminal_ansi_red,
861                2 => |theme| theme.colors().terminal_ansi_green,
862                3 => |theme| theme.colors().terminal_ansi_yellow,
863                4 => |theme| theme.colors().terminal_ansi_blue,
864                5 => |theme| theme.colors().terminal_ansi_magenta,
865                6 => |theme| theme.colors().terminal_ansi_cyan,
866                7 => |theme| theme.colors().terminal_ansi_white,
867                8 => |theme| theme.colors().terminal_ansi_bright_black,
868                9 => |theme| theme.colors().terminal_ansi_bright_red,
869                10 => |theme| theme.colors().terminal_ansi_bright_green,
870                11 => |theme| theme.colors().terminal_ansi_bright_yellow,
871                12 => |theme| theme.colors().terminal_ansi_bright_blue,
872                13 => |theme| theme.colors().terminal_ansi_bright_magenta,
873                14 => |theme| theme.colors().terminal_ansi_bright_cyan,
874                15 => |theme| theme.colors().terminal_ansi_bright_white,
875                // 16-231 are a 6x6x6 RGB color cube, mapped to 0-255 using steps defined by XTerm.
876                // See: https://github.com/xterm-x11/xterm-snapshots/blob/master/256colres.pl
877                // 16..=231 => {
878                //     let (r, g, b) = rgb_for_index(index as u8);
879                //     rgba_color(
880                //         if r == 0 { 0 } else { r * 40 + 55 },
881                //         if g == 0 { 0 } else { g * 40 + 55 },
882                //         if b == 0 { 0 } else { b * 40 + 55 },
883                //     )
884                // }
885                // 232-255 are a 24-step grayscale ramp from (8, 8, 8) to (238, 238, 238).
886                // 232..=255 => {
887                //     let i = index as u8 - 232; // Align index to 0..24
888                //     let value = i * 10 + 8;
889                //     rgba_color(value, value, value)
890                // }
891                // For compatibility with the alacritty::Colors interface
892                // See: https://github.com/alacritty/alacritty/blob/master/alacritty_terminal/src/term/color.rs
893                _ => |_| gpui::black(),
894            }
895        }
896    };
897    color_fetcher
898}