#![allow(dead_code)]
use std::io::{self, Write};
use crossterm::terminal::{BeginSynchronizedUpdate, EndSynchronizedUpdate};
use crossterm::{execute, queue};
use ratatui::backend::CrosstermBackend;
use ratatui::Terminal;
use crate::term_caps::{self, TerminalCaps};
pub fn draw_synced<F>(
terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
use_sync: bool,
f: F,
) -> io::Result<()>
where
F: FnOnce(&mut ratatui::Frame),
{
if use_sync {
{
let writer = terminal.backend_mut();
queue!(writer, BeginSynchronizedUpdate)?;
}
terminal.draw(f)?;
{
let writer = terminal.backend_mut();
queue!(writer, EndSynchronizedUpdate)?;
writer.flush()?;
}
Ok(())
} else {
terminal.draw(f).map(|_| ())
}
}
pub fn should_sync(gpu_acc: bool, caps: &TerminalCaps) -> bool {
gpu_acc && caps.sync_output
}
pub fn should_rich_underline(gpu_acc: bool, caps: &TerminalCaps) -> bool {
gpu_acc && (caps.undercurl || caps.underline_color)
}
pub fn should_hyperlink(gpu_acc: bool, caps: &TerminalCaps) -> bool {
gpu_acc && caps.hyperlinks
}
pub fn write_hyperlink(stdout: &mut impl Write, url: &str, label: &str) -> io::Result<()> {
write!(
stdout,
"{}{}{}",
term_caps::hyperlink_open(url),
label,
term_caps::hyperlink_end()
)
}
pub fn format_hyperlink_label(url: &str, label: &str) -> String {
format!(
"{}{}{}",
term_caps::hyperlink_open(url),
label,
term_caps::hyperlink_end()
)
}
pub fn reset_underline_sgr(out: &mut impl Write) -> io::Result<()> {
write!(out, "\x1b[4:0m\x1b[24m")?;
out.flush()
}
pub fn reset_underline_sgr_stdout() {
let mut out = io::stdout();
let _ = reset_underline_sgr(&mut out);
}
pub fn emit_file_hyperlink_hint(path: &str) -> io::Result<()> {
let url = term_caps::file_url(path);
let mut out = io::stdout();
let _ = url;
let _ = &mut out;
Ok(())
}
pub fn flush_backend(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<()> {
terminal.backend_mut().flush()
}
pub fn force_end_sync() {
let mut out = io::stdout();
let _ = execute!(out, EndSynchronizedUpdate);
}