diff options
Diffstat (limited to 'src/modes/display/preview.rs')
-rw-r--r-- | src/modes/display/preview.rs | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/modes/display/preview.rs b/src/modes/display/preview.rs index a04e2a9..05dfcfd 100644 --- a/src/modes/display/preview.rs +++ b/src/modes/display/preview.rs @@ -16,7 +16,7 @@ use tuikit::attr::{Attr, Color}; use crate::common::{ CALC_PDF_PATH, FFMPEG, FONTIMAGE, ISOINFO, JUPYTER, LIBREOFFICE, LSBLK, LSOF, MEDIAINFO, - PANDOC, RSVG_CONVERT, SS, THUMBNAIL_PATH, UEBERZUG, + PANDOC, RSVG_CONVERT, SS, THUMBNAIL_PATH, TRANSMISSION_SHOW, UEBERZUG, }; use crate::log_info; use crate::modes::ContentWindow; @@ -47,6 +47,7 @@ pub enum ExtensionKind { Notebook, Office, Epub, + Torrent, #[default] Unknown, } @@ -72,6 +73,7 @@ impl ExtensionKind { "ipynb" => Self::Notebook, "doc" | "docx" | "odt" | "sxw" | "xlsx" | "xls" => Self::Office, "epub" => Self::Epub, + "torrent" => Self::Torrent, _ => Self::Unknown, } } @@ -107,6 +109,7 @@ pub enum Preview { Socket(Socket), BlockDevice(BlockDevice), FifoCharDevice(FifoCharDevice), + Torrent(Torrent), #[default] Empty, } @@ -201,6 +204,10 @@ impl Preview { ExtensionKind::Epub if is_program_in_path(PANDOC) => { Ok(Self::epub(&file_info.path).context("Preview: Couldn't parse epub")?) } + ExtensionKind::Torrent if is_program_in_path(TRANSMISSION_SHOW) => { + Ok(Self::torrent(&file_info.path) + .context("Preview couldn't explore the torrent file")?) + } _ => match Self::preview_syntaxed(extension, &file_info.path) { Some(syntaxed_preview) => Ok(syntaxed_preview), None => Self::preview_text_or_binary(file_info), @@ -300,6 +307,10 @@ impl Preview { )) } + pub fn torrent(path: &Path) -> Result<Self> { + Ok(Self::Torrent(Torrent::new(path).context("")?)) + } + /// The size (most of the time the number of lines) of the preview. /// Some preview (thumbnail, empty) can't be scrolled and their size is always 0. pub fn len(&self) -> usize { @@ -317,6 +328,7 @@ impl Preview { Self::Socket(socket) => socket.len(), Self::BlockDevice(blockdevice) => blockdevice.len(), Self::FifoCharDevice(fifo) => fifo.len(), + Self::Torrent(torrent) => torrent.len(), } } @@ -1106,6 +1118,29 @@ impl Iso { } } +pub struct Torrent { + pub content: Vec<String>, + length: usize, +} + +impl Torrent { + fn new(path: &Path) -> Result<Self> { + let path = path.to_str().context("couldn't parse the path")?; + let content: Vec<String> = + execute_and_capture_output_without_check(TRANSMISSION_SHOW, &[path])? + .lines() + .map(|s| s.to_owned()) + .collect(); + Ok(Self { + length: content.len(), + content, + }) + } + fn len(&self) -> usize { + self.length + } +} + /// Common trait for many preview methods which are just a bunch of lines with /// no specific formatting. /// Some previewing (thumbnail and syntaxed text) needs more details. @@ -1151,3 +1186,4 @@ impl_window!(Socket, String); impl_window!(BlockDevice, String); impl_window!(FifoCharDevice, String); impl_window!(TreeLines, TreeLineBuilder); +impl_window!(Torrent, String); |