Skip to content

Commit 3a85db5

Browse files
committed
refactor: update notification layout
1 parent 70dd412 commit 3a85db5

5 files changed

Lines changed: 53 additions & 53 deletions

File tree

src/cli.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ pub mod handler;
88

99
pub mod chatgpt;
1010

11-
pub mod cli;
12-
1311
pub mod config;
1412

1513
pub mod ui;

src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use ratatui::backend::CrosstermBackend;
22
use ratatui::Terminal;
33
use std::{env, io};
44
use tenere::app::{App, AppResult};
5-
use tenere::cli;
65
use tenere::config::Config;
76
use tenere::event::{Event, EventHandler};
87
use tenere::formatter::Formatter;
@@ -15,11 +14,13 @@ use tenere::llm::LLMModel;
1514
use std::sync::Arc;
1615
use tokio::sync::Mutex;
1716

18-
use clap::crate_version;
17+
use clap::{crate_version, Command};
1918

2019
#[tokio::main]
2120
async fn main() -> AppResult<()> {
22-
cli::cli().version(crate_version!()).get_matches();
21+
Command::new("tenere")
22+
.version(crate_version!())
23+
.get_matches();
2324

2425
let config = Arc::new(Config::load());
2526

src/notification.rs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ratatui::{
2-
layout::{Alignment, Rect},
2+
layout::{Alignment, Constraint, Direction, Layout, Rect},
33
style::{Color, Modifier, Style},
44
text::{Line, Text},
55
widgets::{Block, BorderType, Borders, Clear, Paragraph, Wrap},
@@ -25,38 +25,71 @@ impl Notification {
2525
Self {
2626
message,
2727
level,
28-
ttl: 8,
28+
ttl: 4,
2929
}
3030
}
3131

32-
pub fn render(&mut self, frame: &mut Frame, block: Rect) {
32+
pub fn render(&self, index: usize, frame: &mut Frame) {
3333
let (color, title) = match self.level {
3434
NotificationLevel::Info => (Color::Green, "Info"),
3535
NotificationLevel::Warning => (Color::Yellow, "Warning"),
3636
NotificationLevel::Error => (Color::Red, "Error"),
3737
};
3838

39-
let text = Text::from(vec![
40-
Line::styled(
41-
title,
42-
Style::default().fg(color).add_modifier(Modifier::BOLD),
43-
)
44-
.alignment(Alignment::Center),
45-
Line::raw(self.message.as_str()),
39+
let mut text = Text::from(vec![
40+
Line::from(title).style(Style::new().fg(color).add_modifier(Modifier::BOLD))
4641
]);
4742

48-
let para = Paragraph::new(text)
43+
text.extend(Text::from(self.message.as_str()));
44+
45+
let notification_height = text.height() as u16 + 2;
46+
let notification_width = text.width() as u16 + 4;
47+
48+
let block = Paragraph::new(text)
4949
.wrap(Wrap { trim: false })
5050
.alignment(Alignment::Center)
5151
.block(
5252
Block::default()
5353
.borders(Borders::ALL)
5454
.style(Style::default())
55-
.border_type(BorderType::Rounded)
55+
.border_type(BorderType::Thick)
5656
.border_style(Style::default().fg(color)),
5757
);
5858

59-
frame.render_widget(Clear, block);
60-
frame.render_widget(para, block);
59+
let area = notification_rect(
60+
index as u16,
61+
notification_height,
62+
notification_width,
63+
frame.size(),
64+
);
65+
66+
frame.render_widget(Clear, area);
67+
frame.render_widget(block, area);
6168
}
6269
}
70+
71+
pub fn notification_rect(offset: u16, height: u16, width: u16, r: Rect) -> Rect {
72+
let popup_layout = Layout::default()
73+
.direction(Direction::Vertical)
74+
.constraints(
75+
[
76+
Constraint::Length(height * offset),
77+
Constraint::Length(height),
78+
Constraint::Min(1),
79+
]
80+
.as_ref(),
81+
)
82+
.split(r);
83+
84+
Layout::default()
85+
.direction(Direction::Horizontal)
86+
.constraints(
87+
[
88+
Constraint::Min(1),
89+
Constraint::Length(width),
90+
Constraint::Length(2),
91+
]
92+
.as_ref(),
93+
)
94+
.split(popup_layout[1])[1]
95+
}

src/ui.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,6 @@ use ratatui::{
88

99
pub type AppResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
1010

11-
pub fn notification_rect(offset: u16, r: Rect) -> Rect {
12-
let popup_layout = Layout::default()
13-
.direction(Direction::Vertical)
14-
.constraints(
15-
[
16-
Constraint::Length(1 + 5 * offset),
17-
Constraint::Length(5),
18-
Constraint::Min(1),
19-
]
20-
.as_ref(),
21-
)
22-
.split(r);
23-
24-
Layout::default()
25-
.direction(Direction::Horizontal)
26-
.constraints(
27-
[
28-
Constraint::Percentage(74),
29-
Constraint::Percentage(25),
30-
Constraint::Percentage(1),
31-
]
32-
.as_ref(),
33-
)
34-
.split(popup_layout[1])[1]
35-
}
36-
3711
pub fn help_rect(r: Rect) -> Rect {
3812
let popup_layout = Layout::default()
3913
.direction(Direction::Vertical)
@@ -119,8 +93,7 @@ pub fn render(app: &mut App, frame: &mut Frame) {
11993
}
12094

12195
// Notifications
122-
for (i, notif) in app.notifications.iter_mut().enumerate() {
123-
let area = notification_rect(i as u16, frame_size);
124-
notif.render(frame, area);
96+
for (index, notification) in app.notifications.iter().enumerate() {
97+
notification.render(index, frame);
12598
}
12699
}

0 commit comments

Comments
 (0)