summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-01-11 17:51:52 -0500
committerJesse Luehrs <doy@tozt.net>2022-01-11 17:51:52 -0500
commit88080132e8778ef7196c7a8fc30434a8a21d11ba (patch)
tree1e22a9cc96a0a5e16e9ffb6f9f9ec1ae2f291d59
parentbc31125d0d8e37c0f1c507b41229d5c65e210221 (diff)
downloadnbsh-88080132e8778ef7196c7a8fc30434a8a21d11ba.tar.gz
nbsh-88080132e8778ef7196c7a8fc30434a8a21d11ba.zip
detect git repositories
-rw-r--r--Cargo.lock174
-rw-r--r--Cargo.toml1
-rw-r--r--src/shell/mod.rs40
-rw-r--r--src/shell/readline.rs4
4 files changed, 213 insertions, 6 deletions
diff --git a/Cargo.lock b/Cargo.lock
index afe1d71..ac2d3c0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -233,6 +233,9 @@ name = "cc"
version = "1.0.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee"
+dependencies = [
+ "jobserver",
+]
[[package]]
name = "cfg-if"
@@ -300,6 +303,16 @@ dependencies = [
]
[[package]]
+name = "form_urlencoded"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191"
+dependencies = [
+ "matches",
+ "percent-encoding",
+]
+
+[[package]]
name = "futures-channel"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -376,6 +389,21 @@ dependencies = [
]
[[package]]
+name = "git2"
+version = "0.13.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f29229cc1b24c0e6062f6e742aa3e256492a5323365e5ed3413599f8a5eff7d6"
+dependencies = [
+ "bitflags",
+ "libc",
+ "libgit2-sys",
+ "log",
+ "openssl-probe",
+ "openssl-sys",
+ "url",
+]
+
+[[package]]
name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -415,6 +443,17 @@ dependencies = [
]
[[package]]
+name = "idna"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8"
+dependencies = [
+ "matches",
+ "unicode-bidi",
+ "unicode-normalization",
+]
+
+[[package]]
name = "instant"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -436,6 +475,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]]
+name = "jobserver"
+version = "0.1.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "af25a77299a7f711a01975c35a6a424eb6862092cc2d6c72c4ed6cbc56dfc1fa"
+dependencies = [
+ "libc",
+]
+
+[[package]]
name = "js-sys"
version = "0.3.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -466,6 +514,46 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
[[package]]
+name = "libgit2-sys"
+version = "0.12.26+1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "19e1c899248e606fbfe68dcb31d8b0176ebab833b103824af31bddf4b7457494"
+dependencies = [
+ "cc",
+ "libc",
+ "libssh2-sys",
+ "libz-sys",
+ "openssl-sys",
+ "pkg-config",
+]
+
+[[package]]
+name = "libssh2-sys"
+version = "0.2.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b094a36eb4b8b8c8a7b4b8ae43b2944502be3e59cd87687595cf6b0a71b3f4ca"
+dependencies = [
+ "cc",
+ "libc",
+ "libz-sys",
+ "openssl-sys",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
+name = "libz-sys"
+version = "1.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "de5435b8549c16d423ed0c03dbaafe57cf6c3344744f1242520d59c9d8ecec66"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
name = "log"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -488,6 +576,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4"
[[package]]
+name = "matches"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f"
+
+[[package]]
name = "memchr"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -512,6 +606,7 @@ dependencies = [
"blocking",
"futures-lite",
"futures-util",
+ "git2",
"glob",
"hostname",
"libc",
@@ -566,12 +661,37 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
[[package]]
+name = "openssl-probe"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "28988d872ab76095a6e6ac88d99b54fd267702734fd7ffe610ca27f533ddb95a"
+
+[[package]]
+name = "openssl-sys"
+version = "0.9.72"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7e46109c383602735fa0a2e48dd2b7c892b048e1bf69e5c3b1d804b7d9c203cb"
+dependencies = [
+ "autocfg",
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
name = "parking"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72"
[[package]]
+name = "percent-encoding"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
+
+[[package]]
name = "pest"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -627,6 +747,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
+name = "pkg-config"
+version = "0.3.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "58893f751c9b0412871a09abd62ecd2a00298c6c83befa223ef98c52aef40cbe"
+
+[[package]]
name = "polling"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -793,6 +919,21 @@ dependencies = [
]
[[package]]
+name = "tinyvec"
+version = "1.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2c1c1d5a42b6245520c249549ec267180beaffcc0615401ac8e31853d4b6d8d2"
+dependencies = [
+ "tinyvec_macros",
+]
+
+[[package]]
+name = "tinyvec_macros"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
+
+[[package]]
name = "typenum"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -805,6 +946,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c"
[[package]]
+name = "unicode-bidi"
+version = "0.3.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1a01404663e3db436ed2746d9fefef640d868edae3cceb81c3b8d5732fda678f"
+
+[[package]]
+name = "unicode-normalization"
+version = "0.1.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9"
+dependencies = [
+ "tinyvec",
+]
+
+[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -817,6 +973,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
+name = "url"
+version = "2.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c"
+dependencies = [
+ "form_urlencoded",
+ "idna",
+ "matches",
+ "percent-encoding",
+]
+
+[[package]]
name = "users"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -843,6 +1011,12 @@ dependencies = [
]
[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
+
+[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 26e05eb..46d8149 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,6 +12,7 @@ bincode = "1.3.3"
blocking = "1.1.0"
futures-lite = "1.12.0"
futures-util = "0.3.19"
+git2 = "0.13.25"
glob = "0.3.0"
hostname = "0.3.1"
libc = "0.2.112"
diff --git a/src/shell/mod.rs b/src/shell/mod.rs
index e9bd5de..f82909d 100644
--- a/src/shell/mod.rs
+++ b/src/shell/mod.rs
@@ -128,6 +128,7 @@ pub struct Shell {
readline: readline::Readline,
history: history::History,
env: Env,
+ git: Option<git2::Repository>,
focus: Focus,
scene: Scene,
escape: bool,
@@ -137,16 +138,20 @@ pub struct Shell {
impl Shell {
pub fn new(offset: time::UtcOffset) -> Self {
- Self {
+ let env = Env::new();
+ let mut self_ = Self {
readline: readline::Readline::new(),
history: history::History::new(),
- env: Env::new(),
+ env,
+ git: None,
focus: Focus::Readline,
scene: Scene::Readline,
escape: false,
hide_readline: false,
offset,
- }
+ };
+ self_.update_git();
+ self_
}
pub async fn render(
@@ -168,7 +173,13 @@ impl Shell {
)
.await?;
self.readline
- .render(out, &self.env, true, self.offset)
+ .render(
+ out,
+ &self.env,
+ self.git.as_ref(),
+ true,
+ self.offset,
+ )
.await?;
}
Focus::History(idx) => {
@@ -188,7 +199,13 @@ impl Shell {
.await?;
let pos = out.screen().cursor_position();
self.readline
- .render(out, &self.env, false, self.offset)
+ .render(
+ out,
+ &self.env,
+ self.git.as_ref(),
+ false,
+ self.offset,
+ )
.await?;
out.move_to(pos.0, pos.1);
}
@@ -204,7 +221,13 @@ impl Shell {
)
.await?;
self.readline
- .render(out, &self.env, idx.is_none(), self.offset)
+ .render(
+ out,
+ &self.env,
+ self.git.as_ref(),
+ idx.is_none(),
+ self.offset,
+ )
.await?;
out.hide_cursor(true);
}
@@ -274,6 +297,7 @@ impl Shell {
let idx = self.env.idx();
self.env = entry.env().clone();
self.env.set_idx(idx);
+ self.update_git();
}
self.set_focus(
if self.hide_readline {
@@ -558,4 +582,8 @@ impl Shell {
}
self.focus_idx().map_or(Focus::Readline, Focus::History)
}
+
+ fn update_git(&mut self) {
+ self.git = git2::Repository::discover(self.env.current_dir()).ok();
+ }
}
diff --git a/src/shell/readline.rs b/src/shell/readline.rs
index dca33f2..a1f90bb 100644
--- a/src/shell/readline.rs
+++ b/src/shell/readline.rs
@@ -23,6 +23,7 @@ impl Readline {
&self,
out: &mut impl textmode::Textmode,
env: &Env,
+ git: Option<&git2::Repository>,
focus: bool,
offset: time::UtcOffset,
) -> anyhow::Result<()> {
@@ -57,6 +58,9 @@ impl Readline {
}
out.write_str(" (");
out.write_str(&crate::format::path(pwd));
+ if git.is_some() {
+ out.write_str("|g");
+ }
out.write_str(")");
out.move_to(self.size.0 - 2, self.size.1 - 4 - idlen - timelen);
out.write_str(&id);