From da62d6d7c2d05018c4741c88ac8f8f4fbb4a1c9b Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 19 Aug 2023 10:53:41 +0800 Subject: [PATCH] close #42 --- adaptor/src/image.rs | 11 ++++++----- app/src/context.rs | 2 +- core/src/manager/folder.rs | 7 ++++--- core/src/manager/preview.rs | 9 +++++---- shared/src/term/term.rs | 38 ++++++++++--------------------------- 5 files changed, 26 insertions(+), 41 deletions(-) diff --git a/adaptor/src/image.rs b/adaptor/src/image.rs index 4b8dce16..871c1248 100644 --- a/adaptor/src/image.rs +++ b/adaptor/src/image.rs @@ -10,11 +10,12 @@ pub struct Image; impl Image { pub(super) async fn crop(path: &Path, size: (u16, u16)) -> Result { - let (w, h) = { - let r = Term::ratio(); - let (w, h) = ((size.0 as f64 * r.0) as u32, (size.1 as f64 * r.1) as u32); - (w.min(PREVIEW.max_width), h.min(PREVIEW.max_height)) - }; + let (w, h) = Term::ratio() + .map(|(w, h)| { + let (w, h) = ((size.0 as f64 * w) as u32, (size.1 as f64 * h) as u32); + (w.min(PREVIEW.max_width), h.min(PREVIEW.max_height)) + }) + .unwrap_or((PREVIEW.max_width, PREVIEW.max_height)); let img = fs::read(path).await?; let img = tokio::task::spawn_blocking(move || -> Result { diff --git a/app/src/context.rs b/app/src/context.rs index dc5adbd6..e5b982fe 100644 --- a/app/src/context.rs +++ b/app/src/context.rs @@ -25,7 +25,7 @@ impl Ctx { } pub(super) fn area(&self, pos: &Position) -> Rect { - let WindowSize { rows, columns, .. } = Term::size(); + let WindowSize { columns, rows, .. } = Term::size(); let (x, y) = match pos { Position::None => return Rect::default(), diff --git a/core/src/manager/folder.rs b/core/src/manager/folder.rs index 32133815..377bd800 100644 --- a/core/src/manager/folder.rs +++ b/core/src/manager/folder.rs @@ -1,5 +1,6 @@ use std::path::{Path, PathBuf}; +use crossterm::terminal::WindowSize; use indexmap::map::Slice; use ratatui::layout::Rect; use shared::Term; @@ -183,12 +184,12 @@ impl Folder { pub fn rect_current(&self, path: &Path) -> Option { let pos = self.position(path)? - self.offset; - let s = Term::size(); + let WindowSize { columns, .. } = Term::size(); Some(Rect { - x: (s.columns as u32 * PARENT_RATIO / ALL_RATIO) as u16, + x: (columns as u32 * PARENT_RATIO / ALL_RATIO) as u16, y: pos as u16, - width: (s.columns as u32 * CURRENT_RATIO / ALL_RATIO) as u16, + width: (columns as u32 * CURRENT_RATIO / ALL_RATIO) as u16, height: 1, }) } diff --git a/core/src/manager/preview.rs b/core/src/manager/preview.rs index 50d8fe3a..e55aa843 100644 --- a/core/src/manager/preview.rs +++ b/core/src/manager/preview.rs @@ -3,6 +3,7 @@ use std::{io::BufRead, mem, path::{Path, PathBuf}, sync::{atomic::{AtomicUsize, use adaptor::Adaptor; use anyhow::{anyhow, bail, Result}; use config::{BOOT, PREVIEW}; +use crossterm::terminal::WindowSize; use ratatui::prelude::Rect; use shared::{MimeKind, Term}; use syntect::{easy::HighlightFile, util::as_24_bit_terminal_escaped}; @@ -31,16 +32,16 @@ pub enum PreviewData { impl Preview { fn rect() -> Rect { - let s = Term::size(); + let WindowSize { columns, rows, .. } = Term::size(); - let x = (s.columns as u32 * (PARENT_RATIO + CURRENT_RATIO) / ALL_RATIO) as u16; - let width = (s.columns as u32 * PREVIEW_RATIO / ALL_RATIO) as u16; + let x = (columns as u32 * (PARENT_RATIO + CURRENT_RATIO) / ALL_RATIO) as u16; + let width = (columns as u32 * PREVIEW_RATIO / ALL_RATIO) as u16; Rect { x: x.saturating_add(PREVIEW_BORDER / 2), y: PREVIEW_MARGIN / 2, width: width.saturating_sub(PREVIEW_BORDER), - height: s.rows.saturating_sub(PREVIEW_MARGIN), + height: rows.saturating_sub(PREVIEW_MARGIN), } } diff --git a/shared/src/term/term.rs b/shared/src/term/term.rs index f3bb2737..f84ac7ff 100644 --- a/shared/src/term/term.rs +++ b/shared/src/term/term.rs @@ -1,21 +1,7 @@ -use std::{ - io::{stdout, Stdout, Write}, - mem, - ops::{Deref, DerefMut}, -}; +use std::{io::{stdout, Stdout, Write}, mem, ops::{Deref, DerefMut}}; use anyhow::Result; -use crossterm::{ - event::{ - DisableBracketedPaste, DisableFocusChange, EnableBracketedPaste, EnableFocusChange, - KeyboardEnhancementFlags, PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags, - }, - execute, queue, - terminal::{ - disable_raw_mode, enable_raw_mode, supports_keyboard_enhancement, Clear, ClearType, - EnterAlternateScreen, LeaveAlternateScreen, WindowSize, - }, -}; +use crossterm::{event::{DisableBracketedPaste, DisableFocusChange, EnableBracketedPaste, EnableFocusChange, KeyboardEnhancementFlags, PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags}, execute, queue, terminal::{disable_raw_mode, enable_raw_mode, supports_keyboard_enhancement, Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen, WindowSize}}; use ratatui::{backend::CrosstermBackend, Terminal}; pub struct Term { @@ -60,18 +46,18 @@ impl Term { } // TODO: Use `CSI 14 t` to get the actual size of the terminal - if size.width == 0 || size.height == 0 { - size.width = 1000; - size.height = 1000; - } + // if size.width == 0 || size.height == 0 {} size } #[inline] - pub fn ratio() -> (f64, f64) { + pub fn ratio() -> Option<(f64, f64)> { let s = Self::size(); - (f64::from(s.width) / f64::from(s.columns), f64::from(s.height) / f64::from(s.rows)) + if s.width == 0 || s.height == 0 { + return None; + } + Some((f64::from(s.width) / f64::from(s.columns), f64::from(s.height) / f64::from(s.rows))) } #[inline] @@ -102,13 +88,9 @@ impl Drop for Term { impl Deref for Term { type Target = Terminal>; - fn deref(&self) -> &Self::Target { - &self.inner - } + fn deref(&self) -> &Self::Target { &self.inner } } impl DerefMut for Term { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.inner - } + fn deref_mut(&mut self) -> &mut Self::Target { &mut self.inner } }