1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::api_types::{compile, download, execute, fmt, share};
use std::io::{self, Write};

// TODO
// pub enum OutputType {
//     Stdout,
//     File,
// }
//
// pub enum FormatType {
//     Table,
//     JSON,
// }

const DEFAULT_ROW_LEN: usize = 90;
const EXECUTION_TITLE: &str = "Execution";
const FORMAT_TITLE: &str = "Format";
const SHARE_TITLE: &str = "Share";
const STDOUT_TITLE: &str = "Standard Output";
const STDERR_TITLE: &str = "Standard Error";
const ERROR_TITLE: &str = "Error";
const DL_TITLE: &str = "Download";

pub struct Printer {
    // TODO: be trait
    writer: io::BufWriter<io::Stdout>,
    width: usize,
}

impl Printer {
    pub fn new() -> Printer {
        let stdout = io::stdout();
        let mut writer = io::BufWriter::new(stdout);

        Printer {
            writer: writer,
            width: DEFAULT_ROW_LEN,
        }
    }

    //
    // TODO: refactor
    //

    pub fn print_run(&mut self, res: execute::Response) -> Result<(), Box<dyn std::error::Error>> {
        if !res.is_error() {
            self.print_horizontal_line()?;
            self.print_header(EXECUTION_TITLE.to_string())?;
            self.print_horizontal_line()?;
            self.print_header(STDERR_TITLE.to_string())?;
            self.print_horizontal_line()?;
            self.print_body(res.stderr)?;
            self.print_horizontal_line()?;
            self.print_header(STDOUT_TITLE.to_string())?;
            self.print_horizontal_line()?;
            self.print_body(res.stdout)?;
            self.print_horizontal_line()?;
        } else {
            self.print_error(res.error)?;
        }

        Ok(())
    }

    pub fn print_fmt(&mut self, res: fmt::Response) -> Result<(), Box<dyn std::error::Error>> {
        if !res.is_error() {
            self.print_horizontal_line()?;
            self.print_header(EXECUTION_TITLE.to_string())?;
            self.print_horizontal_line()?;
            self.print_body(res.code)?;
            self.print_horizontal_line()?;
        } else {
            // TODO: res.code is empty when error
            self.print_error(res.code)?;
        }

        Ok(())
    }

    pub fn print_share(&mut self, res: share::Response) -> Result<(), Box<dyn std::error::Error>> {
        self.print_horizontal_line()?;
        self.print_header(SHARE_TITLE.to_string())?;
        self.print_horizontal_line()?;
        self.print_header("Permalink to the playground".to_string())?;
        self.print_horizontal_line()?;
        self.print_body(res.playground_url())?;
        self.print_horizontal_line()?;
        self.print_header("Direct link to the gist".to_string())?;
        self.print_horizontal_line()?;
        self.print_body(res.gist_url())?;
        self.print_horizontal_line()?;

        Ok(())
    }

    pub fn print_download(
        &mut self,
        res: download::Response,
    ) -> Result<(), Box<dyn std::error::Error>> {
        self.print_horizontal_line()?;
        self.print_header(DL_TITLE.to_string())?;
        self.print_horizontal_line()?;
        self.print_body(res.code)?;
        self.print_horizontal_line()?;

        Ok(())
    }

    fn print_error(&mut self, message: String) -> Result<(), Box<dyn std::error::Error>> {
        self.print_horizontal_line()?;
        self.print_header(ERROR_TITLE.to_string())?;
        self.print_horizontal_line()?;
        self.print_body(message)?;
        self.print_horizontal_line()?;

        Ok(())
    }

    fn print_header(&mut self, title: String) -> Result<(), Box<dyn std::error::Error>> {
        let width = (DEFAULT_ROW_LEN - title.chars().count()) / 2;

        writeln!(
            self.writer,
            "{}{}{}",
            " ".repeat(width),
            title,
            " ".repeat(width),
        )?;

        Ok(())
    }

    fn print_horizontal_line(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        writeln!(self.writer, "{}", "-".repeat(self.width))?;

        Ok(())
    }

    fn print_new_line(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        writeln!(self.writer, "");

        Ok(())
    }

    fn print_body(&mut self, body: String) -> Result<(), Box<dyn std::error::Error>> {
        writeln!(self.writer, "{}", body)?;

        Ok(())
    }
}