use crate::cli_parser::dotconfig_file;
use super::ll::mvwaddwstr;
use super::traits::WindowLike;
use super::utils::create_window;
use super::utils::ColorPair;
use super::utils::StringExt;
use kconfig_represent::LoadError;
use ncurses::*;
pub(super) struct ErrorWindow {
w: Option<WINDOW>,
text_w: Option<WINDOW>,
error_text: String,
}
impl ErrorWindow {
pub fn load_error(&mut self, load_error: &LoadError) {
self.error_text = format!("{}", load_error);
}
}
impl WindowLike for ErrorWindow {
fn new() -> Self {
Self {
w: None,
text_w: None,
error_text: String::default(),
}
}
fn create(&mut self) {
let (left, top, cols, lines) = bounds();
self.w = Some(create_window(
lines,
cols,
top,
left,
ColorPair::ErrorWindow.raw(),
));
self.text_w = Some(create_window(
lines - 4,
cols - 4,
top + 2,
left + 2,
ColorPair::ErrorWindow.raw(),
));
}
fn del(&self) {
if let Some(w) = self.w {
delwin(w);
}
if let Some(w) = self.text_w {
delwin(w);
}
}
fn raw(&self) -> Option<WINDOW> {
self.w
}
fn draw(&mut self) {
let (_, _, cols, lines) = bounds();
if let Some(w) = self.w {
wclear(w);
wbkgd(w, COLOR_PAIR(ColorPair::ExitWindow.raw()));
wborder(
w,
ACS_VLINE(),
ACS_VLINE(),
ACS_HLINE(),
ACS_HLINE(),
ACS_ULCORNER(),
ACS_URCORNER(),
ACS_LLCORNER(),
ACS_LRCORNER(),
);
let title =
format!(" 🚨 {}: {} ", "Error report for", dotconfig_file()).unicode_truncate(cols);
mvwaddwstr(w, 0, 2, &title);
mvwaddstr(w, lines - 1, 2, " [Enter] Accept | [X] Exit ");
wrefresh(w);
}
if let Some(w) = self.text_w {
wclear(w);
wbkgd(w, COLOR_PAIR(ColorPair::ExitWindow.raw()));
mvwaddstr(w, 0, 0, &format!("{}", self.error_text));
wrefresh(w);
}
}
}
fn bounds() -> (i32, i32, i32, i32) {
let screen_height = getmaxy(stdscr());
let screen_width = getmaxx(stdscr());
let left = 5;
let top = 5;
let cols = screen_width - (left * 2);
let lines = screen_height - (top * 2);
(left, top, cols, lines)
}