1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-06-09 11:52:21 +00:00

refactor: improve as-a-library usage by removing ArgMatches depe… (#547)

This removes ArgMatches from the Context struct and replaces it with a simple HashMap. This work is towards getting Starship in a better place for use as a library in other shells written in Rust so they don't need to use a command-line interface to invoke and configure things.

Contributes to #521
This commit is contained in:
Barnaby Keene 2019-10-20 09:26:27 +01:00 committed by Matan Kushner
parent 48cd6bc519
commit 2c7e01cd62
4 changed files with 30 additions and 15 deletions

View File

@ -4,10 +4,12 @@ use crate::module::Module;
use clap::ArgMatches;
use git2::{Repository, RepositoryState};
use once_cell::sync::OnceCell;
use std::collections::HashMap;
use std::env;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::string::String;
/// Context contains data or common methods that may be used by multiple modules.
/// The data contained within Context will be relevant to this particular rendering
@ -22,8 +24,8 @@ pub struct Context<'a> {
/// A vector containing the full paths of all the files in `current_dir`.
dir_files: OnceCell<Vec<PathBuf>>,
/// The map of arguments that were passed when starship was called.
pub arguments: ArgMatches<'a>,
/// Properties to provide to modules.
pub properties: HashMap<&'a str, String>,
/// Private field to store Git information for modules who need it
repo: OnceCell<Repo>,
@ -49,12 +51,22 @@ impl<'a> Context<'a> {
{
let config = StarshipConfig::initialize();
// Unwrap the clap arguments into a simple hashtable
// we only care about single arguments at this point, there isn't a
// use-case for a list of arguments yet.
let properties: HashMap<&str, std::string::String> = arguments
.args
.iter()
.filter(|(_, v)| !v.vals.is_empty())
.map(|(a, b)| (*a, b.vals.first().cloned().unwrap().into_string().unwrap()))
.collect();
// TODO: Currently gets the physical directory. Get the logical directory.
let current_dir = Context::expand_tilde(dir.into());
Context {
config,
arguments,
properties,
current_dir,
dir_files: OnceCell::new(),
repo: OnceCell::new(),

View File

@ -22,17 +22,20 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let config: CharacterConfig = CharacterConfig::try_load(module.config);
module.get_prefix().set_value("");
let arguments = &context.arguments;
let exit_success = arguments.value_of("status_code").unwrap_or("0") == "0";
let props = &context.properties;
let exit_code_default = std::string::String::from("0");
let exit_code = props.get("status_code").unwrap_or(&exit_code_default);
let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default();
let keymap = arguments.value_of("keymap").unwrap_or("viins");
let keymap_default = std::string::String::from("viins");
let keymap = props.get("keymap").unwrap_or(&keymap_default);
let exit_success = exit_code == "0";
// Match shell "keymap" names to normalized vi modes
// NOTE: in vi mode, fish reports normal mode as "default".
// Unfortunately, this is also the name of the non-vi default mode.
// We do some environment detection in src/init.rs to translate.
// The result: in non-vi fish, keymap is always reported as "insert"
let mode = match (shell.as_str(), keymap) {
let mode = match (shell.as_str(), keymap.as_str()) {
("fish", "default") | ("zsh", "vicmd") => ShellEditMode::Normal,
_ => ASSUMED_MODE,
};

View File

@ -11,10 +11,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("cmd_duration");
let config: CmdDurationConfig = CmdDurationConfig::try_load(module.config);
let arguments = &context.arguments;
let elapsed = arguments
.value_of("cmd_duration")
.unwrap_or("invalid_time")
let props = &context.properties;
let elapsed = props
.get("cmd_duration")
.unwrap_or(&"invalid_time".into())
.parse::<u64>()
.ok()?;

View File

@ -10,10 +10,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
module.set_style(config.style);
let arguments = &context.arguments;
let num_of_jobs = arguments
.value_of("jobs")
.unwrap_or("0")
let props = &context.properties;
let num_of_jobs = props
.get("jobs")
.unwrap_or(&"0".into())
.trim()
.parse::<i64>()
.ok()?;