use std::io::{self, Write};
use crossterm::{
cursor,
event::{self, Event, KeyCode, KeyEvent},
execute, queue,
style::Stylize,
terminal,
};
use exhibit::ExhibitExt;
fn main() {
let mut w = io::stdout();
execute!(w, terminal::EnterAlternateScreen).unwrap();
terminal::enable_raw_mode().unwrap();
let (mut x1, mut y1) = (1, 1);
let (mut x2, mut y2) = (1, 1);
let mut is_first_selected = true;
let text1 = format!(
"+-- One -----------------------------+
|{}. Or 's' to switch.|
|{}. |
+------------------------------------+",
"Press 'q' to quit".dark_red(),
"Or 'h', 'j', 'k', 'l' to move".red().italic()
);
let text2 = format!(
"+-- One -----------------------------+
|{}. Or 's' to switch.|
|{}. |
+------------------------------------+",
"Press 'q' to quit".dark_blue(),
"Or 'h', 'j', 'k', 'l' to move".blue().bold()
);
let (width1, height1) = (text1.lines().next().unwrap().len().try_into().unwrap(), 4);
let (width2, height2) = (text2.lines().next().unwrap().len().try_into().unwrap(), 4);
loop {
let (term_width, term_height) = terminal::size().unwrap();
x1 = x1.clamp(1, term_width - width1);
y1 = y1.clamp(1, term_height - height1 - 1);
x2 = x2.clamp(1, term_width - width2);
y2 = y2.clamp(1, term_height - height2 - 1);
queue!(
w,
terminal::Clear(terminal::ClearType::All),
cursor::Hide,
cursor::MoveTo(0, 0),
)
.unwrap();
if is_first_selected {
write!(
w,
"{}",
text2
.exhibit()
.size(width2, height2)
.pos(x2, y2)
.redact(true)
)
.unwrap();
queue!(w, cursor::MoveTo(0, 0),).unwrap();
write!(w, "{}", text1.exhibit().size(width1, height1).pos(x1, y1)).unwrap();
} else {
write!(
w,
"{}",
text1
.exhibit()
.size(width1, height1)
.pos(x1, y1)
.redact(true)
)
.unwrap();
queue!(w, cursor::MoveTo(0, 0),).unwrap();
write!(w, "{}", text2.exhibit().size(width2, height2).pos(x2, y2)).unwrap();
}
w.flush().unwrap();
if let Ok(Event::Key(KeyEvent {
code: KeyCode::Char(c),
..
})) = event::read()
{
match c {
'q' => break,
'l' => {
if is_first_selected {
x1 += 1
} else {
x2 += 1
}
}
'j' => {
if is_first_selected {
y1 += 1
} else {
y2 += 1
}
}
'h' => {
if is_first_selected {
x1 = x1.saturating_sub(1)
} else {
x2 = x2.saturating_sub(1)
}
}
'k' => {
if is_first_selected {
y1 = y1.saturating_sub(1)
} else {
y2 = y2.saturating_sub(1)
}
}
's' => is_first_selected = !is_first_selected,
_ => {}
}
}
}
execute!(w, cursor::Show, terminal::LeaveAlternateScreen).unwrap();
terminal::disable_raw_mode().unwrap();
}