Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manpages plugin #401

Merged
merged 4 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/shrs_core/src/readline/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl LineState {
}

/// Get the contents of the prompt
fn get_full_command(&self) -> String {
pub fn get_full_command(&self) -> String {
let mut res: String = self.lines.clone();
let cur_line: String = self.cb.as_str().into();
res += cur_line.as_str();
Expand Down
16 changes: 16 additions & 0 deletions plugins/shrs_manpages/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "shrs_manpages"
version = "0.0.4"
description = "keybinding to open man page currently typed command"
readme = "README.md"

authors.workspace = true
categories.workspace = true
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
shrs = { path = "../../crates/shrs", version = "^0.0.4" }
39 changes: 39 additions & 0 deletions plugins/shrs_manpages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

<div align="center">

# shrs_manpages

keybinding to open man page currently typed command

[![crates.io](https://img.shields.io/crates/v/shrs_manpages.svg)](https://crates.io/crates/shrs_manpages)
[![MIT/Apache 2.0](https://img.shields.io/badge/license-MIT%2FApache-blue.svg)](#)

</div>

This is a plugin for [shrs](https://github.com/MrPicklePinosaur/shrs).

## Using this plugin

First add this plugin to your dependencies
```toml
shrs_manpages = { version = "0.0.4" }
```

Register your own keybinding with the manpage handler
```rust
use shrs::prelude::*;
use shrs_manpages::{open_manpage};

let keybinding = keybindings! {
|state|
"C-n" => ("Open manpage", { open_manpage(state); }),
};

let myshell = ShellBuilder::default()
.with_keybinding(keybinding)
.build()
.unwrap();

myshell.run();

```
17 changes: 17 additions & 0 deletions plugins/shrs_manpages/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use shrs::{prelude::*, keybindings};
use shrs_manpages::{open_manpage};

fn main() {

let keybinding = keybindings! {
|state|
"C-n" => ("Open manpage", { open_manpage(state); }),
};

let myshell = ShellBuilder::default()
.with_keybinding(keybinding)
.build()
.unwrap();

myshell.run();
}
32 changes: 32 additions & 0 deletions plugins/shrs_manpages/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::{process::{Command, Stdio}, ffi::OsStr};

use shrs::prelude::*;

/// 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(state: &mut LineStateBundle) {
_open_manpage(state, "man")
}

pub fn open_manpage_with<S: AsRef<OsStr>>(state: &mut LineStateBundle,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>>(state: &mut LineStateBundle, man_command: S){
// TODO IFS
let full_command = state.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
state.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
}