feat: impl delete

This commit is contained in:
Kristofers Solo 2024-02-22 11:16:26 +02:00
parent eb43995718
commit ad2cccb908
3 changed files with 54 additions and 47 deletions

View File

@ -1,6 +1,7 @@
use std::{collections::HashSet, path::Path, vec};
use std::{collections::HashSet, path::Path};
use transmission_rpc::types::{Id, Torrent, TorrentAction, TorrentStatus};
use tracing::error;
use transmission_rpc::types::{Torrent, TorrentAction, TorrentStatus};
use super::{types::Selected, Torrents};
@ -20,77 +21,81 @@ impl Torrents {
_ => TorrentAction::Stop,
};
if let Some(id) = torrent.id() {
self.client.torrent_action(action, vec![id]).await.unwrap();
if let Err(e) = self.client.torrent_action(action, vec![id]).await {
error!("{:?}", e);
}
}
}
}
pub async fn toggle_all(&mut self) {
let torrents: Vec<(Id, TorrentAction)> = self
let torrents: Vec<_> = self
.torrents
.iter()
.map(|torrent| {
.filter_map(|torrent| {
torrent.id().map(|id| {
(
torrent.id().unwrap(),
id,
match torrent.status {
Some(TorrentStatus::Stopped) => TorrentAction::StartNow,
_ => TorrentAction::Stop,
},
)
})
})
.collect();
for (id, action) in torrents {
self.client
.torrent_action(action, vec![id])
.await
.expect("Error toggling torrent");
if let Err(e) = self.client.torrent_action(action, vec![id]).await {
error!("{:?}", e);
}
}
}
pub async fn start_all(&mut self) {
self.action_all(TorrentAction::StartNow)
.await
.expect("Error starting all torrents");
if let Err(e) = self.action_all(TorrentAction::StartNow).await {
error!("{:?}", e);
}
}
pub async fn stop_all(&mut self) {
self.action_all(TorrentAction::Stop)
.await
.expect("Error stopping all torrents");
if let Err(e) = self.action_all(TorrentAction::Stop).await {
error!("{:?}", e);
}
}
pub async fn move_dir(
&mut self,
torrent: &Torrent,
location: &Path,
move_from: Option<bool>,
) -> transmission_rpc::types::Result<()> {
let id = torrent.id().expect("ID not found");
self.client
pub async fn move_dir(&mut self, torrent: &Torrent, location: &Path, move_from: Option<bool>) {
if let Some(id) = torrent.id() {
if let Err(e) = self
.client
.torrent_set_location(vec![id], location.to_string_lossy().into(), move_from)
.await?;
Ok(())
.await
{
error!("{:?}", e);
}
}
}
pub async fn delete(&mut self, ids: Selected, delete_local_data: bool) {
self.client
if let Err(e) = self
.client
.torrent_remove(ids.into(), delete_local_data)
.await
.unwrap();
{
error!("{:?}", e);
}
}
pub async fn rename(
&mut self,
torrent: &Torrent,
name: &Path,
) -> transmission_rpc::types::Result<()> {
let id = torrent.id().expect("ID not found");
let old_name = torrent.name.clone().unwrap();
self.client
pub async fn rename(&mut self, torrent: &Torrent, name: &Path) {
if let (Some(id), Some(old_name)) = (torrent.id(), torrent.name.clone()) {
if let Err(e) = self
.client
.torrent_rename_path(vec![id], old_name, name.to_string_lossy().into())
.await?;
Ok(())
.await
{
error!("{:?}", e);
}
}
}
async fn action_all(&mut self, action: TorrentAction) -> transmission_rpc::types::Result<()> {

View File

@ -13,7 +13,7 @@ pub struct Torrents {
/// Constructs a new instance of [`Torrents`].
pub client: TransClient,
pub torrents: Vec<Torrent>,
pub selected: HashSet<i64>, // Torrent::id
pub selected: HashSet<i64>,
pub fields: Option<Vec<TorrentGetField>>,
}

View File

@ -21,6 +21,8 @@ pub fn get_action(key_event: KeyEvent) -> Option<Action> {
KeyCode::Char('3') => Some(Action::SwitchTab(2)),
KeyCode::Char('t') | KeyCode::Enter | KeyCode::Menu => Some(Action::ToggleTorrent),
KeyCode::Char('a') => Some(Action::ToggleAll),
KeyCode::Char('d') => Some(Action::Delete(false)),
KeyCode::Char('D') => Some(Action::Delete(true)),
KeyCode::Char(' ') => Some(Action::Select),
// Other handlers you could add here.
_ => None,