Lines
95.16 %
Functions
50 %
Branches
100 %
//! Deleting the marked transactions: one `delete-transaction` per id, as Emacs does.
use cli_core::eval::build_delete_transaction_form;
use cli_core::render::{RenderError, WireValue, parse_wire};
use rpc::ErrorCode;
use super::App;
use crate::modal::{ConfirmAction, Modal};
use crate::route::RouteCtx;
use crate::tabs::list::ListTab;
use crate::tabs::seq::Seq;
use crate::view::ViewId;
use crate::viewer::Source;
use crate::window::WindowId;
/// The bulk delete in flight.
#[derive(Default)]
pub(super) struct BulkDelete {
seq: Seq,
total: usize,
pending: usize,
/// The window the delete was confirmed in; its marks go as the deletes succeed.
origin: Option<WindowId>,
deleted: usize,
failures: Vec<String>,
/// Whether a delete of an abandoned bulk went through, so the list is stale.
orphan_deleted: bool,
}
impl BulkDelete {
fn start(&mut self, total: usize, origin: WindowId) -> u64 {
let mut seq = self.seq;
let id = seq.advance();
*self = Self {
seq,
total,
pending: total,
origin: Some(origin),
..Self::default()
};
id
fn summary(&self) -> String {
let mut text = format!("deleted {} of {} transactions", self.deleted, self.total);
if !self.failures.is_empty() {
text.push_str(&format!("; failed: {}", self.failures.join("; ")));
if self.pending > 0 {
text.push_str(&format!("; outcome unknown for {}", self.pending));
text
/// Gives up on the unanswered deletes; the partial outcome, or `None` when nothing ran.
pub(super) fn abandon(&mut self) -> Option<String> {
if self.pending == 0 {
return None;
let text = self.summary();
seq.advance();
orphan_deleted: self.orphan_deleted,
Some(text)
enum Outcome {
Deleted,
Gone,
Failed(String),
fn reply_outcome(wire: &str) -> Outcome {
match parse_wire(wire) {
Err(RenderError::Server(reply)) if reply.code == ErrorCode::NOT_FOUND => Outcome::Gone,
Err(e) => Outcome::Failed(e.to_string()),
Ok(WireValue::Value(value)) if value.is_nil() => Outcome::Failed("not deleted".into()),
Ok(_) => Outcome::Deleted,
impl App {
pub fn open_delete_marked(&mut self) {
let Some(window) = self.front_or_say(ViewId::Transactions) else {
return;
let ids = self
.win::<ListTab>(window)
.map(ListTab::marked_ids)
.unwrap_or_default();
if ids.is_empty() {
self.set_status("no marked transactions");
if self.bulk.pending > 0 {
self.set_status("a bulk delete is still running");
if self.dialog_free("delete of marked transactions") {
self.open_dialog_in(
window,
Modal::confirm(
format!("Delete {} marked transactions?", ids.len()),
ConfirmAction::DeleteMarked(ids),
),
);
pub(crate) fn submit_delete_marked(&mut self, ids: Vec<String>) {
if self.console_eval.is_none() {
self.set_status("console not connected");
let Some(window) = self.dialog_window() else {
let seq = self.bulk.start(ids.len(), window);
for id in ids {
let ctx = RouteCtx::BulkDelete {
id: id.clone(),
let form = build_delete_transaction_form(&id);
if self.dispatch_eval(window, ctx, form).is_none() {
self.record_bulk_reply(&id, Outcome::Failed("not sent".into()));
pub(super) fn deliver_bulk_delete_reply(&mut self, seq: u64, id: &str, wire: &str) {
if self.bulk.seq.awaits(seq) {
self.record_bulk_reply(id, reply_outcome(wire));
/// A reply of a bulk delete whose window closed: the list refreshes once the last one is
/// in, if any delete went through.
pub(super) fn settle_orphan_bulk(&mut self, wire: &str) {
self.bulk.orphan_deleted |= matches!(reply_outcome(wire), Outcome::Deleted);
let more = self
.pending_routes
.values()
.any(|route| matches!(route.ctx, RouteCtx::BulkDelete { .. }));
if !more && std::mem::take(&mut self.bulk.orphan_deleted) {
self.refresh_windows(ViewId::Transactions);
/// A deleted transaction leaves its list's marks and closes its open detail.
pub(super) fn forget_transaction(&mut self, window: Option<WindowId>, id: &str) {
let list = window.and_then(|window| self.win_mut::<ListTab>(window));
if let Some(list) = list {
list.unmark(id);
if let Some(detail) = self.viewer_of(&Source::Detail { id: id.to_string() }) {
self.close_window(detail);
fn record_bulk_reply(&mut self, id: &str, outcome: Outcome) {
match outcome {
Outcome::Deleted => {
self.forget_transaction(self.bulk.origin, id);
self.bulk.deleted += 1;
Outcome::Gone => {
self.bulk.failures.push(format!("{id}: not found"));
Outcome::Failed(reason) => self.bulk.failures.push(format!("{id}: {reason}")),
self.bulk.pending = self.bulk.pending.saturating_sub(1);
if self.bulk.pending == 0 {
let summary = self.bulk.summary();
self.set_status(summary);