From 6372d2a18c2ecc97d7a6b3ebfc2c01c59e99a9b3 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Thu, 25 Jun 2020 00:44:55 -0500 Subject: [PATCH] Made starship usage configurable (#2049) Co-authored-by: Darren Schroeder --- Cargo.lock | 12 ++++++------ Cargo.toml | 5 +++-- crates/nu-cli/Cargo.toml | 4 ++-- crates/nu-cli/src/cli.rs | 18 +++++++++++------- crates/nu-cli/src/git.rs | 39 +++++++++++++++++++++++++++------------ 5 files changed, 49 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e2955a4da1..d8b5ed2d16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -151,9 +151,9 @@ dependencies = [ [[package]] name = "attohttpc" -version = "0.13.0" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5db1932a9d70d5091139d6b0e04ec6a4d9f9184041c15d71a5ef954cb3c5312" +checksum = "93610ce1c035e8a273fe56a19852e42798f3c019ca2726e52d2971197f116525" dependencies = [ "http 0.2.1", "log", @@ -3962,9 +3962,9 @@ dependencies = [ [[package]] name = "starship" -version = "0.41.3" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "891dec63d2b8ffed3d2388c0449dbd939f6da1b26747ba0ed1a01e4d47f32fce" +checksum = "3e5fbb7eefc575ca97af2adcd9176933d28b0fc9357af45bac5d653c0e922342" dependencies = [ "ansi_term 0.12.1", "attohttpc", @@ -4117,9 +4117,9 @@ dependencies = [ [[package]] name = "sysinfo" -version = "0.14.5" +version = "0.14.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b796215da5a4b2a1a5db53ee55866c13b74a89acd259ab762eb10e28e937cb5" +checksum = "11c9e4fd26a5b1dfa0aceb8417d4825b65b49cf98b84a45b7af4007ad403e797" dependencies = [ "cfg-if", "doc-comment", diff --git a/Cargo.toml b/Cargo.toml index 51b41a59b7..ee9983f637 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,7 @@ dunce = "1.0.0" futures = { version = "0.3", features = ["compat", "io-compat"] } log = "0.4.8" pretty_env_logger = "0.4.0" +starship = "0.42.0" [dev-dependencies] nu-test-support = { version = "0.15.1", path = "./crates/nu-test-support" } @@ -58,7 +59,7 @@ nu-build = { version = "0.15.1", path = "./crates/nu-build" } [features] default = ["sys", "ps", "textview", "inc"] -stable = ["default", "starship-prompt", "binaryview", "match", "tree", "post", "fetch", "clipboard-cli", "trash-support", "start"] +stable = ["default", "binaryview", "match", "tree", "post", "fetch", "clipboard-cli", "trash-support", "start"] # Default textview = ["crossterm", "syntect", "url", "nu_plugin_textview"] @@ -76,7 +77,7 @@ tree = ["nu_plugin_tree"] start = ["nu_plugin_start"] clipboard-cli = ["nu-cli/clipboard-cli"] -starship-prompt = ["nu-cli/starship-prompt"] +# starship-prompt = ["nu-cli/starship-prompt"] trash-support = ["nu-cli/trash-support"] # Core plugins that ship with `cargo install nu` by default diff --git a/crates/nu-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index c2bc38df22..7179811a8c 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -89,7 +89,7 @@ which = "4.0.1" trash = { version = "1.0.1", optional = true } clipboard = { version = "0.5", optional = true } -starship = { version = "0.41.3", optional = true } +starship = "0.42.0" rayon = "1.3.0" encoding_rs = "0.8.23" @@ -109,6 +109,6 @@ quickcheck_macros = "0.9" [features] stable = [] -starship-prompt = ["starship"] +# starship-prompt = ["starship"] clipboard-cli = ["clipboard"] trash-support = ["trash"] diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index f7b75e956d..480a353530 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -4,13 +4,11 @@ use crate::commands::plugin::JsonRpc; use crate::commands::plugin::{PluginCommand, PluginSink}; use crate::commands::whole_stream_command; use crate::context::Context; -#[cfg(not(feature = "starship-prompt"))] use crate::git::current_branch; use crate::path::canonicalize; use crate::prelude::*; use crate::EnvironmentSyncer; use futures_codec::FramedRead; - use nu_errors::ShellError; use nu_protocol::hir::{ClassifiedCommand, Expression, InternalCommand, Literal, NamedArguments}; use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; @@ -585,6 +583,15 @@ pub async fn cli( rl.set_helper(Some(crate::shell::Helper::new(context.clone()))); + let config = config::config(Tag::unknown())?; + let use_starship = match config.get("use_starship") { + Some(b) => match b.as_bool() { + Ok(b) => b, + _ => false, + }, + _ => false, + }; + let edit_mode = config::config(Tag::unknown())? .get("edit_mode") .map(|s| match s.value.expect_string() { @@ -622,8 +629,7 @@ pub async fn cli( rl.set_completion_type(completion_mode); let colored_prompt = { - #[cfg(feature = "starship-prompt")] - { + if use_starship { std::env::set_var("STARSHIP_SHELL", ""); std::env::set_var("PWD", &cwd); let mut starship_context = @@ -639,9 +645,7 @@ pub async fn cli( _ => {} }; starship::print::get_prompt(starship_context) - } - #[cfg(not(feature = "starship-prompt"))] - { + } else { format!( "\x1b[32m{}{}\x1b[m> ", cwd, diff --git a/crates/nu-cli/src/git.rs b/crates/nu-cli/src/git.rs index 2dc4aa0d8b..ee2fc6df71 100644 --- a/crates/nu-cli/src/git.rs +++ b/crates/nu-cli/src/git.rs @@ -1,21 +1,36 @@ -#![cfg(not(feature = "starship-prompt"))] - +use crate::prelude::*; use git2::{Repository, RepositoryOpenFlags}; use std::ffi::OsString; pub fn current_branch() -> Option { - let v: Vec = vec![]; - match Repository::open_ext(".", RepositoryOpenFlags::empty(), v) { - Ok(repo) => { - let r = repo.head(); - match r { - Ok(r) => match r.shorthand() { - Some(s) => Some(s.to_string()), - None => None, - }, + if let Ok(config) = crate::data::config::config(Tag::unknown()) { + let use_starship = match config.get("use_starship") { + Some(b) => match b.as_bool() { + Ok(b) => b, + _ => false, + }, + _ => false, + }; + + if !use_starship { + let v: Vec = vec![]; + match Repository::open_ext(".", RepositoryOpenFlags::empty(), v) { + Ok(repo) => { + let r = repo.head(); + match r { + Ok(r) => match r.shorthand() { + Some(s) => Some(s.to_string()), + None => None, + }, + _ => None, + } + } _ => None, } + } else { + None } - _ => None, + } else { + None } }