use anyhow::{Context, Result};
use crossterm::{
cursor, execute, queue,
style::{Color, Print, ResetColor, SetAttribute, SetForegroundColor},
terminal::{self, Clear, ClearType},
};
use std::io::{stdout, Write};
const PANEL_MAX_WIDTH: u16 = 96;
fn panel() -> (u16, u16, u16, u16) {
let (columns, rows) = terminal::size().unwrap_or((100, 30));
let width = PANEL_MAX_WIDTH.min(columns.saturating_sub(4)).max(48);
let height = rows.saturating_sub(2).max(12);
let left = columns.saturating_sub(width) / 2;
(left, 1, width, height)
}
fn border(width: u16, left: &str, fill: &str, right: &str) -> String {
format!(
"{left}{fill}{right}",
fill = fill.repeat(width.saturating_sub(2) as usize)
)
}
fn visible_width(value: &str) -> usize {
let mut visible = String::new();
let mut escape = false;
for character in value.chars() {
if escape {
if character == 'm' {
escape = false;
}
} else if character == '\x1b' {
escape = true;
} else {
visible.push(character);
}
}
visible.chars().count()
}
fn content_line(value: &str, width: u16) -> String {
let padding = width.saturating_sub(visible_width(value) as u16) as usize;
format!(" {value}{} ", " ".repeat(padding))
}
fn centered_line(value: &str, width: u16) -> String {
let available = width.saturating_sub(2);
let padding = available.saturating_sub(visible_width(value) as u16) as usize;
let left = padding / 2;
let right = padding - left;
format!(" {}{}{} ", " ".repeat(left), value, " ".repeat(right))
}
pub struct Guard;
impl Guard {
pub fn enter() -> Result<Self> {
terminal::enable_raw_mode().context("could not enable terminal raw mode")?;
execute!(
stdout(),
terminal::EnterAlternateScreen,
Clear(ClearType::All),
cursor::Hide
)
.context("could not enter terminal UI")?;
Ok(Self)
}
}
impl Drop for Guard {
fn drop(&mut self) {
let _ = terminal::disable_raw_mode();
let _ = execute!(
stdout(),
cursor::Show,
terminal::LeaveAlternateScreen,
ResetColor
);
}
}
pub fn draw(lines: &[String], cursor: Option<(u16, u16)>) -> Result<()> {
let (left, top, width, height) = panel();
let content_top = top + 4;
let mut out = stdout();
queue!(
out,
terminal::BeginSynchronizedUpdate,
cursor::Hide,
cursor::MoveTo(0, 0)
)?;
for row in 0..=top + height {
queue!(out, cursor::MoveTo(0, row), Clear(ClearType::CurrentLine))?;
}
queue!(
out,
cursor::MoveTo(left, top),
SetForegroundColor(Color::Cyan),
Print(border(width, "╭", "─", "╮")),
cursor::MoveTo(left, top + 1),
SetAttribute(crossterm::style::Attribute::Bold),
Print(format!(
"│{}│",
centered_line("◆ GCT · GIT COMMIT CONTROL ◆", width)
)),
cursor::MoveTo(left, top + 2),
SetAttribute(crossterm::style::Attribute::Reset),
SetForegroundColor(Color::DarkGrey),
Print(format!(
"│{}│",
centered_line("Fast, dated commits from your terminal", width)
)),
cursor::MoveTo(left, top + 3),
SetForegroundColor(Color::Cyan),
Print(border(width, "├", "─", "┤")),
)?;
for row in 0..height.saturating_sub(5) {
let line = lines.get(row as usize).map(String::as_str).unwrap_or("");
queue!(
out,
cursor::MoveTo(left, content_top + row),
SetForegroundColor(Color::White),
Print("│"),
Print(content_line(line, width - 2)),
SetForegroundColor(Color::Cyan),
Print("│"),
)?;
}
queue!(
out,
cursor::MoveTo(left, top + height - 1),
Print(border(width, "╰", "─", "╯")),
ResetColor
)?;
if let Some((row, column)) = cursor {
queue!(
out,
cursor::MoveTo(left + 3 + column, content_top + row),
cursor::Show,
terminal::EndSynchronizedUpdate
)?;
} else {
queue!(out, cursor::Hide, terminal::EndSynchronizedUpdate)?;
}
out.flush()?;
Ok(())
}
pub fn error(message: &str) -> Result<()> {
draw(&[format!("\x1b[31m{message}\x1b[0m")], None)
}
pub fn muted(message: impl AsRef<str>) -> String {
format!("\x1b[90m{}\x1b[0m", message.as_ref())
}
pub fn accent(message: impl AsRef<str>) -> String {
format!("\x1b[36m{}\x1b[0m", message.as_ref())
}
pub fn success(message: impl AsRef<str>) -> String {
format!("\x1b[32m{}\x1b[0m", message.as_ref())
}
pub fn warning(message: impl AsRef<str>) -> String {
format!("\x1b[33m{}\x1b[0m", message.as_ref())
}
pub fn selected(message: impl AsRef<str>) -> String {
format!("\x1b[1;30;46m{}\x1b[0m", message.as_ref())
}