use std::borrow::Cow;
use lazy_static::lazy_static;
use regex::Regex;
use serde::Deserialize;
use crate::ansi;
use crate::config::{
delta_unreachable, GrepType, HunkHeaderIncludeFilePath, HunkHeaderIncludeLineNumber,
};
use crate::delta::{State, StateMachine};
use crate::handlers::{self, ripgrep_json};
use crate::paint::{self, BgShouldFill, StyleSectionSpecifier};
use crate::style::Style;
use crate::utils::{process, tabs};
use super::hunk_header::HunkHeaderIncludeHunkLabel;
#[derive(Debug, PartialEq, Eq)]
pub struct GrepLine<'b> {
pub grep_type: GrepType,
pub path: Cow<'b, str>,
pub line_number: Option<usize>,
pub line_type: LineType,
pub code: Cow<'b, str>,
pub submatches: Option<Vec<(usize, usize)>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LineType {
ContextHeader,
Context,
FileHeader,
Match,
Ignore,
}
struct GrepOutputConfig {
add_navigate_marker_to_matches: bool,
render_context_header_as_hunk_header: bool,
pad_line_number: bool,
}
lazy_static! {
static ref OUTPUT_CONFIG: GrepOutputConfig = make_output_config();
}
impl LineType {
fn file_path_separator(&self) -> &str {
// grep, rg, and git grep use ":" for matching lines
// and "-" for non-matching lines (and `git grep -W`
// uses "=" for a context header line).
match self {
LineType::Match => ":",
LineType::Context => "-",
LineType::ContextHeader => "=",
LineType::Ignore | LineType::FileHeader => "",
}
}
}
impl<'a> StateMachine<'a> {
// If this is a line of git grep output then render it accordingly.
pub fn handle_grep_line(&mut self) -> std::io::Result<bool> {
self.painter.emit()?;
let (previous_path, previous_line_type, previous_line, try_parse) = match &self.state {
State::Grep(_,