pub fn history(
    out_writer: &mut impl Write,
    err_writer: &mut impl Write,
    session: &mut Session,
    argv: &[String],
) -> BuiltinResult {
    let app = app_from_crate!()
        .name("history")
        .max_term_width(80)
        .about("examine and manipulate the command history, if session is frozen this command wil ALWAYS fail")
        .after_help("if no subcommand is specified, then only commands run with the same mode and base command  along with builtins are shown")
        .subcommand(
            SubCommand::with_name("sync")
                .about("flush the current in-memory history into the history file"),
        )
        .subcommand(SubCommand::with_name("filter").about("filter history to only show the command you want to see")
            .arg(Arg::with_name("mode-filter").short("m").long("mode").takes_value(true).help("only show commands from the given shell execution mod (only useful"))
            .arg(Arg::with_name("base-filter").short("b").long("base").takes_value(true).help("only show commands whose 'base' matches the given base or have no base (may only be used when '--mode' is 'wrapped')"))
            .arg(Arg::with_name("show-builtin").short("s").long("show-builtin").help("do not ignore builtins and run the same filter checks on builtins as with other commands"))
            .arg(Arg::with_name("pattern").help("the optional regex pattern to search"))
        );

    let matches = handle_matches!(app, argv);

    match matches.subcommand() {
        ("sync", Some(_)) => {
            if let Err(err) = session.history_sync() {
                write!(err_writer, "Error saving to history file: {}", err)?;
            }
        }
        ("filter", Some(sub_matches)) => {
            let base_filter = sub_matches.value_of("base-filter");
            let mode_filter = match sub_matches.value_of("mode-filter") {
                Some(mode) => {
                    if let Ok(parsed) = SessionMode::from_str(mode) {
                        Some(parsed)
                    } else {
                        write!(
                            err_writer,
                            "could not parse value '{}', as SessionMode",
                            mode
                        )?;
                        return Err(WrashError::NonZeroExit(1));
                    }
                }
                None => None,
            };
            let show_builtin = sub_matches.is_present("show-builtin");

            let pattern = if let Some(s) = sub_matches.value_of("pattern") {
                match Regex::new(s) {
                    Ok(r) => Some(r),
                    Err(_) => {
                        write!(err_writer, "invalid regex pattern '{}'", s)?;
                        return Err(WrashError::NonZeroExit(1));
                    }
                }
            } else {
                None
            };

            if base_filter.is_some()
                && mode_filter.is_some()
                && mode_filter.unwrap() != SessionMode::Wrapped
            {
                write!(
                    err_writer,
                    "option '--base' may not be used when '--mode' is not 'wrapped'"
                )?;
                return Err(WrashError::NonZeroExit(1));
            }

            for (i, entry) in session
                .history_iter()
                .filter(|entry| {
                    if entry.is_builtin && !show_builtin {
                        return false;
                    }

                    if let Some(base) = base_filter {
                        if entry.base.is_none() || entry.base.as_ref().unwrap() != base {
                            return false;
                        }
                    }

                    if let Some(mode) = mode_filter {
                        if entry.mode != mode {
                            return false;
                        }
                    }

                    if let Some(pattern) = &pattern {
                        pattern.is_match(entry.get_command().as_str())
                    } else {
                        true
                    }
                })
                .enumerate()
            {
                writeln!(out_writer, "{}: {}", i, entry.get_command())?;
            }
        }
        _ => {
            for (i, entry) in session
                .history_iter()
                .filter(|entry| {
                    entry.is_builtin
                        || (entry.mode == session.mode()
                            && (entry.base.is_none()
                                || entry.base.as_ref().unwrap() == session.base))
                })
                .enumerate()
            {
                writeln!(out_writer, "{}: {}", i, entry.get_command())?;
            }
        }
    }

    Ok(())
}
/// Examine and manipulate the command history, if the command was run in "wrapped" mode,
Examine and manipulate the command history, if the command was run in "wrapped" mode.
Examine and manipulate the command history if the command was run in " wrapped " mode .
pub fn history(
    out_writer: &mut impl Write,
    err_writer: &mut impl Write,
    session: &mut Session,
    argv: &[String],
) -> BuiltinResult {
    let app = app_from_crate!()
        .name("history")
        .max_term_width(80)
        .about("examine and manipulate the command history, if session is frozen this command wil ALWAYS fail")
        .after_help("if no subcommand is specified, then only commands run with the same mode and base command  along with builtins are shown")
        .subcommand(
            SubCommand::with_name("sync")
                .about("flush the current in-memory history into the history file"),
        )
        .subcommand(SubCommand::with_name("filter").about("filter history to only show the command you want to see")
            .arg(Arg::with_name("mode-filter").short("m").long("mode").takes_value(true).help("only show commands from the given shell execution mod (only useful"))
            .arg(Arg::with_name("base-filter").short("b").long("base").takes_value(true).help("only show commands whose 'base' matches the given base or have no base (may only be used when '--mode' is 'wrapped')"))
            .arg(Arg::with_name("show-builtin").short("s").long("show-builtin").help("do not ignore builtins and run the same filter checks on builtins as with other commands"))
            .arg(Arg::with_name("pattern").help("the optional regex pattern to search"))
        );
    let matches = handle_matches!(app, argv);
    match matches.subcommand() {
        ("sync", Some(_)) => {
            if let Err(err) = session.history_sync() {
                write!(err_writer, "Error saving to history file: {}", err)?;
            }
        }
        ("filter", Some(sub_matches)) => {
            let base_filter = sub_matches.value_of("base-filter");
            let mode_filter = match sub_matches.value_of("mode-filter") {
                Some(mode) => {
                    if let Ok(parsed) = SessionMode::from_str(mode) {
                        Some(parsed)
                    } else {
                        write!(
                            err_writer,
                            "could not parse value '{}', as SessionMode",
                            mode
                        )?;
                        return Err(WrashError::NonZeroExit(1));
                    }
                }
                None => None,
            };
            let show_builtin = sub_matches.is_present("show-builtin");
            let pattern = if let Some(s) = sub_matches.value_of("pattern") {
                match Regex::new(s) {
                    Ok(r) => Some(r),
                    Err(_) => {
                        write!(err_writer, "invalid regex pattern '{}'", s)?;
                        return Err(WrashError::NonZeroExit(1));
                    }
                }
            } else {
                None
            };
            if base_filter.is_some()
                && mode_filter.is_some()
                && mode_filter.unwrap() != SessionMode::Wrapped
            {
                write!(
                    err_writer,
                    "option '--base' may not be used when '--mode' is not 'wrapped'"
                )?;
                return Err(WrashError::NonZeroExit(1));
            }
            for (i, entry) in session
                .history_iter()
                .filter(|entry| {
                    if entry.is_builtin && !show_builtin {
                        return false;
                    }
                    if let Some(base) = base_filter {
                        if entry.base.is_none() || entry.base.as_ref().unwrap() != base {
                            return false;
                        }
                    }
                    if let Some(mode) = mode_filter {
                        if entry.mode != mode {
                            return false;
                        }
                    }
                    if let Some(pattern) = &pattern {
                        pattern.is_match(entry.get_command().as_str())
                    } else {
                        true
                    }
                })
                .enumerate()
            {
                writeln!(out_writer, "{}: {}", i, entry.get_command())?;
            }
        }
        _ => {
            for (i, entry) in session
                .history_iter()
                .filter(|entry| {
                    entry.is_builtin
                        || (entry.mode == session.mode()
                            && (entry.base.is_none()
                                || entry.base.as_ref().unwrap() == session.base))
                })
                .enumerate()
            {
                writeln!(out_writer, "{}: {}", i, entry.get_command())?;
            }
        }
    }
    Ok(())
}
pub fn history ( out_writer : & mut impl Write , err_writer : & mut impl Write , session : & mut Session , argv : & [ String ] , ) -> BuiltinResult { let app = app_from_crate ! ( ) . name ( " " ) . max_term_width ( 80 ) . about ( " " ) . after_help ( " " ) . subcommand ( SubCommand :: with_name ( " " ) . about ( " " ) , ) . subcommand ( SubCommand :: with_name ( " " ) . about ( " " ) . arg ( Arg :: with_name ( " " ) . short ( " " ) . long ( " " ) . takes_value ( true ) . help ( " " ) ) . arg ( Arg :: with_name ( " " ) . short ( " " ) . long ( " " ) . takes_value ( true ) . help ( " " ) ) . arg ( Arg :: with_name ( " " ) . short ( " " ) . long ( " " ) . help ( " " ) ) . arg ( Arg :: with_name ( " " ) . help ( " " ) ) ) ; let matches = handle_matches ! ( app argv ) ; match matches . subcommand ( ) { ( " " , Some ( _ ) ) => { if let Err ( err ) = session . history_sync ( ) { write ! ( err_writer " " err ) ? ; } } ( " " , Some ( sub_matches ) ) => { let base_filter = sub_matches . value_of ( " " ) ; let mode_filter = match sub_matches . value_of ( " " ) { Some ( mode ) => { if let Ok ( parsed ) = SessionMode :: from_str ( mode ) { Some ( parsed ) } else { write ! ( err_writer " " mode ) ? ; return Err ( WrashError :: NonZeroExit ( 1 ) ) ; } } None => None , } ; let show_builtin = sub_matches . is_present ( " " ) ; let pattern = if let Some ( s ) = sub_matches . value_of ( " " ) { match Regex :: new ( s ) { Ok ( r ) => Some ( r ) , Err ( _ ) => { write ! ( err_writer " " s ) ? ; return Err ( WrashError :: NonZeroExit ( 1 ) ) ; } } } else { None } ; if base_filter . is_some ( ) && mode_filter . is_some ( ) && mode_filter . unwrap ( ) != SessionMode :: Wrapped { write ! ( err_writer " " ) ? ; return Err ( WrashError :: NonZeroExit ( 1 ) ) ; } for ( i , entry ) in session . history_iter ( ) . filter ( | entry | { if entry . is_builtin && ! show_builtin { return false ; } if let Some ( base ) = base_filter { if entry . base . is_none ( ) || entry . base . as_ref ( ) . unwrap ( ) != base { return false ; } } if let Some ( mode ) = mode_filter { if entry . mode != mode { return false ; } } if let Some ( pattern ) = & pattern { pattern . is_match ( entry . get_command ( ) . as_str ( ) ) } else { true } } ) . enumerate ( ) { writeln ! ( out_writer " " i entry get_command ( ) ) ? ; } } _ => { for ( i , entry ) in session . history_iter ( ) . filter ( | entry | { entry . is_builtin || ( entry . mode == session . mode ( ) && ( entry . base . is_none ( ) || entry . base . as_ref ( ) . unwrap ( ) == session . base ) ) } ) . enumerate ( ) { writeln ! ( out_writer " " i entry get_command ( ) ) ? ; } } } Ok ( ( ) ) }
Examine and manipulate the command history, if the command was run in "wrapped" mode,
Examine and manipulate the command history if the command was run in " wrapped " mode
out_writer
&mut impl Write
err_writer
&mut impl Write
session
&mut Session
argv
&[String]