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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
mod bubbles;
mod critters;
mod kule;

use bubbles::*;
use clap::Parser;
use critters::*;
use kule::Formats;
use kule::FourBit;
use std::io;
use std::io::Read;
use voca_rs::*;

#[derive(PartialEq, Eq)]
pub enum Modes {
    Say,
    Think,
}

pub fn run(mode: Modes) {
    let mut cli = Args::parse();
    if mode == Modes::Think {
        cli.pilin = true;
    }
    let mut text = String::new();
    let (critter_config, bubble_config);
    match cli.configs_from_arguments() {
        Err((s, e)) => {
            if cli.inli {
                eprintln!("error:\n{}", e);
            } else {
                eprintln!("pakala a!\n{}", s);
            }
            return;
        }
        Ok((c, b)) => {
            (critter_config, bubble_config) = (c, b);
        }
    }
    if cli.seme {
        match critters::list_files() {
            Err((s, e)) => {
                if cli.inli {
                    eprintln!("error: {}", e)
                } else {
                    eprintln!("pakala a!\n{}", s)
                }
            }
            Ok(files) => {
                print!("{}", files.join(" "))
            }
        }
        return;
    } else if !cli.text.is_empty() {
        text = cli.text.join(" ")
    } else {
        io::stdin()
            .read_to_string(&mut text)
            .expect("mi ken ala lukin e pana sina.");
    }
    output(&text, critter_config, bubble_config);
}

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
    #[clap(short = 'e', long, help = "o ante e sitelen lukin")]
    lukin: Option<String>,

    #[clap(short = 'T', long, help = "o pana e uta tawa kijetesantakalu")]
    uta: Option<String>,

    #[clap(short = 'o', long, help = "o pana e ijo tawa kijetesantakalu")]
    ijo: Option<String>,

    #[clap(short = 'i', long, help = "o ante e linja pi poki toki")]
    linja: Option<String>,

    #[clap(short = 'u', long, help = "o ante e poki toki")]
    poki: Option<String>,

    #[clap(short = 'f', long, help = "o kepeken sijelo ante")]
    nimi: Option<String>,

    #[clap(
        short = 'W',
        long,
        default_value = "40",
        help = "o pakala e toki lon sitelen nanpa ni"
    )]
    pakala: usize,

    #[clap(short = 'n', long, help = "o pakala ala e toki")]
    pakala_ala: bool,

    #[clap(
        short = 'k',
        long,
        help = "o ante e kule kijetesantakalu\n(walo, pimeja, loje, jelo, laso, kasi, sewi, unu,\n wawa/suli, len, sike, mamamije)"
    )]
    kule: Vec<String>,

    #[clap(short = 'l', long, help = "sijelo seme li lon?")]
    seme: bool,

    // implementation of classic cowsay flags
    #[clap(short = 'b', long = "o-sama")]
    ilo: bool,

    #[clap(short = 'd', long)]
    moli: bool,

    #[clap(short = 'g', long)]
    wile_mani: bool,

    #[clap(short = 'p', long)]
    monsuta: bool,

    #[clap(short = 's', long)]
    kasi_nasa: bool,

    #[clap(short = 't', long)]
    lape: bool,

    #[clap(short = 'w', long)]
    wawa: bool,

    #[clap(short = 'y', long)]
    lili: bool,

    #[clap(short = 'z', long)]
    suwi: bool,

    #[clap(short = 'P', long)]
    pilin: bool,

    #[clap(short = 'I', long, help = "o kepeken toki Inli lon toki pakala")]
    inli: bool,

    // optional text input
    text: Vec<String>,
}

impl Args {
    fn configs_from_arguments(&self) -> Result<(CritterConfig, BubbleConfig), (String, String)> {
        let mut eyes = self.lukin.clone();
        let mut tongue = self.uta.clone();
        let mut line = self.linja.clone();
        let object = self.ijo.clone();
        let mut format = if self.kule.is_empty() {
            kule::reset()
        } else {
            String::new()
        };
        let name = self.nimi.clone();

        let mut border = self.poki.clone();

        if self.moli {
            eyes = Some("xx".to_string());
            tongue = Some("U".to_string());
        } else if self.wile_mani {
            eyes = Some("$$".to_string());
        } else if self.monsuta {
            eyes = Some("@@".to_string());
        } else if self.kasi_nasa {
            eyes = Some("**".to_string());
            tongue = Some("U".to_string());
        } else if self.lape {
            eyes = Some("--".to_string());
            line = Some("zZz".to_string());
            border = Some("_()-".to_string());
        } else if self.wawa {
            eyes = Some("OO".to_string());
        } else if self.lili {
            eyes = Some("..".to_string());
        } else if self.suwi {
            eyes = Some("^^".to_string());
        }
        if self.pilin {
            line = Some("oOo".to_string());
            border = Some("_()-".to_string());
        }

        for i in &self.kule {
            let lower = query::is_lowercase(&i);
            let lower_i = case::lower_case(&i);
            format.push_str(&match lower_i.as_str() {
                "walo" => {
                    if lower {
                        FourBit::White.escape(false)
                    } else {
                        FourBit::BrightWhite.escape(false)
                    }
                }
                "pimeja" => {
                    if lower {
                        FourBit::Black.escape(false)
                    } else {
                        FourBit::BrightBlack.escape(false)
                    }
                }
                "laso" => {
                    if lower {
                        FourBit::Cyan.escape(false)
                    } else {
                        FourBit::BrightCyan.escape(false)
                    }
                }
                "jelo" => {
                    if lower {
                        FourBit::Yellow.escape(false)
                    } else {
                        FourBit::BrightYellow.escape(false)
                    }
                }
                "loje" => {
                    if lower {
                        FourBit::Red.escape(false)
                    } else {
                        FourBit::BrightRed.escape(false)
                    }
                }
                "kasi" => {
                    if lower {
                        FourBit::Green.escape(false)
                    } else {
                        FourBit::BrightGreen.escape(false)
                    }
                }
                "sewi" => {
                    if lower {
                        FourBit::Blue.escape(false)
                    } else {
                        FourBit::BrightBlue.escape(false)
                    }
                }
                "unu" => {
                    if lower {
                        FourBit::Magenta.escape(false)
                    } else {
                        FourBit::BrightMagenta.escape(false)
                    }
                }
                "suli" => Formats::Bright.escape(false),
                "wawa" => Formats::Bright.escape(false),
                "len" => Formats::Dim.escape(false),
                "mamamije" => Formats::Italic.escape(false),
                "sike" => Formats::Blink.escape(false),
                _ => String::new(),
            })
        }
        let mut critter_config = CritterConfig::config_from_string(
            &eyes,
            &tongue,
            &line,
            &object,
            &Some(format),
            &name,
        )?;
        // borg mode uses special formatting so it needs to happen after the critters been made
        if self.ilo {
            critter_config.left_eye = format!(
                "{}o{}{}",
                Formats::Dim.escape(false),
                kule::reset(),
                critter_config.format
            )
            .to_string();
            critter_config.right_eye = format!(
                "{}{}{}o{}{}",
                Formats::Blink.escape(false),
                Formats::Bright.escape(false),
                FourBit::BrightRed.escape(false),
                kule::reset(),
                critter_config.format
            )
            .to_string();
        }
        let bubble_config = BubbleConfig::config_from_string(
            critter_config.template.anchor,
            self.pakala,
            self.pakala_ala,
            &border,
        );

        Ok((critter_config, bubble_config))
    }
}

fn output(text: &str, critter_config: CritterConfig, bubble_config: BubbleConfig) -> () {
    print!("{}", bubble_config.bubble_from_text(text));
    println!("{}{}", critter_config.format_critter(), kule::reset());
}