Skip to content

Commit

Permalink
refactor: update notification layout
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Mar 19, 2024
1 parent 70dd412 commit 3a85db5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 53 deletions.
5 changes: 0 additions & 5 deletions src/cli.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ pub mod handler;

pub mod chatgpt;

pub mod cli;

pub mod config;

pub mod ui;
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use ratatui::backend::CrosstermBackend;
use ratatui::Terminal;
use std::{env, io};
use tenere::app::{App, AppResult};
use tenere::cli;
use tenere::config::Config;
use tenere::event::{Event, EventHandler};
use tenere::formatter::Formatter;
Expand All @@ -15,11 +14,13 @@ use tenere::llm::LLMModel;
use std::sync::Arc;
use tokio::sync::Mutex;

use clap::crate_version;
use clap::{crate_version, Command};

#[tokio::main]
async fn main() -> AppResult<()> {
cli::cli().version(crate_version!()).get_matches();
Command::new("tenere")
.version(crate_version!())
.get_matches();

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

Expand Down
61 changes: 47 additions & 14 deletions src/notification.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ratatui::{
layout::{Alignment, Rect},
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::{Line, Text},
widgets::{Block, BorderType, Borders, Clear, Paragraph, Wrap},
Expand All @@ -25,38 +25,71 @@ impl Notification {
Self {
message,
level,
ttl: 8,
ttl: 4,
}
}

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

let text = Text::from(vec![
Line::styled(
title,
Style::default().fg(color).add_modifier(Modifier::BOLD),
)
.alignment(Alignment::Center),
Line::raw(self.message.as_str()),
let mut text = Text::from(vec![
Line::from(title).style(Style::new().fg(color).add_modifier(Modifier::BOLD))
]);

let para = Paragraph::new(text)
text.extend(Text::from(self.message.as_str()));

let notification_height = text.height() as u16 + 2;
let notification_width = text.width() as u16 + 4;

let block = Paragraph::new(text)
.wrap(Wrap { trim: false })
.alignment(Alignment::Center)
.block(
Block::default()
.borders(Borders::ALL)
.style(Style::default())
.border_type(BorderType::Rounded)
.border_type(BorderType::Thick)
.border_style(Style::default().fg(color)),
);

frame.render_widget(Clear, block);
frame.render_widget(para, block);
let area = notification_rect(
index as u16,
notification_height,
notification_width,
frame.size(),
);

frame.render_widget(Clear, area);
frame.render_widget(block, area);
}
}

pub fn notification_rect(offset: u16, height: u16, width: u16, r: Rect) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Length(height * offset),
Constraint::Length(height),
Constraint::Min(1),
]
.as_ref(),
)
.split(r);

Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Min(1),
Constraint::Length(width),
Constraint::Length(2),
]
.as_ref(),
)
.split(popup_layout[1])[1]
}
31 changes: 2 additions & 29 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,6 @@ use ratatui::{

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

pub fn notification_rect(offset: u16, r: Rect) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Length(1 + 5 * offset),
Constraint::Length(5),
Constraint::Min(1),
]
.as_ref(),
)
.split(r);

Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Percentage(74),
Constraint::Percentage(25),
Constraint::Percentage(1),
]
.as_ref(),
)
.split(popup_layout[1])[1]
}

pub fn help_rect(r: Rect) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
Expand Down Expand Up @@ -119,8 +93,7 @@ pub fn render(app: &mut App, frame: &mut Frame) {
}

// Notifications
for (i, notif) in app.notifications.iter_mut().enumerate() {
let area = notification_rect(i as u16, frame_size);
notif.render(frame, area);
for (index, notification) in app.notifications.iter().enumerate() {
notification.render(index, frame);
}
}

0 comments on commit 3a85db5

Please sign in to comment.