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

Feat/add chatsheets provider #2

Merged
merged 6 commits into from
Aug 1, 2023
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
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub enum CliCommands {
/// Use a custom provider URL for `eg` pages.
#[arg(long, env = "EG_PAGES_URL", value_name = "URL")]
eg_url: Option<String>,
/// Use a custom URL for cheat sheets.
#[arg(long, env = "CHEATSHEETS_URL", value_name = "URL")]
cheat_url: Option<String>,
/// Sets the pager to use.
#[arg(short, long)]
pager: Option<String>,
Expand Down Expand Up @@ -123,6 +126,7 @@ mod tests {
cmd: "ps".to_string(),
pager: Some("bat".to_string()),
cheat_sh_url: None,
cheat_url: None,
eg_url: None,
man_cmd: None,
no_pager: false,
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::error::Result;
use crate::helper::args::common::{HelpArg, VersionArg};
use crate::helper::args::FOUND_EMOTICON;
use crate::helper::docs::cheat_sh::DEFAULT_CHEAT_SHEET_PROVIDER;
use crate::helper::docs::cheatsheets::DEFAULT_CHEATSHEETS_PROVIDER;
use crate::helper::docs::eg::DEFAULT_EG_PAGES_PROVIDER;
use colored::*;
use serde::{Deserialize, Serialize};
Expand All @@ -28,6 +29,8 @@ pub struct Config {
pub cheat_sh_url: Option<String>,
/// Use a custom URL for `eg` pages provider.
pub eg_url: Option<String>,
/// Use a custom URL for cheatsheets provider.
pub cheatsheets_url: Option<String>,
}

impl Default for Config {
Expand All @@ -49,6 +52,7 @@ impl Default for Config {
pager_command: Some("less -R".to_string()),
cheat_sh_url: Some(DEFAULT_CHEAT_SHEET_PROVIDER.to_string()),
eg_url: Some(DEFAULT_EG_PAGES_PROVIDER.to_string()),
cheatsheets_url: Some(DEFAULT_CHEATSHEETS_PROVIDER.to_string()),
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions src/helper/docs/cheatsheets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::helper::docs::HelpProvider;
use ureq::{AgentBuilder, Request};

/// The default cheatsheets provider URL.
pub const DEFAULT_CHEATSHEETS_PROVIDER: &str =
"https://raw.githubusercontent.com/cheat/cheatsheets/master";

/// The `cheatsheets` provider
pub struct Cheatsheets;

impl HelpProvider for Cheatsheets {
fn url(&self) -> &'static str {
DEFAULT_CHEATSHEETS_PROVIDER
}

fn build_request(&self, cmd: &str, url: &str) -> Request {
AgentBuilder::new().build().get(&format!("{}/{}", url, cmd))
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::error::Result;

#[test]
fn test_fetch_cheatsheets() -> Result<()> {
let output = Cheatsheets.fetch("ls", &None)?;

assert!(output.contains(
r##"# To display everything in <dir>, including hidden files:
ls -a <dir>
"##
));
assert!(output.contains(
r##"# To display directories only, include hidden:
ls -d .*/ */ <dir>
"##
));

Ok(())
}
}
8 changes: 7 additions & 1 deletion src/helper/docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ pub mod man;

/// Cheat sheet helper.
pub mod cheat_sh;
/// cheat helper.
pub mod cheatsheets;
/// eg page helper.
pub mod eg;

use crate::config::Config;
use crate::error::{Error, Result};
use crate::helper::docs::cheat_sh::CheatDotSh;
use crate::helper::docs::cheatsheets::Cheatsheets;
use crate::helper::docs::eg::Eg;
use crate::helper::docs::man::show_man_page;
use console::{style, Style, Term};
Expand Down Expand Up @@ -111,11 +114,13 @@ pub fn get_docs_help<Output: Write>(cmd: &str, config: &Config, output: &mut Out
const MAN_PAGE: usize = 0;
const CHEAT_SHEET: usize = 1;
const EG_PAGE: usize = 2;
const CHEATSHEETS: usize = 3;

let menu_options = [
"Show man page",
"Show cheat sheet",
"Show cheat.sh page",
"Show the eg page",
"Show the cheatsheet page",
"Exit",
];
let mut selection = Some(MAN_PAGE);
Expand All @@ -133,6 +138,7 @@ pub fn get_docs_help<Output: Write>(cmd: &str, config: &Config, output: &mut Out
let page = match selection {
Some(CHEAT_SHEET) => CheatDotSh.fetch(cmd, &config.cheat_sh_url)?,
Some(EG_PAGE) => Eg.fetch(cmd, &config.eg_url)?,
Some(CHEATSHEETS) => Cheatsheets.fetch(cmd, &config.cheatsheets_url)?,
_ => return Ok(()),
};

Expand Down