format code

This commit is contained in:
Ernaldis 2023-09-15 13:43:58 -05:00
parent e32ed45110
commit 1cbcf1453c
No known key found for this signature in database
GPG key ID: 5C2C4C2F84BFC021
4 changed files with 26 additions and 21 deletions

View file

@ -115,8 +115,10 @@ fn print_activity_with_line(activity: &Activity, line_number: usize) {
"{} (Started: {}, Ended: {}, Line: {})\n", "{} (Started: {}, Ended: {}, Line: {})\n",
activity.description, activity.description,
activity.start.format(conf::FORMAT_DATETIME), activity.start.format(conf::FORMAT_DATETIME),
activity activity.end.map_or_else(
.end.map_or_else(|| String::from("--"), |end| end.format(conf::FORMAT_DATETIME).to_string()), || String::from("--"),
|end| end.format(conf::FORMAT_DATETIME).to_string()
),
line_number line_number
) )
} }

View file

@ -23,7 +23,8 @@ pub enum ActivityError {
} }
impl Activity { impl Activity {
#[must_use] pub fn start(project: String, description: String, time: Option<NaiveDateTime>) -> Self { #[must_use]
pub fn start(project: String, description: String, time: Option<NaiveDateTime>) -> Self {
Self { Self {
start: time.unwrap_or_else(|| Local::now().naive_local()), start: time.unwrap_or_else(|| Local::now().naive_local()),
end: None, end: None,
@ -36,11 +37,13 @@ impl Activity {
self.end = time.or_else(|| Some(Local::now().naive_local())); self.end = time.or_else(|| Some(Local::now().naive_local()));
} }
#[must_use] pub fn is_stopped(&self) -> bool { #[must_use]
pub fn is_stopped(&self) -> bool {
self.end.is_some() self.end.is_some()
} }
#[must_use] pub fn get_duration(&self) -> Duration { #[must_use]
pub fn get_duration(&self) -> Duration {
if let Some(end) = self.end { if let Some(end) = self.end {
end.signed_duration_since(self.start) end.signed_duration_since(self.start)
} else { } else {

View file

@ -13,7 +13,8 @@ pub struct ActivityFilter<'a> {
pub project: Option<&'a str>, pub project: Option<&'a str>,
} }
#[must_use] pub fn get_descriptions_and_projects( #[must_use]
pub fn get_descriptions_and_projects(
file_content: &[bartib_file::Line], file_content: &[bartib_file::Line],
) -> Vec<(&String, &String)> { ) -> Vec<(&String, &String)> {
let mut activities: Vec<&activity::Activity> = get_activities(file_content).collect(); let mut activities: Vec<&activity::Activity> = get_activities(file_content).collect();
@ -49,7 +50,8 @@ fn get_descriptions_and_projects_from_activities<'a>(
descriptions_and_projects descriptions_and_projects
} }
#[must_use] pub fn get_running_activities(file_content: &[bartib_file::Line]) -> Vec<&activity::Activity> { #[must_use]
pub fn get_running_activities(file_content: &[bartib_file::Line]) -> Vec<&activity::Activity> {
get_activities(file_content) get_activities(file_content)
.filter(|activity| !activity.is_stopped()) .filter(|activity| !activity.is_stopped())
.collect() .collect()
@ -91,14 +93,11 @@ pub fn filter_activities<'a>(
.filter(move |activity| { .filter(move |activity| {
activity.start.date() >= from_date && activity.start.date() <= to_date activity.start.date() >= from_date && activity.start.date() <= to_date
}) })
.filter(move |activity| { .filter(move |activity| filter.project.map_or(true, |p| activity.project == *p))
filter
.project
.map_or(true, |p| activity.project == *p)
})
} }
#[must_use] pub fn get_last_activity_by_end(file_content: &[bartib_file::Line]) -> Option<&activity::Activity> { #[must_use]
pub fn get_last_activity_by_end(file_content: &[bartib_file::Line]) -> Option<&activity::Activity> {
get_activities(file_content) get_activities(file_content)
.filter(|activity| activity.is_stopped()) .filter(|activity| activity.is_stopped())
.max_by_key(|activity| { .max_by_key(|activity| {
@ -108,7 +107,8 @@ pub fn filter_activities<'a>(
}) })
} }
#[must_use] pub fn get_last_activity_by_start( #[must_use]
pub fn get_last_activity_by_start(
file_content: &[bartib_file::Line], file_content: &[bartib_file::Line],
) -> Option<&activity::Activity> { ) -> Option<&activity::Activity> {
get_activities(file_content).max_by_key(|activity| activity.start) get_activities(file_content).max_by_key(|activity| activity.start)

View file

@ -190,8 +190,7 @@ impl Table {
impl fmt::Display for Table { impl fmt::Display for Table {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let terminal_width = term_size::dimensions_stdout() let terminal_width = term_size::dimensions_stdout().map_or(conf::DEFAULT_WIDTH, |d| d.0);
.map_or(conf::DEFAULT_WIDTH, |d| d.0);
let column_width = self.get_column_width(terminal_width - self.columns.len()); let column_width = self.get_column_width(terminal_width - self.columns.len());
@ -249,7 +248,11 @@ fn write_cells<T: AsRef<str> + std::fmt::Display>(
}) })
.collect(); .collect();
let most_lines: usize = wrapped_cells.iter().map(std::vec::Vec::len).max().unwrap_or(1); let most_lines: usize = wrapped_cells
.iter()
.map(std::vec::Vec::len)
.max()
.unwrap_or(1);
for line in 0..most_lines { for line in 0..most_lines {
for (width, wrapped_cell) in column_width.iter().zip(wrapped_cells.iter()) { for (width, wrapped_cell) in column_width.iter().zip(wrapped_cells.iter()) {
@ -279,10 +282,7 @@ fn write_with_width_and_style(
// cells are filled with non-breaking white space. Contrary to normal spaces non-breaking white // cells are filled with non-breaking white space. Contrary to normal spaces non-breaking white
// space will be styled (e.g. underlined) // space will be styled (e.g. underlined)
write!( write!(f, "{style_prefix}{content:\u{a0}<width$}{style_suffix} ")
f,
"{style_prefix}{content:\u{a0}<width$}{style_suffix} "
)
} }
#[cfg(test)] #[cfg(test)]