feat: separate view for status

This commit is contained in:
Airenas Vaiciunas 2024-03-18 18:36:11 +02:00 committed by Nikolas Schmidt-Voigt
parent 7f082cdf3e
commit a5f06ab7d1
5 changed files with 44 additions and 44 deletions

View file

@ -6,13 +6,13 @@ use crate::data::bartib_file;
use crate::data::filter::Filters;
use crate::data::getter;
use crate::data::processor;
use crate::view::status;
use crate::view::status::StatusReportData;
use crate::data::processor::StatusReportData;
pub fn show_status(
file_name: &str,
filter: getter::ActivityFilter,
processors: processor::ProcessorList,
writer: &dyn processor::StatusReportWriter,
) -> Result<()> {
let file_content = bartib_file::get_file_content(file_name)?;
let activities: Vec<&Activity> = getter::get_activities(&file_content).collect();
@ -53,13 +53,12 @@ pub fn show_status(
.map(|f| f.get_duration())
.sum();
status::show(StatusReportData {
let status_report_data = StatusReportData {
activity: current,
today,
current_week,
current_month,
project: filter.project,
});
Ok(())
};
writer.process(&status_report_data)
}

View file

@ -1,3 +1,4 @@
use anyhow::Result;
use chrono::Duration;
use crate::data::activity;
@ -9,6 +10,17 @@ pub trait ActivityProcessor {
fn process(&self, activity: &activity::Activity) -> activity::Activity;
}
pub struct StatusReportData<'a> {
pub activity: Option<&'a activity::Activity>,
pub project: Option<&'a str>,
pub today: Duration,
pub current_week: Duration,
pub current_month: Duration,
}
pub trait StatusReportWriter {
fn process<'a>(&self, data: &'a StatusReportData) -> Result<()>;
}
pub struct RoundProcessor {
pub round: Duration,
}

View file

@ -2,4 +2,4 @@ pub mod conf;
pub mod controller;
pub mod data;
mod view;
pub mod view;

View file

@ -1,4 +1,7 @@
use std::borrow::Borrow;
use anyhow::{bail, Context, Result};
use bartib::view::status::StatusReport;
use chrono::{Datelike, Duration, Local, NaiveDate, NaiveTime};
use clap::{crate_version, App, AppSettings, Arg, ArgMatches, SubCommand};
@ -348,7 +351,8 @@ fn run_subcommand(matches: &ArgMatches, file_name: &str) -> Result<()> {
("status", Some(sub_m)) => {
let filter = create_filter_for_arguments(sub_m);
let processors = create_processors_for_arguments(sub_m);
bartib::controller::status::show_status(file_name, filter, processors)
let writer = create_status_writer(sub_m);
bartib::controller::status::show_status(file_name, filter, processors, writer.borrow())
}
_ => bail!("Unknown command"),
}
@ -364,6 +368,11 @@ fn create_processors_for_arguments(sub_m: &ArgMatches) -> processor::ProcessorLi
processors
}
fn create_status_writer(_sub_m: &ArgMatches) -> Box<dyn processor::StatusReportWriter> {
let result = StatusReport {};
Box::new(result)
}
fn create_filter_for_arguments<'a>(sub_m: &'a ArgMatches) -> ActivityFilter<'a> {
let mut filter = ActivityFilter {
number_of_activities: get_number_argument_or_ignore(

View file

@ -3,50 +3,30 @@ use std::fmt;
use chrono::Duration;
use nu_ansi_term::{Color, Style};
use crate::conf;
use crate::data::activity;
use crate::data::processor::{StatusReportData, StatusReportWriter};
use crate::view::format_util;
pub struct StatusReportData<'a> {
pub activity: Option<&'a activity::Activity>,
pub project: Option<&'a str>,
pub today: Duration,
pub current_week: Duration,
pub current_month: Duration,
}
struct Report<'a> {
data: StatusReportData<'a>,
}
impl<'a> Report<'a> {
fn new(data: StatusReportData<'a>) -> Report<'a> {
Report { data }
}
}
impl<'a> fmt::Display for Report<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut longest_line = 30;
let terminal_width = term_size::dimensions_stdout().map_or(conf::DEFAULT_WIDTH, |d| d.0);
if terminal_width <= longest_line {
longest_line = terminal_width;
}
print_title(f, self.data.project)?;
print_activity(f, self.data.activity, self.data.project)?;
print_duration(f, "Today", self.data.today, longest_line)?;
print_duration(f, "Current week", self.data.current_week, longest_line)?;
print_duration(f, "Current month", self.data.current_month, longest_line)?;
pub struct StatusReport {}
impl StatusReportWriter for StatusReport {
fn process<'a>(&self, data: &'a StatusReportData) -> anyhow::Result<()> {
println!("{data}");
Ok(())
}
}
pub fn show(data: StatusReportData) {
let report = Report::new(data);
println!("{report}");
impl<'a> fmt::Display for StatusReportData<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let longest_line = 30;
print_title(f, self.project)?;
print_activity(f, self.activity, self.project)?;
print_duration(f, "Today", self.today, longest_line)?;
print_duration(f, "Current week", self.current_week, longest_line)?;
print_duration(f, "Current month", self.current_month, longest_line)?;
Ok(())
}
}
fn print_duration(