ghtool/
term.rs

1use std::io::{self, Write};
2
3use eyre::Result;
4
5use crate::github;
6
7pub fn bold(text: &str) -> String {
8    format!("\x1b[1m{}\x1b[0m", text)
9}
10
11pub fn green(text: &str) -> String {
12    format!("\x1b[32m{}\x1b[0m", text)
13}
14
15pub fn print_header(header: &str) {
16    if let Some((w, _)) = term_size::dimensions() {
17        let lines = header.split('\n').collect::<Vec<_>>();
18        let horizontal_border = "─".repeat(w - 2);
19        let border = format!("┌{}┐", horizontal_border);
20        let end_border = format!("└{}┘", horizontal_border);
21        println!("{}", border);
22        for line in lines {
23            let stripped_line = strip_ansi_escapes::strip(line);
24            let mut line = String::from_utf8(stripped_line).unwrap();
25            let line_len = line.chars().count();
26            if line_len > w - 4 {
27                let truncated_line_len = w - 7; // For ellipsis and spaces
28                line = line.chars().take(truncated_line_len).collect::<String>();
29                line.push_str("...");
30            }
31            let line_padding = w - line.chars().count() - 4;
32            let header_line = format!("│ {}{} │", line, " ".repeat(line_padding));
33            println!("{}", header_line);
34        }
35        println!("{}", end_border);
36    }
37}
38
39pub fn exit_with_error<T>(e: eyre::Error) -> T {
40    eprintln!("{}", e);
41    std::process::exit(1);
42}
43
44pub fn print_check_run_header(check_run: &github::SimpleCheckRun) {
45    print_header(&format!(
46        "{} {}\n{} {}",
47        bold("Job:"),
48        check_run.name,
49        bold("Url:"),
50        check_run.url.as_ref().unwrap()
51    ));
52}
53
54pub fn print_all_checks_green() {
55    eprintln!("{} All checks are green", green("✓"));
56}
57
58pub fn read_stdin() -> Result<String> {
59    let mut input = String::new();
60    io::stdin().read_line(&mut input)?;
61    Ok(input.trim().to_string())
62}
63
64pub fn prompt_for_user_to_continue(prompt_message: &str) -> io::Result<()> {
65    print!("{}", prompt_message);
66    io::stdout().flush()?;
67
68    let mut input = String::new();
69    io::stdin().read_line(&mut input)?;
70    Ok(())
71}