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

Nithin/docs #467

Merged
merged 3 commits into from
May 30, 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/alias.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Shell aliasing
//! Command aliases
//!
//! Aliases can be specified as a key value pair of the alias name and the actual command it expands to. Keep in mind that aliases are not evaluated or syntax checked at time of definition, only during substitution. This means that it is possible to define aliases that are invalid commands.
//!
Expand Down
7 changes: 4 additions & 3 deletions crates/shrs_core/src/cmd_output.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Command output is used by shell builtins as well as shell languages to pass return state of
//! commands or programs. This captures stdout and stderr output, as well as exit code
//! Describes the output of a command

use std::{os::unix::process::ExitStatusExt, process::ExitStatus};

/// Describes the output of a command
///
/// Command output is used by shell builtins as well as shell languages to pass return state of
/// commands or programs. This captures stdout and stderr output, as well as exit code
#[derive(Clone, Debug)]
pub struct CmdOutput {
pub stdout: String,
Expand Down
27 changes: 23 additions & 4 deletions crates/shrs_core/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
//! Command queue for mutating `States` and `Shell` in handlers
//!
//! Commands can be pushed into a queue in handlers
//! and will be run directly after it ends.
//! `Commands` is stored in `Shell`, which can be accessed in handlers.
//! ```rust
//! pub struct C{}
//! fn add_state(sh:&Shell){
//! sh.run_cmd(|sh:&mut Shell, states: &mut States|{
//! states.insert(C{});
//! });
//! }
//! ```
//! Functions with the correct signature automatically implement `Command`.

use std::{cell::RefCell, collections::VecDeque};

use crate::{prelude::Shell, state::States};

/// Functions that can mutate `Shell` and `State`
///
/// Trait automatically implemented for functions of the correct signature.
/// Runs after handler completes
pub trait Command: Send + 'static {
fn apply(&self, sh: &mut Shell, states: &mut States);
}
Expand All @@ -16,21 +35,21 @@ where
}

pub struct Commands {
pub queue: RefCell<VecDeque<Box<dyn Command>>>,
queue: RefCell<VecDeque<Box<dyn Command>>>,
}

impl Commands {
pub fn new() -> Commands {
pub(crate) fn new() -> Commands {
Commands {
queue: RefCell::new(VecDeque::new()),
}
}

pub fn run<C: Command + 'static>(&self, command: C) {
pub(crate) fn run<C: Command + 'static>(&self, command: C) {
self.queue.borrow_mut().push_back(Box::new(command));
}

pub fn drain(&self, states: &States) -> VecDeque<Box<dyn Command>> {
pub(crate) fn drain(&self, states: &States) -> VecDeque<Box<dyn Command>> {
self.queue.borrow_mut().drain(..).collect()
}
}
2 changes: 1 addition & 1 deletion crates/shrs_core/src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! An event that can be emitted at any time in the shell lifecycle, by either the
//! shell directly, or as a custom event used in a 3rd party plugin. It is then possible to
//! register a hook on this event to be ran every time the event occurs.
//! To define your own event, you must satisfy the [`HookEventMarker`] marker trait. An easy way to
//! To define your own event, you must satisfy the [`HookEventMarker`] trait. An easy way to
//! do this is to use the [`HookEvent`] derive macro.
//! ```
//! # use shrs_core::prelude::*;
Expand Down
9 changes: 5 additions & 4 deletions crates/shrs_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ extern crate lazy_static;

pub mod alias;
pub mod builtin;
pub mod cmd_output;
pub mod commands;
pub mod completion;
pub mod env;
Expand All @@ -16,16 +15,19 @@ pub mod keybinding;
pub mod lang;
#[macro_use]
mod macros;
pub mod output_writer;
mod cmd_output;
mod output_writer;
pub mod plugin;
pub mod prompt;
pub mod prompt_content_queue;
pub mod readline;
pub mod shell;
pub mod signal;
pub mod state;
pub mod theme;

pub use cmd_output::CmdOutput;
pub use output_writer::OutputWriter;

pub mod prelude {
//! Conveniently import commonly used types

Expand All @@ -49,7 +51,6 @@ pub mod prelude {
prompt_content_queue::{PromptContent, PromptContentQueue},
readline::{prompt::*, *},
shell::{set_working_dir, Runtime, Shell, ShellBuilder, ShellConfig},
signal::Signals,
state::*,
theme::Theme,
};
Expand Down
11 changes: 11 additions & 0 deletions crates/shrs_core/src/output_writer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Writer for printing to stdout and stderr

use std::{
fmt::Display,
io::{stderr, stdout, BufWriter, Write},
Expand All @@ -8,6 +10,15 @@ use crossterm::{
QueueableCommand,
};
use shrs_utils::styled_buf::StyledBuf;
/// Printing in handlers should be done through `OutputWriter`,
/// which automatically uses the configured out and err colors.
/// It also records output in commands so that it can be collected into `CmdOutput`
/// ```rust
/// fn hello(mut out: StateMut<OutputWriter>)->Result<()>{
/// out.println("Hello")?;
/// Ok(())
/// }
/// ```

pub struct OutputWriter {
stdout: BufWriter<std::io::Stdout>,
Expand Down
6 changes: 5 additions & 1 deletion crates/shrs_core/src/prompt_content_queue.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Queue for automatic insertion into prompt
use std::collections::VecDeque;

/// Holds the content to be inserted
pub struct PromptContent {
pub content: String,
///The command will automatically run if true
pub auto_run: bool,
}
impl PromptContent {
Expand All @@ -18,9 +20,11 @@ impl PromptContentQueue {
queue: VecDeque::new(),
}
}
///Push content onto the back of queue
pub fn push(&mut self, value: PromptContent) {
self.queue.push_back(value)
}
///Pop from front of queue
pub fn pop(&mut self) -> Option<PromptContent> {
self.queue.pop_front()
}
Expand Down
17 changes: 0 additions & 17 deletions crates/shrs_core/src/signal.rs

This file was deleted.

22 changes: 11 additions & 11 deletions plugins/shrs_cd_tools/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,17 @@ mod tests {

use super::{QueryBuilder, QueryResult};

#[test]
fn basic() {
let query = QueryBuilder::default()
.files(vec![String::from(".vimrc")])
.build()
.unwrap();

// TODO make proper test (that works on all dev machines)
let path = PathBuf::from("/home/pinosaur");
assert!(query.scan(&path).matched);
}
// #[test]
// fn basic() {
// let query = QueryBuilder::default()
// .files(vec![String::from(".vimrc")])
// .build()
// .unwrap();

// // TODO make proper test (that works on all dev machines)
// let path = PathBuf::from("/home/pinosaur");
// assert!(query.scan(&path).matched);
// }

#[derive(Debug, Deserialize, PartialEq, Eq)]
struct TestParse {
Expand Down
2 changes: 1 addition & 1 deletion plugins/shrs_mux/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
process::{ChildStderr, ChildStdout},
};

use shrs::output_writer::OutputWriter;
use shrs::OutputWriter;

pub fn read_out(
out: &mut OutputWriter,
Expand Down