This commit is contained in:
sxyazi 2023-08-19 10:53:41 +08:00
parent 876a906bed
commit da62d6d7c2
No known key found for this signature in database
5 changed files with 26 additions and 41 deletions

View file

@ -10,11 +10,12 @@ pub struct Image;
impl Image { impl Image {
pub(super) async fn crop(path: &Path, size: (u16, u16)) -> Result<DynamicImage> { pub(super) async fn crop(path: &Path, size: (u16, u16)) -> Result<DynamicImage> {
let (w, h) = { let (w, h) = Term::ratio()
let r = Term::ratio(); .map(|(w, h)| {
let (w, h) = ((size.0 as f64 * r.0) as u32, (size.1 as f64 * r.1) as u32); 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)) (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 = fs::read(path).await?;
let img = tokio::task::spawn_blocking(move || -> Result<DynamicImage> { let img = tokio::task::spawn_blocking(move || -> Result<DynamicImage> {

View file

@ -25,7 +25,7 @@ impl Ctx {
} }
pub(super) fn area(&self, pos: &Position) -> Rect { 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 { let (x, y) = match pos {
Position::None => return Rect::default(), Position::None => return Rect::default(),

View file

@ -1,5 +1,6 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use crossterm::terminal::WindowSize;
use indexmap::map::Slice; use indexmap::map::Slice;
use ratatui::layout::Rect; use ratatui::layout::Rect;
use shared::Term; use shared::Term;
@ -183,12 +184,12 @@ impl Folder {
pub fn rect_current(&self, path: &Path) -> Option<Rect> { pub fn rect_current(&self, path: &Path) -> Option<Rect> {
let pos = self.position(path)? - self.offset; let pos = self.position(path)? - self.offset;
let s = Term::size(); let WindowSize { columns, .. } = Term::size();
Some(Rect { 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, 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, height: 1,
}) })
} }

View file

@ -3,6 +3,7 @@ use std::{io::BufRead, mem, path::{Path, PathBuf}, sync::{atomic::{AtomicUsize,
use adaptor::Adaptor; use adaptor::Adaptor;
use anyhow::{anyhow, bail, Result}; use anyhow::{anyhow, bail, Result};
use config::{BOOT, PREVIEW}; use config::{BOOT, PREVIEW};
use crossterm::terminal::WindowSize;
use ratatui::prelude::Rect; use ratatui::prelude::Rect;
use shared::{MimeKind, Term}; use shared::{MimeKind, Term};
use syntect::{easy::HighlightFile, util::as_24_bit_terminal_escaped}; use syntect::{easy::HighlightFile, util::as_24_bit_terminal_escaped};
@ -31,16 +32,16 @@ pub enum PreviewData {
impl Preview { impl Preview {
fn rect() -> Rect { 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 x = (columns as u32 * (PARENT_RATIO + CURRENT_RATIO) / ALL_RATIO) as u16;
let width = (s.columns as u32 * PREVIEW_RATIO / ALL_RATIO) as u16; let width = (columns as u32 * PREVIEW_RATIO / ALL_RATIO) as u16;
Rect { Rect {
x: x.saturating_add(PREVIEW_BORDER / 2), x: x.saturating_add(PREVIEW_BORDER / 2),
y: PREVIEW_MARGIN / 2, y: PREVIEW_MARGIN / 2,
width: width.saturating_sub(PREVIEW_BORDER), width: width.saturating_sub(PREVIEW_BORDER),
height: s.rows.saturating_sub(PREVIEW_MARGIN), height: rows.saturating_sub(PREVIEW_MARGIN),
} }
} }

View file

@ -1,21 +1,7 @@
use std::{ use std::{io::{stdout, Stdout, Write}, mem, ops::{Deref, DerefMut}};
io::{stdout, Stdout, Write},
mem,
ops::{Deref, DerefMut},
};
use anyhow::Result; use anyhow::Result;
use crossterm::{ 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}};
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}; use ratatui::{backend::CrosstermBackend, Terminal};
pub struct Term { pub struct Term {
@ -60,18 +46,18 @@ impl Term {
} }
// TODO: Use `CSI 14 t` to get the actual size of the terminal // TODO: Use `CSI 14 t` to get the actual size of the terminal
if size.width == 0 || size.height == 0 { // if size.width == 0 || size.height == 0 {}
size.width = 1000;
size.height = 1000;
}
size size
} }
#[inline] #[inline]
pub fn ratio() -> (f64, f64) { pub fn ratio() -> Option<(f64, f64)> {
let s = Self::size(); 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] #[inline]
@ -102,13 +88,9 @@ impl Drop for Term {
impl Deref for Term { impl Deref for Term {
type Target = Terminal<CrosstermBackend<Stdout>>; type Target = Terminal<CrosstermBackend<Stdout>>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target { &self.inner }
&self.inner
}
} }
impl DerefMut for Term { impl DerefMut for Term {
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.inner }
&mut self.inner
}
} }