use super::ll::mvwaddwstr;
use super::traits::WindowLike;
use super::utils::create_window;
use super::utils::ColorPair;
use super::utils::StringExt;
use kconfig_represent::DescriptorInfo;
use ncurses::*;
pub(super) struct InfoWindow {
w: Option<WINDOW>,
descriptor_info: DescriptorInfo,
}
impl InfoWindow {
pub fn descriptor_info(&mut self, descriptor_info: DescriptorInfo) {
self.descriptor_info = descriptor_info;
}
}
impl WindowLike for InfoWindow {
fn new() -> Self {
Self {
w: None,
descriptor_info: DescriptorInfo::default(),
}
}
fn create(&mut self) {
let (left, top, cols, lines) = bounds();
self.w = Some(create_window(
lines,
cols,
top,
left,
ColorPair::InfoWindow.raw(),
));
}
fn del(&self) {
if let Some(w) = self.w {
delwin(w);
}
}
fn raw(&self) -> Option<WINDOW> {
self.w
}
fn draw(&mut self) {
if let Some(w) = self.w {
let (_, _, cols, lines) = bounds();
wclear(w);
wbkgd(w, COLOR_PAIR(ColorPair::InfoWindow.raw()));
wborder(
w,
ACS_VLINE(),
ACS_VLINE(),
ACS_HLINE(),
ACS_HLINE(),
ACS_ULCORNER(),
ACS_URCORNER(),
ACS_LLCORNER(),
ACS_LRCORNER(),
);
let mut col = 2;
let mut line = 2;
let title = format!("{}", self.descriptor_info.descriptor()).unicode_truncate(cols);
mvwaddwstr(w, 0, 2, &title);
for item in self.descriptor_info.iter() {
let words = item.split(char::is_whitespace);
for mut word in words {
if word.chars().count() > cols as usize {
word = &word[..cols as usize];
}
let count = word.chars().count() as i32;
let eol_point = col + count;
if eol_point >= cols - 4 {
line += 1;
col = 2;
}
if line < lines - 2 {
mvwaddnstr(w, line, col, word, count);
col += count + 1;
}
}
line += 1;
}
mvwaddstr(w, lines - 1, 2, "[ENTER] Quit Info Window");
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)
}