Skip to content

Commit

Permalink
Updated the remaining column displays to use new trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
oubiwann committed Dec 20, 2023
1 parent 33f9a58 commit 39dd16c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 138 deletions.
8 changes: 5 additions & 3 deletions rucksack/src/command/handlers/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,15 @@ pub fn passwords(matches: &ArgMatches, app: &App) -> Result<()> {

fn process_records(matches: &ArgMatches, app: &App, mut opts: Opts) -> Result<()> {
let sort_by = matches.get_one::<String>("sort-by").map(|s| s.as_str());
let reveal = matches.get_one::<bool>("reveal").unwrap();
opts.category = app.inputs.category(Flag::Many);
opts.all_tags = options::all_tags(matches);
opts.any_tags = options::any_tags(matches);
opts.kind = options::record_kind(matches);
opts.reveal = *reveal;
opts.decrypted = *matches.get_one::<bool>("decrypt").unwrap();
opts.reveal = options::reveal(matches);
opts.decrypted = options::decrypt(matches);
if opts.reveal && opts.decrypted {
opts.with_passwd = true;
}
match matches.get_one::<String>("group-by").map(|s| s.as_str()) {
Some("name") => opts.group_by_name = true,
Some("password") => opts.group_by_password = true,
Expand Down
4 changes: 4 additions & 0 deletions rucksack/src/input/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ pub fn db_pwd(matches: &ArgMatches) -> Secret<String> {
}
}

pub fn decrypt(matches: &ArgMatches) -> bool {
*matches.get_one::<bool>("decrypt").unwrap_or(&false)
}

pub fn latest(matches: &ArgMatches) -> bool {
*matches.get_one::<bool>("latest").unwrap_or(&false)
}
Expand Down
92 changes: 91 additions & 1 deletion rucksack/src/output/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ pub trait Columns {

fn passwd(&self, opts: &Opts, mut cols: Vec<Column>) -> Vec<Column> {
if opts.with_passwd {
cols.append(&mut vec![Column::Password, Column::Score])
cols.push(Column::Password);
if opts.reveal && opts.decrypted {
cols.push(Column::Score);
}
}
cols
}
Expand Down Expand Up @@ -124,6 +127,93 @@ impl Columns for ColsBackupFiles {
}
}

pub struct ColsGroupByName;

impl Columns for ColsGroupByName {
fn post(&self, _opts: &Opts, mut cols: Vec<Column>) -> Vec<Column> {
cols.append(&mut vec![Column::Count, Column::Url]);
cols
}
}

pub struct ColsGroupByHash;

impl Columns for ColsGroupByHash {
fn pre(&self, _opts: &Opts) -> Vec<Column> {
vec![Column::Name, Column::Kind, Column::Category]
}

fn post(&self, _opts: &Opts, mut cols: Vec<Column>) -> Vec<Column> {
cols.append(&mut vec![Column::Count, Column::LastUpdated, Column::Url]);
cols
}
}

pub struct ColsGroupByPasswd;

impl Columns for ColsGroupByPasswd {
fn pre(&self, _opts: &Opts) -> Vec<Column> {
vec![Column::Name, Column::Kind, Column::Category]
}

fn post(&self, _opts: &Opts, mut cols: Vec<Column>) -> Vec<Column> {
cols.append(&mut vec![Column::Count, Column::Url]);
cols
}
}

pub struct ColsGroupByKind;

impl Columns for ColsGroupByKind {
fn pre(&self, _opts: &Opts) -> Vec<Column> {
vec![Column::Name, Column::Category]
}

fn post(&self, _opts: &Opts, mut cols: Vec<Column>) -> Vec<Column> {
cols.append(&mut vec![Column::Count, Column::Url]);
cols
}
}

pub struct ColsGroupByCat;

impl Columns for ColsGroupByCat {
fn pre(&self, _opts: &Opts) -> Vec<Column> {
vec![Column::Name, Column::Kind]
}

fn post(&self, _opts: &Opts, mut cols: Vec<Column>) -> Vec<Column> {
cols.append(&mut vec![Column::Count, Column::Url]);
cols
}
}

pub struct ColsDefault;

impl Columns for ColsDefault {
fn pre(&self, _opts: &Opts) -> Vec<Column> {
vec![Column::Name, Column::Kind, Column::Category]
}

fn post(&self, _opts: &Opts, mut cols: Vec<Column>) -> Vec<Column> {
cols.append(&mut vec![Column::Count, Column::Url]);
cols
}
}

pub struct ColsPasswdHist;

impl Columns for ColsPasswdHist {
fn post(&self, _opts: &Opts, mut cols: Vec<Column>) -> Vec<Column> {
cols.append(&mut vec![
Column::Created,
Column::LastUpdated,
Column::LastAccessed,
]);
cols
}
}

#[cfg(test)]
mod tests {
use super::Column;
Expand Down
143 changes: 9 additions & 134 deletions rucksack/src/output/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,146 +53,21 @@ impl Table {
} else if self.opts.backup_files {
self.columns = column::ColsBackupFiles {}.new(&self.opts);
} else if self.opts.group_by_name {
if self.opts.with_status {
self.columns = vec![
Column::Password,
Column::Score,
Column::Status,
Column::Count,
Column::Url,
];
} else {
self.columns = vec![Column::Password, Column::Score, Column::Count, Column::Url];
}
self.opts.with_passwd = true;
self.columns = column::ColsGroupByName {}.new(&self.opts);
} else if self.opts.group_by_hash {
if self.opts.with_status {
self.columns = vec![
Column::Name,
Column::Kind,
Column::Category,
Column::Status,
Column::Count,
Column::LastUpdated,
Column::Url,
];
} else {
self.columns = vec![
Column::Name,
Column::Kind,
Column::Category,
Column::Count,
Column::LastUpdated,
Column::Url,
];
}
self.columns = column::ColsGroupByHash {}.new(&self.opts);
} else if self.opts.group_by_password {
if self.opts.with_status {
self.columns = vec![
Column::Name,
Column::Kind,
Column::Category,
Column::Status,
Column::Count,
Column::Url,
];
} else {
self.columns = vec![
Column::Name,
Column::Kind,
Column::Category,
Column::Count,
Column::Url,
];
}
self.columns = column::ColsGroupByPasswd {}.new(&self.opts);
} else if self.opts.group_by_kind {
if self.opts.with_status {
self.columns = vec![
Column::Name,
Column::Category,
Column::Password,
Column::Score,
Column::Status,
Column::Count,
Column::Url,
];
} else {
self.columns = vec![
Column::Name,
Column::Category,
Column::Password,
Column::Score,
Column::Count,
Column::Url,
];
}
self.columns = column::ColsGroupByKind {}.new(&self.opts);
} else if self.opts.group_by_category {
if self.opts.with_status {
self.columns = vec![
Column::Name,
Column::Kind,
Column::Password,
Column::Score,
Column::Status,
Column::Count,
Column::Url,
];
} else {
self.columns = vec![
Column::Name,
Column::Kind,
Column::Password,
Column::Score,
Column::Count,
Column::Url,
];
}
self.columns = column::ColsGroupByCat {}.new(&self.opts);
} else if self.opts.password_history {
self.columns = vec![
Column::Password,
Column::Created,
Column::LastUpdated,
Column::LastAccessed,
];
} else if self.opts.decrypted {
if self.opts.with_status {
self.columns = vec![
Column::Name,
Column::Kind,
Column::Category,
Column::Password,
Column::Score,
Column::Status,
Column::Count,
Column::Url,
];
} else {
self.columns = vec![
Column::Name,
Column::Kind,
Column::Category,
Column::Password,
Column::Score,
Column::Count,
Column::Url,
];
}
} else if self.opts.with_status {
self.columns = vec![
Column::Name,
Column::Kind,
Column::Category,
Column::Status,
Column::Count,
Column::Url,
];
self.opts.with_passwd = true;
self.columns = column::ColsPasswdHist {}.new(&self.opts);
} else {
self.columns = vec![
Column::Name,
Column::Kind,
Column::Category,
Column::Count,
Column::Url,
];
self.columns = column::ColsDefault {}.new(&self.opts);
}
self.set_headers();
}
Expand Down

0 comments on commit 39dd16c

Please sign in to comment.