mirror of
https://github.com/nikolassv/bartib
synced 2024-11-28 06:20:28 +00:00
format code
This commit is contained in:
parent
e32ed45110
commit
1cbcf1453c
4 changed files with 26 additions and 21 deletions
|
@ -115,8 +115,10 @@ fn print_activity_with_line(activity: &Activity, line_number: usize) {
|
|||
"{} (Started: {}, Ended: {}, Line: {})\n",
|
||||
activity.description,
|
||||
activity.start.format(conf::FORMAT_DATETIME),
|
||||
activity
|
||||
.end.map_or_else(|| String::from("--"), |end| end.format(conf::FORMAT_DATETIME).to_string()),
|
||||
activity.end.map_or_else(
|
||||
|| String::from("--"),
|
||||
|end| end.format(conf::FORMAT_DATETIME).to_string()
|
||||
),
|
||||
line_number
|
||||
)
|
||||
}
|
||||
|
|
|
@ -23,7 +23,8 @@ pub enum ActivityError {
|
|||
}
|
||||
|
||||
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 {
|
||||
start: time.unwrap_or_else(|| Local::now().naive_local()),
|
||||
end: None,
|
||||
|
@ -36,11 +37,13 @@ impl Activity {
|
|||
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()
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_duration(&self) -> Duration {
|
||||
#[must_use]
|
||||
pub fn get_duration(&self) -> Duration {
|
||||
if let Some(end) = self.end {
|
||||
end.signed_duration_since(self.start)
|
||||
} else {
|
||||
|
|
|
@ -13,7 +13,8 @@ pub struct ActivityFilter<'a> {
|
|||
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],
|
||||
) -> Vec<(&String, &String)> {
|
||||
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
|
||||
}
|
||||
|
||||
#[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)
|
||||
.filter(|activity| !activity.is_stopped())
|
||||
.collect()
|
||||
|
@ -91,14 +93,11 @@ pub fn filter_activities<'a>(
|
|||
.filter(move |activity| {
|
||||
activity.start.date() >= from_date && activity.start.date() <= to_date
|
||||
})
|
||||
.filter(move |activity| {
|
||||
filter
|
||||
.project
|
||||
.map_or(true, |p| activity.project == *p)
|
||||
})
|
||||
.filter(move |activity| 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)
|
||||
.filter(|activity| activity.is_stopped())
|
||||
.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],
|
||||
) -> Option<&activity::Activity> {
|
||||
get_activities(file_content).max_by_key(|activity| activity.start)
|
||||
|
|
|
@ -190,8 +190,7 @@ impl Table {
|
|||
|
||||
impl fmt::Display for Table {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let terminal_width = term_size::dimensions_stdout()
|
||||
.map_or(conf::DEFAULT_WIDTH, |d| d.0);
|
||||
let terminal_width = term_size::dimensions_stdout().map_or(conf::DEFAULT_WIDTH, |d| d.0);
|
||||
|
||||
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();
|
||||
|
||||
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 (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
|
||||
// space will be styled (e.g. underlined)
|
||||
write!(
|
||||
f,
|
||||
"{style_prefix}{content:\u{a0}<width$}{style_suffix} "
|
||||
)
|
||||
write!(f, "{style_prefix}{content:\u{a0}<width$}{style_suffix} ")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in a new issue