patch-select: Add support for patch filter option

This commit is contained in:
Erik Kundt
2024-01-29 17:30:41 +01:00
parent 5e9e6140ed
commit 8909cb668f
9 changed files with 177 additions and 59 deletions
+1
View File
@@ -5,6 +5,7 @@ use anyhow::Result;
use radicle::cob::Label;
use radicle::prelude::Did;
pub mod format;
pub mod issue;
pub mod patch;
+7
View File
@@ -0,0 +1,7 @@
use radicle::identity::Did;
/// Format a DID.
pub fn did(did: &Did) -> String {
let nid = did.as_key().to_human();
format!("{}…{}", &nid[..7], &nid[nid.len() - 7..])
}
+74 -4
View File
@@ -1,8 +1,13 @@
use std::fmt::Display;
use anyhow::Result;
use radicle::cob::patch::{Patch, PatchId, Patches};
use radicle::identity::Did;
use radicle::storage::git::Repository;
use radicle::{patch, Profile};
use super::format;
#[derive(Clone, Default, Debug, Eq, PartialEq)]
pub enum State {
@@ -13,6 +18,18 @@ pub enum State {
Archived,
}
impl Display for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let state = match self {
State::Draft => "draft",
State::Open => "open",
State::Merged => "merged",
State::Archived => "archived",
};
f.write_str(state)
}
}
#[derive(Clone, Default, Debug, Eq, PartialEq)]
pub struct Filter {
state: Option<State>,
@@ -21,8 +38,8 @@ pub struct Filter {
}
impl Filter {
pub fn with_state(mut self, state: State) -> Self {
self.state = Some(state);
pub fn with_state(mut self, state: Option<State>) -> Self {
self.state = state;
self
}
@@ -36,8 +53,61 @@ impl Filter {
self
}
pub fn matches(&self, _patch: &Patch) -> bool {
true
pub fn matches(&self, profile: &Profile, patch: &Patch) -> bool {
let matches_state = match self.state {
Some(State::Draft) => matches!(patch.state(), patch::State::Draft),
Some(State::Open) => matches!(patch.state(), patch::State::Open { .. }),
Some(State::Merged) => matches!(patch.state(), patch::State::Merged { .. }),
Some(State::Archived) => matches!(patch.state(), patch::State::Archived),
None => true,
};
let matches_authored = self
.authored
.then(|| *patch.author().id() == profile.did())
.unwrap_or(true);
let matches_authors = (!self.authors.is_empty())
.then(|| {
self.authors
.iter()
.any(|other| *patch.author().id() == *other)
})
.unwrap_or(true);
matches_state && matches_authored && matches_authors
}
}
impl ToString for Filter {
fn to_string(&self) -> String {
let mut filter = String::new();
filter.push(' ');
if let Some(state) = &self.state {
filter.push_str(&format!("is:{}", state));
filter.push(' ');
}
if self.authored {
filter.push_str("is:authored");
filter.push(' ');
}
if !self.authors.is_empty() {
filter.push_str("authors:");
filter.push('[');
let mut authors = self.authors.iter().peekable();
while let Some(author) = authors.next() {
filter.push_str(&format::did(author));
if authors.peek().is_some() {
filter.push(',');
}
}
filter.push(']');
}
filter
}
}
+1 -1
View File
@@ -6,7 +6,7 @@ use radicle::cob::issue::{Issue, IssueId};
use radicle::cob::patch::{Patch, PatchId};
use radicle::crypto::ssh::keystore::{Keystore, MemorySigner};
use radicle::crypto::Signer;
use radicle::prelude::{Id, Project};
use radicle::identity::{Id, Project};
use radicle::profile::env::RAD_PASSPHRASE;
use radicle::storage::git::Repository;
use radicle::storage::{ReadRepository, ReadStorage};
+10 -1
View File
@@ -66,6 +66,14 @@ pub mod style {
Style::default().fg(Color::Yellow)
}
pub fn yellow_dim() -> Style {
yellow().add_modifier(TextModifiers::DIM)
}
pub fn yellow_dim_reversed() -> Style {
yellow_dim().add_modifier(TextModifiers::REVERSED)
}
pub fn blue() -> Style {
Style::default().fg(Color::Blue)
}
@@ -108,7 +116,8 @@ pub mod style {
pub fn default_reversed() -> Style {
Style::default()
.add_modifier(TextModifiers::DIM)
.fg(Color::DarkGray)
// .add_modifier(TextModifiers::DIM)
.add_modifier(TextModifiers::REVERSED)
}