yazi_core/notify/
notify.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
use std::ops::ControlFlow;

use ratatui::layout::Rect;
use tokio::task::JoinHandle;

use super::{Message, NOTIFY_SPACING};

#[derive(Default)]
pub struct Notify {
	pub(super) tick_handle: Option<JoinHandle<()>>,
	pub messages:           Vec<Message>,
}

impl Notify {
	pub fn limit(&self, area: Rect) -> usize {
		if self.messages.is_empty() {
			return 0;
		}

		let mut height = area.height as usize;
		let flow = (0..self.messages.len().min(3)).try_fold(0, |acc, i| {
			match height.checked_sub(self.messages[i].height(area.width) + NOTIFY_SPACING as usize) {
				Some(h) => {
					height = h;
					ControlFlow::Continue(acc + 1)
				}
				None => ControlFlow::Break(acc),
			}
		});

		1.max(match flow {
			ControlFlow::Continue(i) => i,
			ControlFlow::Break(i) => i,
		})
	}
}