aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2023-03-26 00:10:35 -0400
committerJesse Luehrs <doy@tozt.net>2023-03-26 00:10:35 -0400
commit93e6a9a08aec3cbb8b847a815ad905aa0d0bffca (patch)
tree9c9d3922475e846bd11293d91ddee36b56302355
parentf252f4c6595421339cb3119f1ca8ef491077f365 (diff)
downloadfancy-prompt-93e6a9a08aec3cbb8b847a815ad905aa0d0bffca.tar.gz
fancy-prompt-93e6a9a08aec3cbb8b847a815ad905aa0d0bffca.zip
fmt
-rw-r--r--.rustfmt.toml5
-rw-r--r--src/args.rs27
-rw-r--r--src/colors.rs23
-rw-r--r--src/data.rs6
-rw-r--r--src/main.rs4
-rw-r--r--src/power/mod.rs17
-rw-r--r--src/power/sys/linux.rs7
-rw-r--r--src/prompt.rs146
-rw-r--r--src/sys/unix.rs14
-rw-r--r--src/vcs/git.rs31
-rw-r--r--src/vcs/mod.rs12
-rw-r--r--src/verbose.rs22
12 files changed, 141 insertions, 173 deletions
diff --git a/.rustfmt.toml b/.rustfmt.toml
index 6735a78..55b0b14 100644
--- a/.rustfmt.toml
+++ b/.rustfmt.toml
@@ -1,5 +1,2 @@
+edition = "2018"
max_width = 78
-control_brace_style = "ClosingNextLine"
-use_small_heuristics = false
-combine_control_expr = false
-struct_lit_single_line = false
diff --git a/src/args.rs b/src/args.rs
index bae31f2..5186373 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -12,15 +12,18 @@ pub fn parse() -> CommandLineOptions {
.about("Prints a fancy prompt")
.author(crate_authors!())
.version(crate_version!())
- .arg(clap::Arg::with_name("prompt-escape")
- .long("prompt-escape")
- .value_name("SHELL")
- .help("Produces escape sequence wrappers for the given shell")
- .takes_value(true))
- .arg(clap::Arg::with_name("error-code")
- .value_name("ERROR_CODE")
- .help("The error code of the previously run command")
- )
+ .arg(
+ clap::Arg::with_name("prompt-escape")
+ .long("prompt-escape")
+ .value_name("SHELL")
+ .help("Produces escape sequence wrappers for the given shell")
+ .takes_value(true),
+ )
+ .arg(
+ clap::Arg::with_name("error-code")
+ .value_name("ERROR_CODE")
+ .help("The error code of the previously run command"),
+ )
.get_matches();
let shell = matches
@@ -32,9 +35,5 @@ pub fn parse() -> CommandLineOptions {
.map(|code| code.parse().expect("error code must be a u8"))
.unwrap_or(0);
- CommandLineOptions {
- shell,
- error_code,
- }
+ CommandLineOptions { shell, error_code }
}
-
diff --git a/src/colors.rs b/src/colors.rs
index 0e53fdc..084e4f2 100644
--- a/src/colors.rs
+++ b/src/colors.rs
@@ -97,7 +97,7 @@ impl Colors {
pub fn print<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
color: &str,
text: &str,
) {
@@ -107,7 +107,7 @@ impl Colors {
pub fn pad<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
len: usize,
) {
write!(t, "{}", " ".repeat(len)).unwrap();
@@ -115,14 +115,14 @@ impl Colors {
pub fn newline<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
) {
write!(t, "{}", "\n").unwrap();
}
pub fn print_host<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
host: Option<&str>,
text: &str,
) {
@@ -134,7 +134,7 @@ impl Colors {
pub fn print_user<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
user: Option<&str>,
text: &str,
) {
@@ -146,7 +146,7 @@ impl Colors {
fn print_with_color<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
color: Option<&term::color::Color>,
text: &str,
) {
@@ -157,7 +157,7 @@ impl Colors {
fn print_reset<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
) {
self.print_wrapped(t, |t| {
t.reset().unwrap();
@@ -166,7 +166,7 @@ impl Colors {
fn print_color<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
color: Option<&term::color::Color>,
) {
self.print_wrapped(t, |t| {
@@ -190,11 +190,10 @@ impl Colors {
fn print_wrapped<W: std::io::Write, T>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
printer: T,
- )
- where
- T: FnOnce(&mut dyn term::Terminal<Output=W>),
+ ) where
+ T: FnOnce(&mut dyn term::Terminal<Output = W>),
{
match self.shell_type {
ShellType::Bash => {
diff --git a/src/data.rs b/src/data.rs
index 322f373..c2ea49e 100644
--- a/src/data.rs
+++ b/src/data.rs
@@ -62,8 +62,7 @@ fn hostname() -> Option<String> {
name.truncate(idx);
}
Some(name)
- }
- else {
+ } else {
None
}
}
@@ -71,8 +70,7 @@ fn hostname() -> Option<String> {
fn terminal_cols() -> Option<usize> {
if let Some((w, _h)) = term_size::dimensions() {
Some(w)
- }
- else {
+ } else {
None
}
}
diff --git a/src/main.rs b/src/main.rs
index b32c2b7..328dc8a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -44,7 +44,9 @@ mod tests {
error_code: 0,
hostname: Some(String::from("hush")),
terminal_cols: Some(80),
- pwd: Some(std::path::PathBuf::from("/home/doy/coding/fancy-prompt")),
+ pwd: Some(std::path::PathBuf::from(
+ "/home/doy/coding/fancy-prompt",
+ )),
home: Some(std::path::PathBuf::from("/home/doy")),
user: Some(String::from("doy")),
is_root: false,
diff --git a/src/power/mod.rs b/src/power/mod.rs
index afe5194..fb75499 100644
--- a/src/power/mod.rs
+++ b/src/power/mod.rs
@@ -15,9 +15,7 @@ impl PowerInfo {
pub fn new() -> PowerInfo {
let power_supplies = sys::power_supplies();
- PowerInfo {
- power_supplies,
- }
+ PowerInfo { power_supplies }
}
pub fn battery_usage(&self) -> Option<f64> {
@@ -26,22 +24,19 @@ impl PowerInfo {
for battery in self.batteries() {
if let Some(now) = battery.energy_now {
total_now += now;
- }
- else {
+ } else {
return None;
}
if let Some(full) = battery.energy_full {
total_full += full;
- }
- else {
+ } else {
return None;
}
}
if total_full > 0 {
Some((total_now as f64) / (total_full as f64))
- }
- else {
+ } else {
None
}
}
@@ -59,13 +54,13 @@ impl PowerInfo {
self.batteries().count() > 0
}
- fn batteries(&self) -> impl Iterator<Item=&sys::PowerSupplyInfo> {
+ fn batteries(&self) -> impl Iterator<Item = &sys::PowerSupplyInfo> {
self.power_supplies
.iter()
.filter(|p| p.ty == sys::PowerSupplyType::Battery)
}
- fn mains(&self) -> impl Iterator<Item=&sys::PowerSupplyInfo> {
+ fn mains(&self) -> impl Iterator<Item = &sys::PowerSupplyInfo> {
self.power_supplies
.iter()
.filter(|p| p.ty == sys::PowerSupplyType::AC)
diff --git a/src/power/sys/linux.rs b/src/power/sys/linux.rs
index 9b4bcc4..90115a2 100644
--- a/src/power/sys/linux.rs
+++ b/src/power/sys/linux.rs
@@ -18,10 +18,11 @@ pub fn power_supplies() -> Vec<super::PowerSupplyInfo> {
let ty = super::super::slurp(entry.path().join("type"))
.map(|t: String| super::PowerSupplyType::from_str(&t))
.expect("couldn't find power supply type");
- let energy_full = super::super::slurp(entry.path().join("energy_full"));
+ let energy_full =
+ super::super::slurp(entry.path().join("energy_full"));
let energy_now = super::super::slurp(entry.path().join("energy_now"));
- let online =
- super::super::slurp(entry.path().join("online")).map(|n: u8| n != 0);
+ let online = super::super::slurp(entry.path().join("online"))
+ .map(|n: u8| n != 0);
power_supplies.push(super::PowerSupplyInfo {
name,
diff --git a/src/prompt.rs b/src/prompt.rs
index 39411f0..a68b01e 100644
--- a/src/prompt.rs
+++ b/src/prompt.rs
@@ -17,25 +17,24 @@ pub struct Prompt {
impl Prompt {
pub fn new(data: data::PromptData) -> Prompt {
let colors = colors::Colors::new(data.shell.clone());
- Prompt {
- colors,
- data,
- }
+ Prompt { colors, data }
}
pub fn display<W: std::io::Write>(&self, w: W) {
let mut t = term::TerminfoTerminal::new(w).unwrap();
- let user =
- self.data.user
- .as_ref()
- .map(String::as_ref)
- .unwrap_or_else(|| "???");
- let host =
- self.data.hostname
- .as_ref()
- .map(String::as_ref)
- .unwrap_or_else(|| "???");
+ let user = self
+ .data
+ .user
+ .as_ref()
+ .map(String::as_ref)
+ .unwrap_or_else(|| "???");
+ let host = self
+ .data
+ .hostname
+ .as_ref()
+ .map(String::as_ref)
+ .unwrap_or_else(|| "???");
let max_vcs_len = 20; // "g*+?:mybr...nch:+1-1"
let vcs = self.format_vcs();
@@ -57,13 +56,14 @@ impl Prompt {
- user.len() - 1 - host.len() // "doy@lance"
- 1 // " "
- 10 // "[19:40:50]"
- - 1; // " "
+ - 1; // " "
if self.data.power_info.has_batteries() {
max_path_len -= battery_len + 2 // "{<=========}"
- + 1; // " "
+ + 1; // " "
}
- if max_path_len < 10 { // "~/a/...cde"
+ if max_path_len < 10 {
+ // "~/a/...cde"
panic!(
"terminal too small (need at least {} cols)",
cols + 10 - max_path_len
@@ -113,7 +113,7 @@ impl Prompt {
fn display_path<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
path: &str,
path_color: &str,
vcs: Option<&str>,
@@ -130,16 +130,16 @@ impl Prompt {
fn display_border<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
- len: usize
+ t: &mut dyn term::Terminal<Output = W>,
+ len: usize,
) {
self.colors.print(t, "default", &"-".repeat(len));
}
fn display_battery<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
- len: usize
+ t: &mut dyn term::Terminal<Output = W>,
+ len: usize,
) {
self.print_host(t, "{");
if let Some(battery_usage) = self.data.power_info.battery_usage() {
@@ -153,17 +153,18 @@ impl Prompt {
if len >= filled {
if charging {
self.colors.print(t, "battery_charging", "<");
- }
- else {
+ } else {
self.colors.print(t, color, ">");
}
}
if filled > 1 {
- self.colors
- .print(t, "battery_charging", &"=".repeat(filled - 1));
+ self.colors.print(
+ t,
+ "battery_charging",
+ &"=".repeat(filled - 1),
+ );
}
- }
- else {
+ } else {
self.colors.print(t, "error", &"?".repeat(len));
}
self.print_host(t, "}");
@@ -171,7 +172,7 @@ impl Prompt {
fn display_identity<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
user: &str,
host: &str,
) {
@@ -182,7 +183,7 @@ impl Prompt {
fn display_time<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
) {
self.print_host(t, "[");
self.colors.print(
@@ -195,31 +196,25 @@ impl Prompt {
fn display_error_code<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
) {
let error_code_color = if self.data.error_code == 0 {
"default"
- }
- else {
+ } else {
"error"
};
self.colors.print(
t,
error_code_color,
- &format!("{:03}", self.data.error_code)
+ &format!("{:03}", self.data.error_code),
);
}
fn display_prompt<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
+ t: &mut dyn term::Terminal<Output = W>,
) {
- let prompt = if self.data.is_root {
- "#"
- }
- else {
- "$"
- };
+ let prompt = if self.data.is_root { "#" } else { "$" };
self.print_user(t, prompt);
}
@@ -233,16 +228,16 @@ impl Prompt {
fn print_host<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
- text: &str
+ t: &mut dyn term::Terminal<Output = W>,
+ text: &str,
) {
self.colors.print_host(t, self.hostname(), text);
}
fn print_user<W: std::io::Write>(
&self,
- t: &mut dyn term::Terminal<Output=W>,
- text: &str
+ t: &mut dyn term::Terminal<Output = W>,
+ text: &str,
) {
self.colors.print_user(t, self.user(), text);
}
@@ -259,35 +254,27 @@ impl Prompt {
fn battery_discharge_color(usage: f64, charging: bool) -> &'static str {
if usage >= 0.8 {
"battery_full"
- }
- else if charging {
+ } else if charging {
"default"
- }
- else if usage >= 0.4 {
+ } else if usage >= 0.4 {
"default"
- }
- else if usage >= 0.15 {
+ } else if usage >= 0.15 {
"battery_warn"
- }
- else if usage >= 0.05 {
+ } else if usage >= 0.05 {
"battery_crit"
- }
- else {
+ } else {
"battery_emerg"
}
}
fn path_color(path: Option<&std::path::Path>) -> String {
path.as_ref()
- .map(|path| {
- match sys::path_writable(path) {
- sys::PathWritability::Writable
- => String::from("default"),
- sys::PathWritability::NotWritable
- => String::from("path_not_writable"),
- sys::PathWritability::NotExist
- => String::from("path_not_exist"),
+ .map(|path| match sys::path_writable(path) {
+ sys::PathWritability::Writable => String::from("default"),
+ sys::PathWritability::NotWritable => {
+ String::from("path_not_writable")
}
+ sys::PathWritability::NotExist => String::from("path_not_exist"),
})
.unwrap_or_else(|| String::from("path_not_exist"))
}
@@ -317,8 +304,7 @@ fn format_vcs(vcs_info: Option<&dyn vcs::VcsInfo>) -> Option<String> {
.map(|branch| {
if branch == "master" {
String::new()
- }
- else {
+ } else {
branch
}
})
@@ -338,8 +324,7 @@ fn format_vcs(vcs_info: Option<&dyn vcs::VcsInfo>) -> Option<String> {
if remote > 0 {
write!(vcs, "-{}", remote).unwrap();
}
- }
- else {
+ } else {
write!(vcs, ":-").unwrap();
}
@@ -360,11 +345,9 @@ fn vcs_color(vcs_info: Option<&dyn vcs::VcsInfo>) -> String {
.map(|vcs_info| {
if vcs_info.is_error() {
String::from("vcs_error")
- }
- else if vcs_info.is_dirty() {
+ } else if vcs_info.is_dirty() {
String::from("vcs_dirty")
- }
- else {
+ } else {
String::from("default")
}
})
@@ -387,7 +370,8 @@ where
let home_str = home.as_ref().to_string_lossy().into_owned();
let home_re = regex::Regex::new(
&(String::from(r"^") + &regex::escape(&home_str)),
- ).unwrap();
+ )
+ .unwrap();
path_str = home_re.replace(&path_str, "~").into_owned();
}
@@ -404,13 +388,13 @@ where
}
if path_str.len() > len {
- path_str = String::from(&path_str[..len - 6]) + "..."
+ path_str = String::from(&path_str[..len - 6])
+ + "..."
+ &path_str[path_str.len() - 3..]
}
path_str
- }
- else {
+ } else {
String::from("???")
}
}
@@ -422,22 +406,24 @@ fn compress_vcs(vcs: &str, len: usize) -> String {
vcs_parts_re
.captures(vcs)
.map(|cap| {
- let prefix_len = cap.get(1)
+ let prefix_len = cap
+ .get(1)
.map(|mat| mat.end() - mat.start() + 1)
.unwrap_or(0);
- let suffix_len = cap.get(2)
+ let suffix_len = cap
+ .get(2)
.map(|mat| mat.end() - mat.start() + 1)
.unwrap_or(0);
let branch_len = len - prefix_len - suffix_len;
let branch_re = regex::Regex::new(&format!(
r"(:[^:]{{{}}})[^:]*([^:]{{3}}:?)",
(branch_len - 6).to_string()
- )).unwrap();
+ ))
+ .unwrap();
branch_re.replace(vcs, "$1...$2").into_owned()
})
.unwrap_or_else(|| vcs.to_string())
- }
- else {
+ } else {
vcs.to_string()
}
}
diff --git a/src/sys/unix.rs b/src/sys/unix.rs
index 0f433b3..2d52627 100644
--- a/src/sys/unix.rs
+++ b/src/sys/unix.rs
@@ -1,5 +1,5 @@
-use users;
use std;
+use users;
use std::os::unix::fs::MetadataExt;
use std::os::unix::fs::PermissionsExt;
@@ -16,17 +16,13 @@ pub fn path_writable(path: &std::path::Path) -> super::PathWritability {
if euid == 0 {
super::PathWritability::Writable
- }
- else if (file_uid == euid) && (file_mode & 0o200 != 0) {
+ } else if (file_uid == euid) && (file_mode & 0o200 != 0) {
super::PathWritability::Writable
- }
- else if (file_gid == egid) && (file_mode & 0o020 != 0) {
+ } else if (file_gid == egid) && (file_mode & 0o020 != 0) {
super::PathWritability::Writable
- }
- else if file_mode & 0o002 != 0 {
+ } else if file_mode & 0o002 != 0 {
super::PathWritability::Writable
- }
- else {
+ } else {
super::PathWritability::NotWritable
}
})
diff --git a/src/vcs/git.rs b/src/vcs/git.rs
index 5b8ad96..1a5b856 100644
--- a/src/vcs/git.rs
+++ b/src/vcs/git.rs
@@ -38,8 +38,7 @@ impl GitInfo {
if true {
// XXX
status_options.update_index(true);
- }
- else {
+ } else {
status_options.update_index(false);
status_options.no_refresh(true);
}
@@ -73,8 +72,7 @@ impl GitInfo {
let branch = head.ok().and_then(|head| {
if head.is_branch() {
head.shorthand().map(|s| s.to_string())
- }
- else {
+ } else {
head.resolve().ok().and_then(|head| head.target()).map(
|oid| {
let mut sha = String::new();
@@ -109,16 +107,10 @@ impl GitInfo {
};
talk_about_time!("active operation");
- let remote_branch_diff = git.head()
+ let remote_branch_diff = git
+ .head()
.ok()
- .and_then(|head| {
- if head.is_branch() {
- Some(head)
- }
- else {
- None
- }
- })
+ .and_then(|head| if head.is_branch() { Some(head) } else { None })
.and_then(|head| head.resolve().ok())
.map(|head| {
(head.target(), head.shorthand().map(|s| s.to_string()))
@@ -128,11 +120,11 @@ impl GitInfo {
name.and_then(|name| {
git.refname_to_id(
&(String::from("refs/remotes/origin/") + &name),
- ).ok()
- .and_then(|remote_id| {
- git.graph_ahead_behind(head_id, remote_id)
- .ok()
- })
+ )
+ .ok()
+ .and_then(|remote_id| {
+ git.graph_ahead_behind(head_id, remote_id).ok()
+ })
})
})
});
@@ -195,8 +187,7 @@ pub fn detect() -> Option<Box<dyn super::VcsInfo>> {
if let Some(git) = git {
Some(Box::new(GitInfo::new(&git)))
- }
- else {
+ } else {
None
}
}
diff --git a/src/vcs/mod.rs b/src/vcs/mod.rs
index e672ec6..f033d2c 100644
--- a/src/vcs/mod.rs
+++ b/src/vcs/mod.rs
@@ -27,9 +27,12 @@ pub trait VcsInfo {
fn is_dirty(&self) -> bool {
let diff = self.remote_branch_diff();
- self.has_modified_files() || self.has_staged_files()
- || self.has_new_files() || !diff.is_some()
- || diff.map(|(local, remote)| local > 0 || remote > 0)
+ self.has_modified_files()
+ || self.has_staged_files()
+ || self.has_new_files()
+ || !diff.is_some()
+ || diff
+ .map(|(local, remote)| local > 0 || remote > 0)
.unwrap_or(false)
}
@@ -41,8 +44,7 @@ pub trait VcsInfo {
pub fn detect() -> Option<Box<dyn VcsInfo>> {
if let Some(git) = git::detect() {
Some(git)
- }
- else {
+ } else {
None
}
}
diff --git a/src/verbose.rs b/src/verbose.rs
index 8f23c03..1b02799 100644
--- a/src/verbose.rs
+++ b/src/verbose.rs
@@ -2,11 +2,13 @@
use std;
#[cfg(feature = "verbose")]
-pub static mut STACK: Option<Vec<(String, std::time::Instant, std::time::Instant)>> = None;
+pub static mut STACK: Option<
+ Vec<(String, std::time::Instant, std::time::Instant)>,
+> = None;
#[cfg(feature = "verbose")]
macro_rules! start_talking_about_time {
- ($category:expr) => (
+ ($category:expr) => {
use std;
use verbose;
unsafe {
@@ -17,12 +19,12 @@ macro_rules! start_talking_about_time {
stack.push((String::from(category), now.clone(), now.clone()));
eprintln!("{}starting {}", " ".repeat(len), category);
}
- )
+ };
}
#[cfg(feature = "verbose")]
macro_rules! talk_about_time {
- ($what:expr) => (
+ ($what:expr) => {
unsafe {
let stack = verbose::STACK.get_or_insert_with(|| Vec::new());
let len = stack.len();
@@ -38,12 +40,12 @@ macro_rules! talk_about_time {
);
last.1 = std::time::Instant::now();
}
- )
+ };
}
#[cfg(feature = "verbose")]
macro_rules! stop_talking_about_time {
- () => (
+ () => {
unsafe {
let stack = verbose::STACK.get_or_insert_with(|| Vec::new());
let last = stack.pop().unwrap();
@@ -57,20 +59,20 @@ macro_rules! stop_talking_about_time {
elapsed.subsec_nanos()
);
}
- )
+ };
}
#[cfg(not(feature = "verbose"))]
macro_rules! start_talking_about_time {
- ($e:expr) => ()
+ ($e:expr) => {};
}
#[cfg(not(feature = "verbose"))]
macro_rules! talk_about_time {
- ($e:expr) => ()
+ ($e:expr) => {};
}
#[cfg(not(feature = "verbose"))]
macro_rules! stop_talking_about_time {
- () => ()
+ () => {};
}