Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/controller/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use crate::data::processor;
use crate::view::list;

// lists all currently running activities.
pub fn list_running(file_name: &str) -> Result<()> {
pub fn list_running(file_name: &str, separator: &str) -> Result<()> {
let file_content = bartib_file::get_file_content(file_name)?;
let running_activities = getter::get_running_activities(&file_content);

list::list_running_activities(&running_activities);
list::list_running_activities(&running_activities, separator);

Ok(())
}
Expand All @@ -28,6 +28,7 @@ pub fn list(
filter: getter::ActivityFilter,
do_group_activities: bool,
processors: processor::ProcessorList,
separator: &str,
) -> Result<()> {
let file_content = bartib_file::get_file_content(file_name)?;
let activities = getter::get_activities(&file_content).collect();
Expand All @@ -47,10 +48,14 @@ pub fn list(
);

if do_group_activities {
list::list_activities_grouped_by_date(&filtered_activities[first_element..]);
list::list_activities_grouped_by_date(&filtered_activities[first_element..], separator);
} else {
let with_start_dates = filter.date.is_none();
list::list_activities(&filtered_activities[first_element..], with_start_dates);
list::list_activities(
&filtered_activities[first_element..],
with_start_dates,
separator,
);
}

Ok(())
Expand Down Expand Up @@ -187,20 +192,20 @@ pub fn list_projects(file_name: &str, current: bool, no_quotes: bool) -> Result<
}

// return last finished activity
pub fn list_last_activities(file_name: &str, number: usize) -> Result<()> {
pub fn list_last_activities(file_name: &str, number: usize, separator: &str) -> Result<()> {
let file_content = bartib_file::get_file_content(file_name)?;

let descriptions_and_projects: Vec<(&String, &String)> =
getter::get_descriptions_and_projects(&file_content);
let first_element = descriptions_and_projects.len().saturating_sub(number);

list::list_descriptions_and_projects(&descriptions_and_projects[first_element..]);
list::list_descriptions_and_projects(&descriptions_and_projects[first_element..], separator);

Ok(())
}

// searches for the term in descriptions and projects
pub fn search(file_name: &str, search_term: Option<&str>) -> Result<()> {
pub fn search(file_name: &str, search_term: Option<&str>, separator: &str) -> Result<()> {
let search_term = search_term
.map(|term| format!("*{}*", term.to_lowercase()))
.unwrap_or("".to_string());
Expand All @@ -220,7 +225,11 @@ pub fn search(file_name: &str, search_term: Option<&str>) -> Result<()> {
})
.collect();

list::list_descriptions_and_projects_with_index(&matches, "No matching activities found");
list::list_descriptions_and_projects_with_index(
&matches,
"No matching activities found",
separator,
);

Ok(())
}
62 changes: 54 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,16 @@ To get started, view the `start` help with `bartib start --help`")
SubCommand::with_name("cancel").about("cancels all currently running activities"),
)
.subcommand(
SubCommand::with_name("current").about("lists all currently running activities"),
SubCommand::with_name("current").about("lists all currently running activities")
.arg(Arg::with_name("separator")
.short("s")
.long("separator")
.help("separator to seperate columns of the output table")
.required(false)
.takes_value(true)
.default_value("")
)
,
)
.subcommand(
SubCommand::with_name("list")
Expand Down Expand Up @@ -189,7 +198,15 @@ To get started, view the `start` help with `bartib start --help`")
.help("maximum number of activities to display")
.required(false)
.takes_value(true),
),
)
.arg(Arg::with_name("separator")
.short("s")
.long("separator")
.help("separator to seperate columns of the output table")
.required(false)
.takes_value(true)
.default_value("")
)
)
.subcommand(
SubCommand::with_name("report")
Expand Down Expand Up @@ -224,7 +241,16 @@ To get started, view the `start` help with `bartib start --help`")
.required(false)
.takes_value(true)
.default_value("10"),
),
)
.arg(Arg::with_name("separator")
.short("s")
.long("separator")
.help("separator to seperate columns of the output table")
.required(false)
.takes_value(true)
.default_value("")
)
,
)
.subcommand(
SubCommand::with_name("projects")
Expand Down Expand Up @@ -268,7 +294,15 @@ To get started, view the `start` help with `bartib start --help`")
.required(true)
.takes_value(true)
.default_value("''"),
),
)
.arg(Arg::with_name("separator")
.short("s")
.long("separator")
.help("separator to seperate columns of the output table")
.required(false)
.takes_value(true)
.default_value("")
)
)
.subcommand(
SubCommand::with_name("status")
Expand Down Expand Up @@ -342,12 +376,22 @@ fn run_subcommand(matches: &ArgMatches, file_name: &str) -> Result<()> {
bartib::controller::manipulation::stop(file_name, time)
}
("cancel", Some(_)) => bartib::controller::manipulation::cancel(file_name),
("current", Some(_)) => bartib::controller::list::list_running(file_name),
("current", Some(sub_m)) => {
let separator = sub_m.value_of("separator").unwrap();
bartib::controller::list::list_running(file_name, separator)
}
("list", Some(sub_m)) => {
let filter = create_filter_for_arguments(sub_m);
let processors = create_processors_for_arguments(sub_m);
let do_group_activities = !sub_m.is_present("no_grouping") && filter.date.is_none();
bartib::controller::list::list(file_name, filter, do_group_activities, processors)
let separator = sub_m.value_of("separator").unwrap();
bartib::controller::list::list(
file_name,
filter,
do_group_activities,
processors,
separator,
)
}
("report", Some(sub_m)) => {
let filter = create_filter_for_arguments(sub_m);
Expand All @@ -362,7 +406,8 @@ fn run_subcommand(matches: &ArgMatches, file_name: &str) -> Result<()> {
("last", Some(sub_m)) => {
let number = get_number_argument_or_ignore(sub_m.value_of("number"), "-n/--number")
.unwrap_or(10);
bartib::controller::list::list_last_activities(file_name, number)
let delim = sub_m.value_of("separator").unwrap_or("");
bartib::controller::list::list_last_activities(file_name, number, delim)
}
("edit", Some(sub_m)) => {
let optional_editor_command = sub_m.value_of("editor");
Expand All @@ -372,7 +417,8 @@ fn run_subcommand(matches: &ArgMatches, file_name: &str) -> Result<()> {
("sanity", Some(_)) => bartib::controller::list::sanity_check(file_name),
("search", Some(sub_m)) => {
let search_term = sub_m.value_of("search_term");
bartib::controller::list::search(file_name, search_term)
let separator = sub_m.value_of("separator").unwrap();
bartib::controller::list::search(file_name, search_term, separator)
}
("status", Some(sub_m)) => {
let filter = create_filter_for_arguments(sub_m);
Expand Down
141 changes: 80 additions & 61 deletions src/view/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ use crate::view::report;
use crate::view::table;

// displays a table with activities
pub fn list_activities(activities: &[&activity::Activity], with_start_dates: bool) {
pub fn list_activities(
activities: &[&activity::Activity],
with_start_dates: bool,
separator: &str,
) {
if activities.is_empty() {
println!("No activity to display");
return;
}

let mut activity_table = create_activity_table();
let mut activity_table = create_activity_table(separator);

activities
.iter()
Expand All @@ -26,13 +30,13 @@ pub fn list_activities(activities: &[&activity::Activity], with_start_dates: boo
}

// list activities grouped by the dates of their start time
pub fn list_activities_grouped_by_date(activities: &[&activity::Activity]) {
pub fn list_activities_grouped_by_date(activities: &[&activity::Activity], separator: &str) {
if activities.is_empty() {
println!("No activity to display");
return;
}

let mut activity_table = create_activity_table();
let mut activity_table = create_activity_table(separator);

group_activities_by_date(activities)
.iter()
Expand All @@ -44,29 +48,32 @@ pub fn list_activities_grouped_by_date(activities: &[&activity::Activity]) {
println!("\n{activity_table}");
}

fn create_activity_table() -> table::Table {
table::Table::new(vec![
table::Column {
label: "Started".to_string(),
wrap: table::Wrap::NoWrap,
},
table::Column {
label: "Stopped".to_string(),
wrap: table::Wrap::NoWrap,
},
table::Column {
label: "Description".to_string(),
wrap: table::Wrap::Wrap,
},
table::Column {
label: "Project".to_string(),
wrap: table::Wrap::Wrap,
},
table::Column {
label: "Duration".to_string(),
wrap: table::Wrap::NoWrap,
},
])
fn create_activity_table(separator: &str) -> table::Table {
table::Table::new(
vec![
table::Column {
label: "Started".to_string(),
wrap: table::Wrap::NoWrap,
},
table::Column {
label: "Stopped".to_string(),
wrap: table::Wrap::NoWrap,
},
table::Column {
label: "Description".to_string(),
wrap: table::Wrap::Wrap,
},
table::Column {
label: "Project".to_string(),
wrap: table::Wrap::Wrap,
},
table::Column {
label: "Duration".to_string(),
wrap: table::Wrap::NoWrap,
},
],
separator.to_string(),
)
}

fn create_activities_group(title: &str, activities: &[&activity::Activity]) -> table::Group {
Expand All @@ -88,28 +95,32 @@ fn create_activities_group(title: &str, activities: &[&activity::Activity]) -> t
}

// displays a table with running activities (no end time)
pub fn list_running_activities(activities: &[&activity::Activity]) {
pub fn list_running_activities(activities: &[&activity::Activity], separator: &str) {
if activities.is_empty() {
println!("No Activity is currently running");
} else {
let mut activity_table = table::Table::new(vec![
table::Column {
label: "Started At".to_string(),
wrap: table::Wrap::NoWrap,
},
table::Column {
label: "Description".to_string(),
wrap: table::Wrap::Wrap,
},
table::Column {
label: "Project".to_string(),
wrap: table::Wrap::Wrap,
},
table::Column {
label: "Duration".to_string(),
wrap: table::Wrap::NoWrap,
},
]);
let separator = separator.to_string();
let mut activity_table = table::Table::new(
vec![
table::Column {
label: "Started At".to_string(),
wrap: table::Wrap::NoWrap,
},
table::Column {
label: "Description".to_string(),
wrap: table::Wrap::Wrap,
},
table::Column {
label: "Project".to_string(),
wrap: table::Wrap::Wrap,
},
table::Column {
label: "Duration".to_string(),
wrap: table::Wrap::NoWrap,
},
],
separator,
);

activities
.iter()
Expand All @@ -128,7 +139,10 @@ pub fn list_running_activities(activities: &[&activity::Activity]) {
}

// display a list of projects and descriptions with generated index number
pub fn list_descriptions_and_projects(descriptions_and_projects: &[(&String, &String)]) {
pub fn list_descriptions_and_projects(
descriptions_and_projects: &[(&String, &String)],
separator: &str,
) {
list_descriptions_and_projects_with_index(
&descriptions_and_projects
.iter()
Expand All @@ -137,32 +151,37 @@ pub fn list_descriptions_and_projects(descriptions_and_projects: &[(&String, &St
.rev()
.collect::<Vec<_>>(),
"No activities have been tracked yet",
separator,
)
}

// display a list of projects ands descriptions with custom indexes
pub fn list_descriptions_and_projects_with_index(
descriptions_and_projects: &[(usize, &(&String, &String))],
zero_length_error: &str,
separator: &str,
) {
if descriptions_and_projects.is_empty() {
println!("{zero_length_error}");
return;
}
let mut descriptions_and_projects_table = table::Table::new(vec![
table::Column {
label: " # ".to_string(),
wrap: table::Wrap::NoWrap,
},
table::Column {
label: "Description".to_string(),
wrap: table::Wrap::Wrap,
},
table::Column {
label: "Project".to_string(),
wrap: table::Wrap::Wrap,
},
]);
let mut descriptions_and_projects_table = table::Table::new(
vec![
table::Column {
label: " # ".to_string(),
wrap: table::Wrap::NoWrap,
},
table::Column {
label: "Description".to_string(),
wrap: table::Wrap::Wrap,
},
table::Column {
label: "Project".to_string(),
wrap: table::Wrap::Wrap,
},
],
separator.to_string(),
);

for (index, (description, project)) in descriptions_and_projects {
descriptions_and_projects_table.add_row(table::Row::new(vec![
Expand Down
Loading