shrs_manpages 0.0.6

keybinding to open man page currently typed command
Documentation
use std::{ffi::OsStr, process::Command};

use anyhow::Result;
use shrs::{prelude::*, readline::line::LineContents};

/// Open a man page for the currently typed command using the `man` command by default.
/// If you wish to specify a different man command, use [open_manpage_with].
pub fn open_manpage(mut contents: StateMut<LineContents>, sh: &Shell) -> Result<()> {
    _open_manpage(&mut contents, "man");
    Ok(())
}

pub fn open_manpage_with<S: AsRef<OsStr>>(state: &mut LineContents, man_command: S) {
    _open_manpage(state, man_command)
}

/// Grab the current line and attempt to open man page of command
fn _open_manpage<S: AsRef<OsStr>>(line: &mut LineContents, man_command: S) {
    // TODO IFS
    let full_command = line.get_full_command();
    let Some(command) = full_command.split(' ').next() else {
        return;
    };

    // Spawn man command and pass `command` to it as the man page to open
    Command::new(man_command).arg(command).spawn().unwrap();

    // TODO: the old cursor buffer isn't actually preserved after executing the command.
    // so we need to save the old line and restore it after
    line.cb.clear();
    // let _ = state.line.cb.insert(cursor_buffer::Location::Front(), &full_command);

    // TODO after handling keybinding it seems that the line accepts the contents, so we
    // automatically run the command that was present before - open issue to allow keybindings to
    // decide if the line should accept after the keybinding or not
}