mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
fix: notification title width does not include the width of the icon (#1000)
This commit is contained in:
parent
0e26f5d3c7
commit
aee65bc4d1
3 changed files with 29 additions and 14 deletions
|
|
@ -19,7 +19,9 @@ pub struct Message {
|
||||||
impl From<NotifyOpt> for Message {
|
impl From<NotifyOpt> for Message {
|
||||||
fn from(opt: NotifyOpt) -> Self {
|
fn from(opt: NotifyOpt) -> Self {
|
||||||
let title = opt.title.lines().next().unwrap_or_default();
|
let title = opt.title.lines().next().unwrap_or_default();
|
||||||
let max_width = opt.content.lines().map(|s| s.width()).max().unwrap_or(0).max(title.width());
|
let title_width = title.width() + (opt.level.icon().width() + /* Space */ 1);
|
||||||
|
|
||||||
|
let max_width = opt.content.lines().map(|s| s.width()).max().unwrap_or(0).max(title_width);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
title: title.to_owned(),
|
title: title.to_owned(),
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
use ratatui::{buffer::Buffer, layout::{self, Constraint, Offset, Rect}, widgets::{Block, BorderType, Paragraph, Widget, Wrap}};
|
use ratatui::{buffer::Buffer, layout::{self, Constraint, Offset, Rect}, widgets::{Block, BorderType, Paragraph, Widget, Wrap}};
|
||||||
use yazi_config::THEME;
|
|
||||||
use yazi_core::notify::Message;
|
use yazi_core::notify::Message;
|
||||||
use yazi_proxy::options::NotifyLevel;
|
|
||||||
|
|
||||||
use crate::Ctx;
|
use crate::Ctx;
|
||||||
|
|
||||||
|
|
@ -49,12 +47,6 @@ impl<'a> Widget for Layout<'a> {
|
||||||
let tile = Self::tile(available, ¬ify.messages[..limit]);
|
let tile = Self::tile(available, ¬ify.messages[..limit]);
|
||||||
|
|
||||||
for (i, m) in notify.messages.iter().enumerate().take(limit) {
|
for (i, m) in notify.messages.iter().enumerate().take(limit) {
|
||||||
let (icon, style) = match m.level {
|
|
||||||
NotifyLevel::Info => (&THEME.notify.icon_info, THEME.notify.title_info),
|
|
||||||
NotifyLevel::Warn => (&THEME.notify.icon_warn, THEME.notify.title_warn),
|
|
||||||
NotifyLevel::Error => (&THEME.notify.icon_error, THEME.notify.title_error),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut rect =
|
let mut rect =
|
||||||
tile[i].offset(Offset { x: (100 - m.percent) as i32 * tile[i].width as i32 / 100, y: 0 });
|
tile[i].offset(Offset { x: (100 - m.percent) as i32 * tile[i].width as i32 / 100, y: 0 });
|
||||||
rect.width -= rect.x - tile[i].x;
|
rect.width -= rect.x - tile[i].x;
|
||||||
|
|
@ -65,9 +57,9 @@ impl<'a> Widget for Layout<'a> {
|
||||||
.block(
|
.block(
|
||||||
Block::bordered()
|
Block::bordered()
|
||||||
.border_type(BorderType::Rounded)
|
.border_type(BorderType::Rounded)
|
||||||
.title(format!("{icon} {}", m.title))
|
.title(format!("{} {}", m.level.icon(), m.title))
|
||||||
.title_style(style)
|
.title_style(*m.level.style())
|
||||||
.border_style(style),
|
.border_style(*m.level.style()),
|
||||||
)
|
)
|
||||||
.render(rect, buf);
|
.render(rect, buf);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,8 @@ use std::{str::FromStr, time::Duration};
|
||||||
|
|
||||||
use anyhow::bail;
|
use anyhow::bail;
|
||||||
use mlua::{ExternalError, ExternalResult};
|
use mlua::{ExternalError, ExternalResult};
|
||||||
use yazi_shared::event::Cmd;
|
use yazi_config::THEME;
|
||||||
|
use yazi_shared::{event::Cmd, theme::Style};
|
||||||
|
|
||||||
pub struct NotifyOpt {
|
pub struct NotifyOpt {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
|
@ -41,7 +42,7 @@ impl<'a> TryFrom<mlua::Table<'a>> for NotifyOpt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Clone, Copy, Default)]
|
||||||
pub enum NotifyLevel {
|
pub enum NotifyLevel {
|
||||||
#[default]
|
#[default]
|
||||||
Info,
|
Info,
|
||||||
|
|
@ -49,6 +50,26 @@ pub enum NotifyLevel {
|
||||||
Error,
|
Error,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl NotifyLevel {
|
||||||
|
#[inline]
|
||||||
|
pub fn icon(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Self::Info => &THEME.notify.icon_info,
|
||||||
|
Self::Warn => &THEME.notify.icon_warn,
|
||||||
|
Self::Error => &THEME.notify.icon_error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn style(self) -> &'static Style {
|
||||||
|
match self {
|
||||||
|
Self::Info => &THEME.notify.title_info,
|
||||||
|
Self::Warn => &THEME.notify.title_warn,
|
||||||
|
Self::Error => &THEME.notify.title_error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FromStr for NotifyLevel {
|
impl FromStr for NotifyLevel {
|
||||||
type Err = anyhow::Error;
|
type Err = anyhow::Error;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue