mirror of
https://github.com/kristoferssolo/traxor.git
synced 2026-02-04 06:42:04 +00:00
Initial commit
It's Alive First somewhat working version of `traxor` Created tabs Refactored data storing something works Initial commit
This commit is contained in:
98
src/app/mod.rs
Normal file
98
src/app/mod.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
mod tab;
|
||||
mod torrent;
|
||||
|
||||
use ratatui::widgets::TableState;
|
||||
|
||||
pub use self::{tab::Tab, torrent::Torrents};
|
||||
|
||||
/// Main Application.
|
||||
/// TODO: write description
|
||||
#[derive(Debug)]
|
||||
pub struct App<'a> {
|
||||
pub running: bool,
|
||||
index: usize,
|
||||
tabs: &'a [Tab],
|
||||
pub state: TableState,
|
||||
pub torrents: Torrents,
|
||||
}
|
||||
|
||||
impl<'a> App<'a> {
|
||||
/// Constructs a new instance of [`App`].
|
||||
/// Returns instance of `Self`.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
running: true,
|
||||
tabs: &[Tab::All, Tab::Active, Tab::Downloading, Tab::Settings],
|
||||
index: 0,
|
||||
state: TableState::default(),
|
||||
torrents: Torrents::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles the tick event of the terminal.
|
||||
pub async fn tick(&mut self) {
|
||||
self.torrents.update().await;
|
||||
}
|
||||
|
||||
/// Set running to false to quit the application.
|
||||
pub fn quit(&mut self) {
|
||||
self.running = false;
|
||||
}
|
||||
|
||||
pub fn next(&mut self) {
|
||||
let i = match self.state.selected() {
|
||||
Some(i) => {
|
||||
if i >= self.torrents.len() - 1 {
|
||||
0
|
||||
} else {
|
||||
i + 1
|
||||
}
|
||||
}
|
||||
None => 0,
|
||||
};
|
||||
self.state.select(Some(i));
|
||||
}
|
||||
|
||||
pub fn previous(&mut self) {
|
||||
let i = match self.state.selected() {
|
||||
Some(i) => {
|
||||
if i == 0 {
|
||||
self.torrents.len() - 1
|
||||
} else {
|
||||
i - 1
|
||||
}
|
||||
}
|
||||
None => 0,
|
||||
};
|
||||
self.state.select(Some(i));
|
||||
}
|
||||
|
||||
/// Switches to the next tab.
|
||||
pub fn next_tab(&mut self) {
|
||||
self.index = (self.index + 1) % self.tabs.len();
|
||||
}
|
||||
|
||||
/// Switches to the previous tab.
|
||||
pub fn prev_tab(&mut self) {
|
||||
if self.index > 0 {
|
||||
self.index -= 1;
|
||||
} else {
|
||||
self.index = self.tabs.len() - 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Switches to the tab whose index is `idx`.
|
||||
pub fn switch_tab(&mut self, idx: usize) {
|
||||
self.index = idx
|
||||
}
|
||||
|
||||
/// Returns current active [`Tab`] number
|
||||
pub fn index(&self) -> usize {
|
||||
self.index
|
||||
}
|
||||
|
||||
/// Returns [`Tab`] slice
|
||||
pub fn tabs(&self) -> &[Tab] {
|
||||
self.tabs
|
||||
}
|
||||
}
|
||||
62
src/app/tab.rs
Normal file
62
src/app/tab.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use transmission_rpc::types::TorrentGetField;
|
||||
|
||||
/// Available tabs.
|
||||
/// TODO: write description
|
||||
#[derive(Debug, Default)]
|
||||
pub enum Tab {
|
||||
#[default]
|
||||
All,
|
||||
Active,
|
||||
Downloading,
|
||||
Settings,
|
||||
}
|
||||
|
||||
impl Tab {
|
||||
/// Returns slice [`TorrentGetField`] apropriate variants.
|
||||
pub fn fields(&self) -> &[TorrentGetField] {
|
||||
match self {
|
||||
Tab::All => &[
|
||||
TorrentGetField::Status,
|
||||
TorrentGetField::PeersGettingFromUs,
|
||||
TorrentGetField::UploadRatio,
|
||||
TorrentGetField::TotalSize,
|
||||
TorrentGetField::UploadedEver,
|
||||
TorrentGetField::DownloadDir,
|
||||
TorrentGetField::Name,
|
||||
],
|
||||
Tab::Active => &[
|
||||
TorrentGetField::TotalSize,
|
||||
TorrentGetField::UploadedEver,
|
||||
TorrentGetField::UploadRatio,
|
||||
TorrentGetField::PeersGettingFromUs,
|
||||
TorrentGetField::PeersSendingToUs,
|
||||
TorrentGetField::Status,
|
||||
TorrentGetField::Eta,
|
||||
TorrentGetField::PercentDone,
|
||||
TorrentGetField::RateDownload,
|
||||
TorrentGetField::RateUpload,
|
||||
TorrentGetField::Name,
|
||||
],
|
||||
Tab::Downloading => &[
|
||||
TorrentGetField::TotalSize,
|
||||
TorrentGetField::PercentDone,
|
||||
TorrentGetField::RateDownload,
|
||||
TorrentGetField::Eta,
|
||||
TorrentGetField::DownloadDir,
|
||||
TorrentGetField::Name,
|
||||
],
|
||||
Tab::Settings => &[],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for Tab {
|
||||
fn to_string(&self) -> String {
|
||||
match *self {
|
||||
Tab::All => String::from("All"),
|
||||
Tab::Active => String::from("Active"),
|
||||
Tab::Downloading => String::from("Downloading"),
|
||||
Tab::Settings => String::from("Settings"),
|
||||
}
|
||||
}
|
||||
}
|
||||
80
src/app/torrent.rs
Normal file
80
src/app/torrent.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use anyhow::Result;
|
||||
use transmission_rpc::{
|
||||
types::{Id, Torrent, TorrentGetField},
|
||||
TransClient,
|
||||
};
|
||||
|
||||
use url::Url;
|
||||
|
||||
/// List of torrents.
|
||||
pub struct Torrents {
|
||||
/// Constructs a new instance of [`Torrents`].
|
||||
client: TransClient,
|
||||
torrents: Vec<Torrent>,
|
||||
ids: Option<Vec<Id>>,
|
||||
fields: Option<Vec<TorrentGetField>>,
|
||||
}
|
||||
|
||||
impl Torrents {
|
||||
/// Constructs a new instance of [`Torrents`].
|
||||
pub fn new() -> Torrents {
|
||||
let url = Url::parse("http://localhost:9091/transmission/rpc").unwrap();
|
||||
Self {
|
||||
client: TransClient::new(url),
|
||||
torrents: Vec::new(),
|
||||
ids: None,
|
||||
fields: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the number of [`Torrent`]s in [`Torrents`]
|
||||
pub fn len(&self) -> usize {
|
||||
self.torrents.len()
|
||||
}
|
||||
|
||||
/// Sets `self.fields`
|
||||
pub fn set_fields(&mut self, fields: Option<Vec<TorrentGetField>>) -> &mut Self {
|
||||
self.fields = fields;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets
|
||||
pub fn url(&mut self, url: &str) -> Result<&mut Self> {
|
||||
self.client = TransClient::new(Url::parse(url)?);
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Updates [`Torrent`] values.
|
||||
pub async fn update(&mut self) -> &mut Self {
|
||||
self.torrents = self
|
||||
.client
|
||||
.torrent_get(self.fields.clone(), self.ids.clone())
|
||||
.await
|
||||
.unwrap()
|
||||
.arguments
|
||||
.torrents;
|
||||
self
|
||||
}
|
||||
|
||||
/// Returns [`Vec`] of [`Torrent`] as reference.
|
||||
pub fn torrents(&self) -> &Vec<Torrent> {
|
||||
&self.torrents
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Torrents {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let fields: Vec<String> = match &self.fields {
|
||||
Some(fields) => fields.iter().map(|field| field.to_str()).collect(),
|
||||
None => vec![String::from("None")],
|
||||
};
|
||||
write!(
|
||||
f,
|
||||
"fields:
|
||||
{:?};\n\ntorrents: {:?}",
|
||||
fields, self.torrents
|
||||
)
|
||||
}
|
||||
}
|
||||
78
src/event.rs
Normal file
78
src/event.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use anyhow::Result;
|
||||
use crossterm::event::{self, Event as CrosstermEvent, KeyEvent, MouseEvent};
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
/// Terminal events.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum Event {
|
||||
/// Terminal tick.
|
||||
Tick,
|
||||
/// Key press.
|
||||
Key(KeyEvent),
|
||||
/// Mouse click/scroll.
|
||||
Mouse(MouseEvent),
|
||||
/// Terminal resize.
|
||||
Resize(u16, u16),
|
||||
}
|
||||
|
||||
/// Terminal event handler.
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
/// TODO: write description
|
||||
pub struct EventHandler {
|
||||
/// Event sender channel.
|
||||
sender: mpsc::Sender<Event>,
|
||||
/// Event receiver channel.
|
||||
receiver: mpsc::Receiver<Event>,
|
||||
/// Event handler thread.
|
||||
handler: thread::JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl EventHandler {
|
||||
/// Constructs a new instance of [`EventHandler`].
|
||||
pub fn new(tick_rate: u64) -> Self {
|
||||
let tick_rate = Duration::from_millis(tick_rate);
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
let handler = {
|
||||
let sender = sender.clone();
|
||||
thread::spawn(move || {
|
||||
let mut last_tick = Instant::now();
|
||||
loop {
|
||||
let timeout = tick_rate
|
||||
.checked_sub(last_tick.elapsed())
|
||||
.unwrap_or(tick_rate);
|
||||
|
||||
if event::poll(timeout).expect("no events available") {
|
||||
match event::read().expect("unable to read event") {
|
||||
CrosstermEvent::Key(e) => sender.send(Event::Key(e)),
|
||||
CrosstermEvent::Mouse(e) => sender.send(Event::Mouse(e)),
|
||||
CrosstermEvent::Resize(w, h) => sender.send(Event::Resize(w, h)),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
.expect("failed to send terminal event")
|
||||
}
|
||||
|
||||
if last_tick.elapsed() >= tick_rate {
|
||||
sender.send(Event::Tick).expect("failed to send tick event");
|
||||
last_tick = Instant::now();
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
Self {
|
||||
sender,
|
||||
receiver,
|
||||
handler,
|
||||
}
|
||||
}
|
||||
|
||||
/// Receive the next event from the handler thread.
|
||||
///
|
||||
/// This function will always block the current thread if
|
||||
/// there is no data available and it's possible for more data to be sent.
|
||||
pub fn next(&self) -> Result<Event> {
|
||||
Ok(self.receiver.recv()?)
|
||||
}
|
||||
}
|
||||
35
src/handler.rs
Normal file
35
src/handler.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use crate::app::App;
|
||||
use anyhow::Result;
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||
|
||||
/// Handles the key events and updates the state of [`App`].
|
||||
pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> Result<()> {
|
||||
match key_event.code {
|
||||
// Exit application on `ESC` or `q`
|
||||
KeyCode::Esc | KeyCode::Char('q') => {
|
||||
app.quit();
|
||||
}
|
||||
// Exit application on `Ctrl-C`
|
||||
KeyCode::Char('c') | KeyCode::Char('C') => {
|
||||
if key_event.modifiers == KeyModifiers::CONTROL {
|
||||
app.quit();
|
||||
}
|
||||
}
|
||||
KeyCode::Char('l') | KeyCode::Right => {
|
||||
app.next_tab();
|
||||
}
|
||||
KeyCode::Char('h') | KeyCode::Left => {
|
||||
app.prev_tab();
|
||||
}
|
||||
|
||||
KeyCode::Char('j') | KeyCode::Down => app.next(),
|
||||
KeyCode::Char('k') | KeyCode::Up => app.previous(),
|
||||
KeyCode::Char('1') => app.switch_tab(0),
|
||||
KeyCode::Char('2') => app.switch_tab(1),
|
||||
KeyCode::Char('3') => app.switch_tab(2),
|
||||
KeyCode::Char('4') => app.switch_tab(3),
|
||||
// Other handlers you could add here.
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
14
src/lib.rs
Normal file
14
src/lib.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
/// Application.
|
||||
pub mod app;
|
||||
|
||||
/// Terminal events handler.
|
||||
pub mod event;
|
||||
|
||||
/// Widget renderer.
|
||||
pub mod ui;
|
||||
|
||||
/// Terminal user interface.
|
||||
pub mod tui;
|
||||
|
||||
/// Event handler.
|
||||
pub mod handler;
|
||||
38
src/main.rs
Normal file
38
src/main.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use anyhow::Result;
|
||||
use ratatui::backend::CrosstermBackend;
|
||||
use ratatui::Terminal;
|
||||
use std::io;
|
||||
use traxor::app::App;
|
||||
use traxor::event::{Event, EventHandler};
|
||||
use traxor::handler::handle_key_events;
|
||||
use traxor::tui::Tui;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
// Create an application.
|
||||
let mut app = App::new();
|
||||
|
||||
// Initialize the terminal user interface.
|
||||
let backend = CrosstermBackend::new(io::stderr());
|
||||
let terminal = Terminal::new(backend)?;
|
||||
let events = EventHandler::new(250); // Update time in ms
|
||||
let mut tui = Tui::new(terminal, events);
|
||||
tui.init()?;
|
||||
|
||||
// Start the main loop.
|
||||
while app.running {
|
||||
// Render the user interface.
|
||||
tui.draw(&mut app)?;
|
||||
// Handle events.
|
||||
match tui.events.next()? {
|
||||
Event::Tick => app.tick().await,
|
||||
Event::Key(key_event) => handle_key_events(key_event, &mut app)?,
|
||||
Event::Mouse(_) => {}
|
||||
Event::Resize(_, _) => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Exit the user interface.
|
||||
tui.exit()?;
|
||||
Ok(())
|
||||
}
|
||||
77
src/tui.rs
Normal file
77
src/tui.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
use crate::app::App;
|
||||
use crate::event::EventHandler;
|
||||
use crate::ui;
|
||||
use anyhow::Result;
|
||||
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
|
||||
use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
|
||||
use ratatui::backend::Backend;
|
||||
use ratatui::Terminal;
|
||||
use std::io;
|
||||
use std::panic;
|
||||
|
||||
/// Representation of a terminal user interface.
|
||||
///
|
||||
/// It is responsible for setting up the terminal,
|
||||
/// initializing the interface and handling the draw events.
|
||||
#[derive(Debug)]
|
||||
pub struct Tui<B: Backend> {
|
||||
/// Interface to the Terminal.
|
||||
terminal: Terminal<B>,
|
||||
/// Terminal event handler.
|
||||
pub events: EventHandler,
|
||||
}
|
||||
|
||||
impl<B: Backend> Tui<B> {
|
||||
/// Constructs a new instance of [`Tui`].
|
||||
pub fn new(terminal: Terminal<B>, events: EventHandler) -> Self {
|
||||
Self { terminal, events }
|
||||
}
|
||||
|
||||
/// Initializes the terminal interface.
|
||||
///
|
||||
/// It enables the raw mode and sets terminal properties.
|
||||
pub fn init(&mut self) -> Result<()> {
|
||||
terminal::enable_raw_mode()?;
|
||||
crossterm::execute!(io::stderr(), EnterAlternateScreen, EnableMouseCapture)?;
|
||||
|
||||
// Define a custom panic hook to reset the terminal properties.
|
||||
// This way, you won't have your terminal messed up if an unexpected error happens.
|
||||
let panic_hook = panic::take_hook();
|
||||
panic::set_hook(Box::new(move |panic| {
|
||||
Self::reset().expect("failed to reset the terminal");
|
||||
panic_hook(panic);
|
||||
}));
|
||||
|
||||
self.terminal.hide_cursor()?;
|
||||
self.terminal.clear()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// [`Draw`] the terminal interface by [`rendering`] the widgets.
|
||||
///
|
||||
/// [`Draw`]: tui::Terminal::draw
|
||||
/// [`rendering`]: crate::ui:render
|
||||
pub fn draw(&mut self, app: &mut App) -> Result<()> {
|
||||
self.terminal.draw(|frame| ui::render(app, frame))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Resets the terminal interface.
|
||||
///
|
||||
/// This function is also used for the panic hook to revert
|
||||
/// the terminal properties if unexpected errors occur.
|
||||
fn reset() -> Result<()> {
|
||||
terminal::disable_raw_mode()?;
|
||||
crossterm::execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Exits the terminal interface.
|
||||
///
|
||||
/// It disables the raw mode and reverts back the terminal properties.
|
||||
pub fn exit(&mut self) -> Result<()> {
|
||||
Self::reset()?;
|
||||
self.terminal.show_cursor()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
100
src/ui/mod.rs
Normal file
100
src/ui/mod.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
use ratatui::{
|
||||
backend::Backend,
|
||||
layout::Alignment,
|
||||
prelude::{Constraint, Direction, Layout},
|
||||
style::{Color, Style},
|
||||
text::Line,
|
||||
widgets::{Block, BorderType, Borders, Row, Table, Tabs},
|
||||
Frame,
|
||||
};
|
||||
|
||||
use crate::app::{App, Tab};
|
||||
|
||||
use self::utils::Wrapper;
|
||||
|
||||
mod utils;
|
||||
|
||||
fn render_table<'a>(app: &mut App, tab: Tab) -> (Table<'a>, Vec<Constraint>) {
|
||||
let fields = tab.fields();
|
||||
let torrents = app.torrents.set_fields(None).torrents();
|
||||
|
||||
let rows: Vec<Row<'_>> = torrents
|
||||
.iter()
|
||||
.map(|torrent| {
|
||||
Row::new(
|
||||
fields
|
||||
.iter()
|
||||
.map(|&field| field.value(torrent.clone()))
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let widths = fields
|
||||
.iter()
|
||||
.map(|&field| Constraint::Length(field.width()))
|
||||
.collect();
|
||||
|
||||
let header = Row::new(
|
||||
fields
|
||||
.iter()
|
||||
.map(|&field| field.title())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.style(Style::default().fg(Color::Yellow));
|
||||
(
|
||||
Table::new(rows)
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded),
|
||||
)
|
||||
.header(header)
|
||||
.highlight_style(Style::default().fg(Color::Red))
|
||||
.highlight_symbol(">> ")
|
||||
.column_spacing(1),
|
||||
widths,
|
||||
)
|
||||
}
|
||||
|
||||
/// Renders the user interface widgets.
|
||||
pub fn render<'a, B: Backend>(app: &mut App, frame: &mut Frame<'_, B>) {
|
||||
// This is where you add new widgets.
|
||||
// See the following resources:
|
||||
// - https://docs.rs/ratatui/latest/ratatui/widgets/index.html
|
||||
// - https://github.com/ratatui-org/ratatui/tree/master/examples
|
||||
|
||||
let size = frame.size();
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([Constraint::Length(3), Constraint::Min(0)].as_ref())
|
||||
.split(size);
|
||||
|
||||
// let titles = app.tabs.iter().map(Line::from).collect();
|
||||
let titles = app
|
||||
.tabs()
|
||||
.iter()
|
||||
.map(|x| Line::from(x.to_string()))
|
||||
.collect();
|
||||
let tabs = Tabs::new(titles)
|
||||
.block(
|
||||
Block::default()
|
||||
.title_alignment(Alignment::Center)
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded),
|
||||
)
|
||||
.select(app.index())
|
||||
.style(Style::default().fg(Color::Blue))
|
||||
.highlight_style(Style::default().fg(Color::Green))
|
||||
.divider("|");
|
||||
|
||||
frame.render_widget(tabs, chunks[0]);
|
||||
let (inner, widths) = match app.index() {
|
||||
0 => render_table(app, Tab::All),
|
||||
1 => render_table(app, Tab::Active),
|
||||
2 => render_table(app, Tab::Downloading),
|
||||
3 => render_table(app, Tab::Settings),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
frame.render_stateful_widget(inner.widths(widths.as_ref()), chunks[1], &mut app.state)
|
||||
}
|
||||
212
src/ui/utils.rs
Normal file
212
src/ui/utils.rs
Normal file
@@ -0,0 +1,212 @@
|
||||
use transmission_rpc::types::{ErrorType, Torrent, TorrentGetField, TorrentStatus};
|
||||
|
||||
pub trait Wrapper {
|
||||
fn title(&self) -> String {
|
||||
String::from("")
|
||||
}
|
||||
|
||||
fn value(&self, torrent: Torrent) -> String {
|
||||
format!("{}", torrent.name.unwrap_or(String::from("")))
|
||||
}
|
||||
|
||||
fn width(&self) -> u16 {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
impl Wrapper for TorrentGetField {
|
||||
fn title(&self) -> String {
|
||||
match self {
|
||||
TorrentGetField::ActivityDate => String::from("Activity Date"),
|
||||
TorrentGetField::AddedDate => String::from("Added Date"),
|
||||
TorrentGetField::DoneDate => String::from("Done Date"),
|
||||
TorrentGetField::DownloadDir => String::from("Path"),
|
||||
TorrentGetField::EditDate => String::from("Edit Date"),
|
||||
TorrentGetField::Error => String::from("Error Type"),
|
||||
TorrentGetField::ErrorString => String::from("Error String"),
|
||||
TorrentGetField::Eta => String::from("ETA"),
|
||||
TorrentGetField::FileStats => String::from("File Stats"),
|
||||
TorrentGetField::Files => String::from("Files"),
|
||||
TorrentGetField::HashString => String::from("Hash String"),
|
||||
TorrentGetField::Id => String::from("Id"),
|
||||
TorrentGetField::IsFinished => String::from("Finished"),
|
||||
TorrentGetField::IsPrivate => String::from("Private"),
|
||||
TorrentGetField::IsStalled => String::from("Stalled"),
|
||||
TorrentGetField::Labels => String::from("Labels"),
|
||||
TorrentGetField::LeftUntilDone => String::from("Left Until Done"),
|
||||
TorrentGetField::MetadataPercentComplete => String::from("Metadata Percent Complete"),
|
||||
TorrentGetField::Name => String::from("Name"),
|
||||
TorrentGetField::PeersConnected => String::from("Connected"),
|
||||
TorrentGetField::PeersGettingFromUs => String::from("Peers"),
|
||||
TorrentGetField::PeersSendingToUs => String::from("Seeds"),
|
||||
TorrentGetField::PercentDone => String::from("%"),
|
||||
TorrentGetField::Priorities => String::from("Priorities"),
|
||||
TorrentGetField::QueuePosition => String::from("Queue"),
|
||||
TorrentGetField::RateDownload => String::from("Download Speed"),
|
||||
TorrentGetField::RateUpload => String::from("Upload Speed"),
|
||||
TorrentGetField::RecheckProgress => String::from("Progress"),
|
||||
TorrentGetField::SecondsSeeding => String::from("Seconds Seeding"),
|
||||
TorrentGetField::SeedRatioLimit => String::from("Seed Ratio Limit"),
|
||||
TorrentGetField::SeedRatioMode => String::from("Seed Ratio Mode"),
|
||||
TorrentGetField::SizeWhenDone => String::from("Size"),
|
||||
TorrentGetField::Status => String::from("Status"),
|
||||
TorrentGetField::TorrentFile => String::from("Torrent File"),
|
||||
TorrentGetField::TotalSize => String::from("Total Size"),
|
||||
TorrentGetField::Trackers => String::from("Trackers"),
|
||||
TorrentGetField::UploadRatio => String::from("Ratio"),
|
||||
TorrentGetField::UploadedEver => String::from("Uploaded"),
|
||||
TorrentGetField::Wanted => String::from("Wanted"),
|
||||
TorrentGetField::WebseedsSendingToUs => String::from("Webseeds Sending to Us"),
|
||||
}
|
||||
}
|
||||
|
||||
fn value(&self, torrent: Torrent) -> String {
|
||||
match self {
|
||||
TorrentGetField::ActivityDate => optional_to_string(torrent.activity_date),
|
||||
TorrentGetField::AddedDate => optional_to_string(torrent.added_date),
|
||||
TorrentGetField::DoneDate => optional_to_string(torrent.done_date),
|
||||
TorrentGetField::DownloadDir => optional_to_string(torrent.download_dir),
|
||||
TorrentGetField::EditDate => optional_to_string(torrent.edit_date),
|
||||
TorrentGetField::Error => match torrent.error {
|
||||
Some(error) => match error {
|
||||
ErrorType::Ok => String::from("Ok"),
|
||||
ErrorType::LocalError => String::from("LocalError"),
|
||||
ErrorType::TrackerError => String::from("TrackerError"),
|
||||
ErrorType::TrackerWarning => String::from("TrackerWarning"),
|
||||
},
|
||||
None => String::from("N/A"),
|
||||
},
|
||||
TorrentGetField::ErrorString => optional_to_string(torrent.error_string),
|
||||
TorrentGetField::Eta => match torrent.eta {
|
||||
Some(eta) => match eta {
|
||||
-1 => String::from(""),
|
||||
_ => eta.to_string(),
|
||||
},
|
||||
None => String::from(""),
|
||||
},
|
||||
TorrentGetField::FileStats => match torrent.file_stats {
|
||||
Some(file_stats) => file_stats.iter().map(|x| x.priority.to_string()).collect(),
|
||||
None => String::from("N/A"),
|
||||
},
|
||||
TorrentGetField::Files => match torrent.files {
|
||||
Some(files) => files.iter().map(|x| x.name.to_owned()).collect(),
|
||||
None => String::from("N/A"),
|
||||
},
|
||||
TorrentGetField::HashString => optional_to_string(torrent.hash_string),
|
||||
TorrentGetField::Id => optional_to_string(torrent.id),
|
||||
TorrentGetField::IsFinished => optional_to_string(torrent.is_finished),
|
||||
TorrentGetField::IsPrivate => optional_to_string(torrent.is_private),
|
||||
TorrentGetField::IsStalled => optional_to_string(torrent.is_stalled),
|
||||
TorrentGetField::Labels => match torrent.labels {
|
||||
Some(labels) => labels.join(" "),
|
||||
None => String::from("N/A"),
|
||||
},
|
||||
TorrentGetField::LeftUntilDone => optional_to_string(torrent.left_until_done),
|
||||
TorrentGetField::MetadataPercentComplete => {
|
||||
optional_to_string(torrent.metadata_percent_complete)
|
||||
}
|
||||
TorrentGetField::Name => optional_to_string(torrent.name),
|
||||
TorrentGetField::PeersConnected => optional_to_string(torrent.peers_connected),
|
||||
TorrentGetField::PeersGettingFromUs => {
|
||||
optional_to_string(torrent.peers_getting_from_us)
|
||||
}
|
||||
TorrentGetField::PeersSendingToUs => optional_to_string(torrent.peers_sending_to_us),
|
||||
TorrentGetField::PercentDone => match torrent.percent_done {
|
||||
Some(percent_done) => format!("{:.0}", percent_done * 100.0),
|
||||
None => String::from("N/A"),
|
||||
},
|
||||
TorrentGetField::Priorities => match torrent.priorities {
|
||||
Some(priorities) => priorities.iter().map(|x| x.to_string()).collect(),
|
||||
None => String::from("N/A"),
|
||||
},
|
||||
TorrentGetField::QueuePosition => String::from("N/A"),
|
||||
TorrentGetField::RateDownload => optional_to_string(torrent.rate_download),
|
||||
TorrentGetField::RateUpload => optional_to_string(torrent.rate_upload),
|
||||
TorrentGetField::RecheckProgress => optional_to_string(torrent.recheck_progress),
|
||||
TorrentGetField::SecondsSeeding => optional_to_string(torrent.seconds_seeding),
|
||||
TorrentGetField::SeedRatioLimit => optional_to_string(torrent.seed_ratio_limit),
|
||||
TorrentGetField::SeedRatioMode => String::from("N/A"),
|
||||
TorrentGetField::SizeWhenDone => optional_to_string(torrent.size_when_done),
|
||||
TorrentGetField::Status => match torrent.status {
|
||||
Some(status) => match status {
|
||||
TorrentStatus::Stopped => String::from("Stopped"),
|
||||
TorrentStatus::Seeding => String::from("Seeding"),
|
||||
TorrentStatus::Verifying => String::from("Verifying"),
|
||||
TorrentStatus::Downloading => String::from("Downloading"),
|
||||
TorrentStatus::QueuedToSeed => String::from("QueuedToSeed"),
|
||||
TorrentStatus::QueuedToVerify => String::from("QueuedToVerify"),
|
||||
TorrentStatus::QueuedToDownload => String::from("QueuedToDownload"),
|
||||
},
|
||||
None => String::from("N/A"),
|
||||
},
|
||||
TorrentGetField::TorrentFile => optional_to_string(torrent.torrent_file),
|
||||
TorrentGetField::TotalSize => optional_to_string(torrent.total_size),
|
||||
TorrentGetField::Trackers => match torrent.trackers {
|
||||
Some(trackers) => trackers.iter().map(|x| x.announce.to_string()).collect(),
|
||||
None => String::from("N/A"),
|
||||
},
|
||||
TorrentGetField::UploadRatio => match torrent.upload_ratio {
|
||||
Some(upload_ratio) => format!("{:.2}", upload_ratio),
|
||||
None => String::from("N/A"),
|
||||
},
|
||||
TorrentGetField::UploadedEver => optional_to_string(torrent.uploaded_ever),
|
||||
TorrentGetField::Wanted => match torrent.wanted {
|
||||
Some(wanted) => wanted.iter().map(|x| x.to_string()).collect(),
|
||||
None => String::from("N/A"),
|
||||
},
|
||||
TorrentGetField::WebseedsSendingToUs => String::from("N/A"),
|
||||
}
|
||||
}
|
||||
|
||||
fn width(&self) -> u16 {
|
||||
match self {
|
||||
TorrentGetField::ActivityDate => 10,
|
||||
TorrentGetField::AddedDate => 10,
|
||||
TorrentGetField::DoneDate => 10,
|
||||
TorrentGetField::DownloadDir => 30,
|
||||
TorrentGetField::EditDate => 10,
|
||||
TorrentGetField::Error => 10,
|
||||
TorrentGetField::ErrorString => 10,
|
||||
TorrentGetField::Eta => 10,
|
||||
TorrentGetField::FileStats => 10,
|
||||
TorrentGetField::Files => 10,
|
||||
TorrentGetField::HashString => 10,
|
||||
TorrentGetField::Id => 10,
|
||||
TorrentGetField::IsFinished => 10,
|
||||
TorrentGetField::IsPrivate => 10,
|
||||
TorrentGetField::IsStalled => 10,
|
||||
TorrentGetField::Labels => 10,
|
||||
TorrentGetField::LeftUntilDone => 10,
|
||||
TorrentGetField::MetadataPercentComplete => 10,
|
||||
TorrentGetField::Name => 70,
|
||||
TorrentGetField::PeersConnected => 10,
|
||||
TorrentGetField::PeersGettingFromUs => 10,
|
||||
TorrentGetField::PeersSendingToUs => 10,
|
||||
TorrentGetField::PercentDone => 10,
|
||||
TorrentGetField::Priorities => 10,
|
||||
TorrentGetField::QueuePosition => 10,
|
||||
TorrentGetField::RateDownload => 10,
|
||||
TorrentGetField::RateUpload => 10,
|
||||
TorrentGetField::RecheckProgress => 10,
|
||||
TorrentGetField::SecondsSeeding => 10,
|
||||
TorrentGetField::SeedRatioLimit => 10,
|
||||
TorrentGetField::SeedRatioMode => 10,
|
||||
TorrentGetField::SizeWhenDone => 10,
|
||||
TorrentGetField::Status => 15,
|
||||
TorrentGetField::TorrentFile => 10,
|
||||
TorrentGetField::TotalSize => 10,
|
||||
TorrentGetField::Trackers => 10,
|
||||
TorrentGetField::UploadRatio => 10,
|
||||
TorrentGetField::UploadedEver => 10,
|
||||
TorrentGetField::Wanted => 10,
|
||||
TorrentGetField::WebseedsSendingToUs => 10,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn optional_to_string<T: ToString>(option: Option<T>) -> String {
|
||||
match option {
|
||||
Some(val) => val.to_string(),
|
||||
None => String::from("N/A"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user