Made starship usage configurable (#2049)

Co-authored-by: Darren Schroeder <fdncred@hotmail.com>
This commit is contained in:
Darren Schroeder 2020-06-25 00:44:55 -05:00 committed by GitHub
parent 4468947ad4
commit 6372d2a18c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 29 deletions

12
Cargo.lock generated
View File

@ -151,9 +151,9 @@ dependencies = [
[[package]] [[package]]
name = "attohttpc" name = "attohttpc"
version = "0.13.0" version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5db1932a9d70d5091139d6b0e04ec6a4d9f9184041c15d71a5ef954cb3c5312" checksum = "93610ce1c035e8a273fe56a19852e42798f3c019ca2726e52d2971197f116525"
dependencies = [ dependencies = [
"http 0.2.1", "http 0.2.1",
"log", "log",
@ -3962,9 +3962,9 @@ dependencies = [
[[package]] [[package]]
name = "starship" name = "starship"
version = "0.41.3" version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "891dec63d2b8ffed3d2388c0449dbd939f6da1b26747ba0ed1a01e4d47f32fce" checksum = "3e5fbb7eefc575ca97af2adcd9176933d28b0fc9357af45bac5d653c0e922342"
dependencies = [ dependencies = [
"ansi_term 0.12.1", "ansi_term 0.12.1",
"attohttpc", "attohttpc",
@ -4117,9 +4117,9 @@ dependencies = [
[[package]] [[package]]
name = "sysinfo" name = "sysinfo"
version = "0.14.5" version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b796215da5a4b2a1a5db53ee55866c13b74a89acd259ab762eb10e28e937cb5" checksum = "11c9e4fd26a5b1dfa0aceb8417d4825b65b49cf98b84a45b7af4007ad403e797"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"doc-comment", "doc-comment",

View File

@ -47,6 +47,7 @@ dunce = "1.0.0"
futures = { version = "0.3", features = ["compat", "io-compat"] } futures = { version = "0.3", features = ["compat", "io-compat"] }
log = "0.4.8" log = "0.4.8"
pretty_env_logger = "0.4.0" pretty_env_logger = "0.4.0"
starship = "0.42.0"
[dev-dependencies] [dev-dependencies]
nu-test-support = { version = "0.15.1", path = "./crates/nu-test-support" } 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] [features]
default = ["sys", "ps", "textview", "inc"] 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 # Default
textview = ["crossterm", "syntect", "url", "nu_plugin_textview"] textview = ["crossterm", "syntect", "url", "nu_plugin_textview"]
@ -76,7 +77,7 @@ tree = ["nu_plugin_tree"]
start = ["nu_plugin_start"] start = ["nu_plugin_start"]
clipboard-cli = ["nu-cli/clipboard-cli"] clipboard-cli = ["nu-cli/clipboard-cli"]
starship-prompt = ["nu-cli/starship-prompt"] # starship-prompt = ["nu-cli/starship-prompt"]
trash-support = ["nu-cli/trash-support"] trash-support = ["nu-cli/trash-support"]
# Core plugins that ship with `cargo install nu` by default # Core plugins that ship with `cargo install nu` by default

View File

@ -89,7 +89,7 @@ which = "4.0.1"
trash = { version = "1.0.1", optional = true } trash = { version = "1.0.1", optional = true }
clipboard = { version = "0.5", optional = true } clipboard = { version = "0.5", optional = true }
starship = { version = "0.41.3", optional = true } starship = "0.42.0"
rayon = "1.3.0" rayon = "1.3.0"
encoding_rs = "0.8.23" encoding_rs = "0.8.23"
@ -109,6 +109,6 @@ quickcheck_macros = "0.9"
[features] [features]
stable = [] stable = []
starship-prompt = ["starship"] # starship-prompt = ["starship"]
clipboard-cli = ["clipboard"] clipboard-cli = ["clipboard"]
trash-support = ["trash"] trash-support = ["trash"]

View File

@ -4,13 +4,11 @@ use crate::commands::plugin::JsonRpc;
use crate::commands::plugin::{PluginCommand, PluginSink}; use crate::commands::plugin::{PluginCommand, PluginSink};
use crate::commands::whole_stream_command; use crate::commands::whole_stream_command;
use crate::context::Context; use crate::context::Context;
#[cfg(not(feature = "starship-prompt"))]
use crate::git::current_branch; use crate::git::current_branch;
use crate::path::canonicalize; use crate::path::canonicalize;
use crate::prelude::*; use crate::prelude::*;
use crate::EnvironmentSyncer; use crate::EnvironmentSyncer;
use futures_codec::FramedRead; use futures_codec::FramedRead;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::hir::{ClassifiedCommand, Expression, InternalCommand, Literal, NamedArguments}; use nu_protocol::hir::{ClassifiedCommand, Expression, InternalCommand, Literal, NamedArguments};
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; 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()))); 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())? let edit_mode = config::config(Tag::unknown())?
.get("edit_mode") .get("edit_mode")
.map(|s| match s.value.expect_string() { .map(|s| match s.value.expect_string() {
@ -622,8 +629,7 @@ pub async fn cli(
rl.set_completion_type(completion_mode); rl.set_completion_type(completion_mode);
let colored_prompt = { let colored_prompt = {
#[cfg(feature = "starship-prompt")] if use_starship {
{
std::env::set_var("STARSHIP_SHELL", ""); std::env::set_var("STARSHIP_SHELL", "");
std::env::set_var("PWD", &cwd); std::env::set_var("PWD", &cwd);
let mut starship_context = let mut starship_context =
@ -639,9 +645,7 @@ pub async fn cli(
_ => {} _ => {}
}; };
starship::print::get_prompt(starship_context) starship::print::get_prompt(starship_context)
} } else {
#[cfg(not(feature = "starship-prompt"))]
{
format!( format!(
"\x1b[32m{}{}\x1b[m> ", "\x1b[32m{}{}\x1b[m> ",
cwd, cwd,

View File

@ -1,9 +1,18 @@
#![cfg(not(feature = "starship-prompt"))] use crate::prelude::*;
use git2::{Repository, RepositoryOpenFlags}; use git2::{Repository, RepositoryOpenFlags};
use std::ffi::OsString; use std::ffi::OsString;
pub fn current_branch() -> Option<String> { pub fn current_branch() -> Option<String> {
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<OsString> = vec![]; let v: Vec<OsString> = vec![];
match Repository::open_ext(".", RepositoryOpenFlags::empty(), v) { match Repository::open_ext(".", RepositoryOpenFlags::empty(), v) {
Ok(repo) => { Ok(repo) => {
@ -18,4 +27,10 @@ pub fn current_branch() -> Option<String> {
} }
_ => None, _ => None,
} }
} else {
None
}
} else {
None
}
} }