aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 39fc9fe500c3e273347992f1cb26073ceacfb712 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use clap::Parser;
use crossterm::terminal::size;
use unicode_width::UnicodeWidthStr;

mod mikuart;
use mikuart::get_miku_art;

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Args {
    /// Text to display in the speech bubble
    text: Option<String>,

    /// Style of the Miku art. A random one is chosen if not specified
    #[arg(short, long)]
    style: Option<i32>,

    /// List all available art styles with their indices
    #[arg(short, long)]
    list: bool,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Args::parse();

    if args.list {
        list_all_art_styles();
        return Ok(());
    }

    let text = args.text.unwrap_or_else(|| {
        eprintln!("Error: Text is required when not using --list");
        std::process::exit(1);
    });
    let style = args.style.unwrap_or(-1); // Default to -1 to select a random style
    draw_miku_says(&text, style)?;

    Ok(())
}

fn list_all_art_styles() {
    println!("Available Miku art styles:");
    println!("==========================");

    // Get all art styles to determine the count
    let all_art = get_miku_art(None, Some(true));
    let styles_count = all_art.len();
    println!("Total styles: {}", styles_count);

    for i in 0..styles_count {
        println!("\n--- Style {} ---", i);
        let art = get_miku_art(Some(i as i32), Some(false));
        for line in art {
            println!("{}", line);
        }
    }

    println!("\nUse --style <number> to select a specific style, or omit for random selection.");
}

fn draw_miku_says(text: &str, style: i32) -> Result<(), Box<dyn std::error::Error>> {
    let speech_bubble_lines = get_speech_bubble_lines(text);
    let miku_art = get_miku_art(Some(style), None);

    let (window_width, window_height) = size()?;
    let available_width = window_width as usize;

    let mut all_content_lines = Vec::new();
    all_content_lines.extend(speech_bubble_lines.clone());
    all_content_lines.extend(miku_art.iter().map(|s| s.to_string()));

    let max_content_width = all_content_lines
        .iter()
        .map(|l| l.width())
        .max()
        .unwrap_or(0);

    let overall_left_padding = if available_width > max_content_width {
        (available_width - max_content_width) / 2
    } else {
        0
    };

    let bubble_width = speech_bubble_lines
        .first()
        .map(|line| line.width())
        .unwrap_or(0);

    let first_miku_line = miku_art.first().unwrap_or(&"");
    let tail_pos = first_miku_line
        .chars()
        .position(|c| !c.is_whitespace())
        .unwrap_or(0);

    let bubble_center = bubble_width / 2;
    let miku_tail_absolute_pos = overall_left_padding + tail_pos;

    let bubble_left_padding = miku_tail_absolute_pos.saturating_sub(bubble_center);

    let bubble_line_count = speech_bubble_lines.len();

    let content_height = all_content_lines.len();
    let available_height = window_height as usize;

    let top_padding = if content_height < available_height {
        (available_height - content_height) / 2
    } else {
        0
    };

    for _ in 0..top_padding {
        println!();
    }

    for (i, line) in all_content_lines.iter().enumerate() {
        let is_bubble_line = i < bubble_line_count;
        let is_tail_line =
            is_bubble_line && (i == bubble_line_count - 2 || i == bubble_line_count - 1);

        if is_bubble_line {
            if is_tail_line {
                let tail_padding = overall_left_padding + tail_pos;
                let horizontal_padding = " ".repeat(tail_padding);
                println!("{horizontal_padding}{line}");
            } else {
                let horizontal_padding = " ".repeat(bubble_left_padding);
                println!("{horizontal_padding}{line}");
            }
        } else {
            let horizontal_padding = " ".repeat(overall_left_padding);
            println!("{horizontal_padding}{line}");
        }
    }

    for _ in 0..top_padding {
        println!();
    }

    Ok(())
}

fn get_speech_bubble_lines(text: &str) -> Vec<String> {
    const MAX_BUBBLE_WIDTH: usize = 50;
    const MIN_BUBBLE_WIDTH: usize = 20;

    let temp_lines = wrap_text(text, MAX_BUBBLE_WIDTH - 4);

    let content_width = temp_lines
        .iter()
        .map(|line| line.width())
        .max()
        .unwrap_or(0);

    let bubble_width = (content_width + 4).clamp(MIN_BUBBLE_WIDTH, MAX_BUBBLE_WIDTH);

    let lines = wrap_text(text, bubble_width - 4);
    let mut bubble_lines = Vec::new();

    let top_left = '╭';
    let top_right = '╮';
    let bottom_left = '╰';
    let bottom_right = '╯';
    let horizontal = '─';
    let vertical = '│';

    bubble_lines.push(format!(
        "{}{}{}",
        top_left,
        horizontal.to_string().repeat(bubble_width),
        top_right
    ));

    for line in &lines {
        let line_display_width = line.width();
        let padding_needed = (bubble_width - 2).saturating_sub(line_display_width);
        let left_padding = padding_needed / 2;
        let right_padding = padding_needed - left_padding;

        bubble_lines.push(format!(
            "{} {}{}{} {}",
            vertical,
            " ".repeat(left_padding),
            line,
            " ".repeat(right_padding),
            vertical
        ));
    }

    bubble_lines.push(format!(
        "{}{}{}",
        bottom_left,
        horizontal.to_string().repeat(bubble_width),
        bottom_right
    ));

    bubble_lines.push("\\".to_string());
    bubble_lines.push(" \\".to_string());

    bubble_lines
}

fn wrap_text(text: &str, max_width: usize) -> Vec<String> {
    let mut lines = Vec::new();
    let words: Vec<&str> = text.split_whitespace().collect();
    let mut current_line = String::new();

    for word in words {
        let word_width = word.width();
        let current_width = current_line.width();

        if current_width + word_width < max_width {
            if !current_line.is_empty() {
                current_line.push(' ');
            }
            current_line.push_str(word);
        } else {
            if !current_line.is_empty() {
                lines.push(current_line);
            }
            current_line = word.to_string();
        }
    }

    if !current_line.is_empty() {
        lines.push(current_line);
    }

    lines
}