Skip to content

Commit

Permalink
pausing torrents
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldemian committed Aug 23, 2023
1 parent d703e2a commit 803a3b0
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 309 deletions.
415 changes: 174 additions & 241 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions src/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,14 @@ impl Disk {
let mut infos: VecDeque<BlockInfo> = VecDeque::new();
let mut idxs = VecDeque::new();

let available = self
.block_infos
.get(&info_hash)
.ok_or(Error::TorrentDoesNotExist)?
.len();

let block_infos = self
.block_infos
.get_mut(&info_hash)
.ok_or(Error::TorrentDoesNotExist)?;

info!("available blocks {}", block_infos.len());
let available = block_infos.len();

info!("disk: available blocks {available}");

// prevent from requesting more blocks than what is available
if available < qnt {
Expand Down
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io;

use futures::Sink;

Check failure on line 3 in src/error.rs

View workflow job for this annotation

GitHub Actions / Lints

unused import: `futures::Sink`

Check warning on line 3 in src/error.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `futures::Sink`

Check warning on line 3 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable)

unused import: `futures::Sink`

Check warning on line 3 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable)

unused import: `futures::Sink`

Check warning on line 3 in src/error.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable)

unused import: `futures::Sink`
use thiserror::Error;
use tokio::sync::{mpsc, oneshot};

Expand Down
23 changes: 15 additions & 8 deletions src/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{
cli::Args,
config::Config,
disk::DiskMsg,
error::Error,
torrent::{Stats, Torrent, TorrentMsg, TorrentStatus},
};

Expand All @@ -38,6 +39,7 @@ pub struct AppStyle {
pub highlight_fg: Style,
pub success: Style,
pub error: Style,
pub warning: Style,
}

impl Default for AppStyle {
Expand All @@ -54,15 +56,17 @@ impl AppStyle {
highlight_fg: Style::default().fg(Color::LightBlue),
success: Style::default().fg(Color::LightGreen),
error: Style::default().fg(Color::Red),
warning: Style::default().fg(Color::Yellow),
}
}
}

#[derive(Debug, Clone)]
pub enum FrMsg {
Quit,
Draw([u8; 20], TorrentInfo),
NewTorrent(String),
Draw([u8; 20], TorrentInfo),
TogglePause([u8; 20]),
Quit,
}

#[derive(Debug, Clone, Default)]
Expand All @@ -74,6 +78,7 @@ pub struct TorrentInfo {
pub download_rate: u64,
pub uploaded: u64,
pub size: u64,
pub info_hash: [u8; 20],
}

pub struct Frontend<'a> {
Expand Down Expand Up @@ -112,7 +117,7 @@ impl<'a> Frontend<'a> {
}

/// Run the UI event loop
pub async fn run(&mut self, mut fr_rx: mpsc::Receiver<FrMsg>) -> Result<(), std::io::Error> {
pub async fn run(&mut self, mut fr_rx: mpsc::Receiver<FrMsg>) -> Result<(), Error> {
let mut reader = EventStream::new();

// setup terminal
Expand Down Expand Up @@ -150,8 +155,6 @@ impl<'a> Frontend<'a> {
return Ok(());
},
FrMsg::Draw(info_hash, torrent_info) => {
info!("draw {torrent_info:#?}");

self.torrent_list
.torrent_infos
.insert(info_hash, torrent_info);
Expand All @@ -161,6 +164,10 @@ impl<'a> Frontend<'a> {
FrMsg::NewTorrent(magnet) => {
self.new_torrent(&magnet).await;
}
FrMsg::TogglePause(id) => {
let tx = self.torrent_txs.get(&id).ok_or(Error::TorrentDoesNotExist)?;
tx.send(TorrentMsg::TogglePause).await?;
}
}
}
}
Expand Down Expand Up @@ -204,9 +211,9 @@ impl<'a> Frontend<'a> {
.torrent_infos
.insert(info_hash, torrent_info_l);

if self.torrent_list.state.selected().is_none() {
self.torrent_list.state.select(Some(0));
}
// if self.torrent_list.state.selected().is_none() {
// self.torrent_list.state.select(Some(0));
// }

let args = Args::parse();
let mut listen = self.config.listen;
Expand Down
24 changes: 21 additions & 3 deletions src/frontend/torrent_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ratatui::{
widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph},
Terminal,
};
use tracing::info;

use crate::{to_human_readable, torrent::TorrentStatus};

Expand All @@ -20,6 +21,7 @@ pub struct TorrentList<'a> {
pub style: AppStyle,
pub state: ListState,
pub torrent_infos: HashMap<[u8; 20], TorrentInfo>,
active_torrent: Option<[u8; 20]>,
ctx: Arc<FrontendCtx>,
show_popup: bool,
input: String,
Expand All @@ -39,6 +41,8 @@ impl<'a> TorrentList<'a> {
" move down ".into(),
Span::styled("t".to_string(), style.highlight_fg),
" add torrent ".into(),
Span::styled("p".to_string(), style.highlight_fg),
" toggle pause/resume ".into(),
Span::styled("q".to_string(), style.highlight_fg),
" quit".into(),
]
Expand All @@ -51,6 +55,7 @@ impl<'a> TorrentList<'a> {
.block(Block::default().borders(Borders::ALL).title("Keybindings"));

Self {
active_torrent: None,
style,
footer,
cursor_position: 0,
Expand Down Expand Up @@ -108,6 +113,17 @@ impl<'a> TorrentList<'a> {
self.show_popup = true;
self.draw(terminal).await;
}
KeyCode::Char('p') => {
info!("sending pause with id {:?}", self.active_torrent);
if let Some(active_torrent) = self.active_torrent {
let _ = self
.ctx
.fr_tx
.send(FrMsg::TogglePause(active_torrent))
.await;
self.draw(terminal).await;
}
}
_ => {}
},
}
Expand All @@ -126,6 +142,7 @@ impl<'a> TorrentList<'a> {
let status_style = match ctx.status {
TorrentStatus::Seeding => self.style.success,
TorrentStatus::Error => self.style.error,
TorrentStatus::Paused => self.style.warning,
_ => self.style.highlight_fg,
};

Expand Down Expand Up @@ -162,12 +179,13 @@ impl<'a> TorrentList<'a> {
line_bottom,
];

if Some(i) == selected {
self.active_torrent = Some(ctx.info_hash);
}

if Some(i) != selected && selected > Some(0) {
items.remove(0);
}
if Some(i) != selected && selected == Some(0) {
items.pop();
}

rows.push(ListItem::new(items));
}
Expand Down
Loading

0 comments on commit 803a3b0

Please sign in to comment.