yazi_core/notify/
message.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::time::{Duration, Instant};

use unicode_width::UnicodeWidthStr;
use yazi_proxy::options::{NotifyLevel, NotifyOpt};

use super::NOTIFY_BORDER;

pub struct Message {
	pub title:   String,
	pub content: String,
	pub level:   NotifyLevel,
	pub timeout: Duration,

	pub instant:   Instant,
	pub percent:   u8,
	pub max_width: usize,
}

impl From<NotifyOpt> for Message {
	fn from(opt: NotifyOpt) -> Self {
		let title = opt.title.lines().next().unwrap_or_default();
		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(),
			content: opt.content,
			level:   opt.level,
			timeout: opt.timeout,

			instant:   Instant::now(),
			percent:   0,
			max_width: max_width + NOTIFY_BORDER as usize,
		}
	}
}

impl Message {
	#[inline]
	pub fn height(&self, width: u16) -> usize {
		let lines = ratatui::widgets::Paragraph::new(self.content.as_str())
			.wrap(ratatui::widgets::Wrap { trim: false })
			.line_count(width);

		lines + NOTIFY_BORDER as usize
	}
}

impl PartialEq for Message {
	fn eq(&self, other: &Self) -> bool {
		self.level == other.level && self.content == other.content && self.title == other.title
	}
}