Skip to content

Commit

Permalink
fix: notification title width does not include the width of the icon (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored May 3, 2024
1 parent 0e26f5d commit aee65bc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
4 changes: 3 additions & 1 deletion yazi-core/src/notify/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ pub struct Message {
impl From<NotifyOpt> for Message {
fn from(opt: NotifyOpt) -> Self {
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 {
title: title.to_owned(),
Expand Down
14 changes: 3 additions & 11 deletions yazi-fm/src/notify/layout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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_proxy::options::NotifyLevel;

use crate::Ctx;

Expand Down Expand Up @@ -49,12 +47,6 @@ impl<'a> Widget for Layout<'a> {
let tile = Self::tile(available, &notify.messages[..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 =
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;
Expand All @@ -65,9 +57,9 @@ impl<'a> Widget for Layout<'a> {
.block(
Block::bordered()
.border_type(BorderType::Rounded)
.title(format!("{icon} {}", m.title))
.title_style(style)
.border_style(style),
.title(format!("{} {}", m.level.icon(), m.title))
.title_style(*m.level.style())
.border_style(*m.level.style()),
)
.render(rect, buf);
}
Expand Down
25 changes: 23 additions & 2 deletions yazi-proxy/src/options/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::{str::FromStr, time::Duration};

use anyhow::bail;
use mlua::{ExternalError, ExternalResult};
use yazi_shared::event::Cmd;
use yazi_config::THEME;
use yazi_shared::{event::Cmd, theme::Style};

pub struct NotifyOpt {
pub title: String,
Expand Down Expand Up @@ -41,14 +42,34 @@ impl<'a> TryFrom<mlua::Table<'a>> for NotifyOpt {
}
}

#[derive(Default)]
#[derive(Clone, Copy, Default)]
pub enum NotifyLevel {
#[default]
Info,
Warn,
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 {
type Err = anyhow::Error;

Expand Down

0 comments on commit aee65bc

Please sign in to comment.