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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use piston_window::{*, types::{Color, FontSize}};
use std::{thread, time::{Duration, Instant}};

use crate::{draw::*, text::*, TYPE_TIME};

/// A terminal stores a PistonWindow, background and foreground colors,
/// a font, fontsize, and glyph cache, and the current message and input strings.
pub struct Terminal {
    title: String,
    active: bool,
    /// The window that displays our terminal.
    pub window: PistonWindow,
    /// The background color of our terminal.
    pub bg_color: Color,
    /// The foreground color of our terminal.
    pub fg_color: Color,
    /// Whether or not to use scanlines
    pub scanlines: bool,
    glyphs: Glyphs,
    font: String,
    art_font: String,
    /// The font size of normal text in our terminal.
    pub font_size: FontSize,
    /// The font size of art in our terminal.
    pub art_font_size: FontSize,
    art_mode: bool,
    message: Vec<String>,
    input: String,
}

impl Terminal {
    /// Creates a new window with the given title, colors, and font info
    /// 
    /// ```no_run
    /// # use simpleterm::text::*;
    /// # use simpleterm::terminal::Terminal;
    /// let mut term: Terminal = Terminal::new("simpleterm test", (800, 600), DARK_GREY, GOLD, "LeagueSpartan-Regular.ttf", 32);
    /// ```
    pub fn new(title: &str, size: (u32, u32), bg: Color, fg: Color, font: &str, font_size: u32) -> Terminal {
        let mut new_window: PistonWindow = WindowSettings::new(title, size).exit_on_esc(true).build().unwrap();
        let loaded_glyphs = load_font(&mut new_window, font);

        Terminal {
            title: String::from(title),
            active: true,
            window: new_window,
            bg_color: bg,
            fg_color: fg,
            scanlines: true,
            glyphs: loaded_glyphs,
            font: String::from(font),
            art_font: String::from("LeagueMono-Regular.ttf"),
            font_size,
            art_font_size: 10,
            art_mode: false,
            message: Vec::new(),
            input: String::default(),
        }
    }

    /// Types out the given message, then waits for the user to type something and returns Some(input string).
    /// If the window is closed before input can be returned, returns None.
    /// 
    /// ```no_run
    /// # use simpleterm::text::*;
    /// # use simpleterm::terminal::Terminal;
    /// # let mut term: Terminal = Terminal::new("simpleterm test", (800, 600), DARK_GREY, GOLD, "LeagueSpartan-Regular.ttf", 32);
    /// let user_input: String = term.ask("This will wait for the user enter input!").unwrap();
    /// ```
    pub fn ask(&mut self, message: &str) -> Option<String> {
        if self.active {
            if self.art_mode {
                self.glyphs = load_font(&mut self.window, &self.font);
                self.art_mode = false;
            }

            self.new_message(message);
            self.wait_for_input();
            Some(self.input.clone())
        } else {
            None
        }
    }

    /// Displays an ascii art string centered on the terminal. This uses 10pt font and a monospace font.
    /// 
    /// ```no_run
    /// # use std::time::Duration;
    /// # use simpleterm::{art::*, text::*};
    /// # use simpleterm::terminal::Terminal;
    /// # let mut term: Terminal = Terminal::new("simpleterm test", (800, 600), DARK_GREY, GOLD, "LeagueSpartan-Regular.ttf", 32);
    /// term.display_art(GEO, Duration::from_secs(2));
    /// ```
    pub fn display_art(&mut self, art: &str, time: Duration) {
        if self.active {
            if !self.art_mode {
                self.glyphs = load_font(&mut self.window, &self.art_font);
                self.art_mode = true;
            }

            self.message = art.split('\n').map(String::from).collect();
            self.input = String::default();
            self.show_art(time);
        }
    }
    
    /// Types out the given message, then waits for the given amount of time to continue.
    /// 
    /// ```no_run
    /// # use std::time::Duration;
    /// # use simpleterm::text::*;
    /// # use simpleterm::terminal::Terminal;
    /// # let mut term: Terminal = Terminal::new("simpleterm test", (800, 600), DARK_GREY, GOLD, "LeagueSpartan-Regular.ttf", 32);
    /// term.show("This will wait for 1 second!", Duration::from_secs(1));
    /// ```
    pub fn show(&mut self, message: &str, time: Duration) {
        if self.active {
            if self.art_mode {
                self.glyphs = load_font(&mut self.window, &self.font);
                self.art_mode = false;
            }

            self.new_message(message);
            self.wait_for_timer(time);
        }
    }

    /// Types out the given message, then waits for the user to press Enter to continue.
    /// 
    /// ```no_run
    /// # use simpleterm::text::*;
    /// # use simpleterm::terminal::Terminal;
    /// # let mut term: Terminal = Terminal::new("simpleterm test", (800, 600), DARK_GREY, GOLD, "LeagueSpartan-Regular.ttf", 32);
    /// term.tell("This will wait for the user to hit enter!");
    /// ```
    pub fn tell(&mut self, message: &str) {
        if self.active {
            if self.art_mode {
                self.glyphs = load_font(&mut self.window, &self.font);
                self.art_mode = false;
            }

            self.new_message(message);
            self.input = String::from("Press Enter to Continue");
            self.wait_for_continue();
        }
    }

    /// Closes the current window and creates a new one with the given (x, y) Size.
    /// 
    /// ```no_run
    /// # use simpleterm::text::*;
    /// # use simpleterm::terminal::Terminal;
    /// # let mut term: Terminal = Terminal::new("simpleterm test", (800, 600), DARK_GREY, GOLD, "LeagueSpartan-Regular.ttf", 32);
    /// term.resize((800, 600).into());
    /// ```
    pub fn resize(&mut self, new_size: Size) {
        if self.active {
            let new_window: PistonWindow = WindowSettings::new(self.title.clone(), new_size).exit_on_esc(true).build().unwrap();
            self.window = new_window;
        }
    }

    /// Loads a new font from the given font filename and sets the given font size
    /// 
    /// ```no_run
    /// # use simpleterm::text::*;
    /// # use simpleterm::terminal::Terminal;
    /// # let mut term: Terminal = Terminal::new("simpleterm test", (800, 600), DARK_GREY, GOLD, "LeagueSpartan-Regular.ttf", 32);
    /// term.set_font("LeagueSpartan-Regular.ttf", 24);
    /// ```
    pub fn set_font(&mut self, font: &str, size: FontSize) {
        if self.active {
            if !self.art_mode { self.glyphs = load_font(&mut self.window, font); }
            self.font = String::from(font);
            self.font_size = size;
        }
    }

    /// Loads a new art font from the given font filename and sets the given font size.
    /// You probably want to use a mono-space font here, and a small size.
    /// 
    /// The default is LeagueMono-Regular.ttf at 10pt.
    /// 
    /// ```no_run
    /// # use simpleterm::text::*;
    /// # use simpleterm::terminal::Terminal;
    /// # let mut term: Terminal = Terminal::new("simpleterm test", (800, 600), DARK_GREY, GOLD, "LeagueSpartan-Regular.ttf", 32);
    /// term.set_art_font("LeagueMono-Regular.ttf", 10);
    /// ```
    pub fn set_art_font(&mut self, font: &str, size: FontSize) {
        if self.active {
            if self.art_mode { self.glyphs = load_font(&mut self.window, font); }
            self.art_font = String::from(font);
            self.art_font_size = size;
        }
    }

    /// Changes the terminal's background and foreground to the given colors. The change will be apparent in the next text command.
    /// 
    /// ```no_run
    /// # use simpleterm::text::*;
    /// # use simpleterm::terminal::Terminal;
    /// # let mut term: Terminal = Terminal::new("simpleterm test", (800, 600), DARK_GREY, GOLD, "LeagueSpartan-Regular.ttf", 32);
    /// term.set_colors(DARK_GREY, CRIMSON);
    /// ```
    pub fn set_colors(&mut self, bgc: Color, fgc: Color) {
        self.bg_color = bgc;
        self.fg_color = fgc;
    }

    // Displays an art string along with the rest of the terminal.
    fn show_art(&mut self, timer: Duration) {
        let bgc: Color = self.bg_color;
        let fgc: Color = self.fg_color;

        let art: &Vec<String> = &self.message;
        let glyphs: &mut Glyphs = &mut self.glyphs;
        let font_size: FontSize = self.art_font_size;
        let use_filter: bool = self.scanlines;
        
        let start: Instant = Instant::now();
        let mut active: bool = self.active;
        while let Some(e) = self.window.next() {
            e.close(|_| { active = false; });

            let win_size: Size = self.window.window.size();

            let now: Instant = Instant::now();
            if now.duration_since(start) > timer { break; }

            self.window.draw_2d(&e, |c, g, device| {
                clear(bgc, g);

                draw_background(win_size, bgc, fgc, use_filter, c, g);
                draw_art(win_size, art, glyphs, font_size, fgc, c, g);
                draw_foreground(win_size, bgc, use_filter, c, g);
            
                glyphs.factory.encoder.flush(device);
            });
        }
        self.active = active;
    }

    // Types a message one character at a time, waiting TYPE_TIME between each character.
    fn type_message(&mut self) {
        let bgc: Color = self.bg_color;
        let fgc: Color = self.fg_color;
        let current_input: &str = &(self.input[..]);
        let glyphs = &mut self.glyphs;
        let font_size: FontSize = self.font_size;

        let mut typed_message: Vec<String> = Vec::new();
        let use_filter: bool = self.scanlines;

        let mut active: bool = self.active;
        for (i, line) in self.message.iter().enumerate() {
            typed_message.push(String::default());

            let line_len: usize = line.len();
            for j in 1..line_len {
                typed_message[i] = String::from(&line[..=j]);
                typed_message[i].push_str("[]");
                if let Some(e) = self.window.next() {
                    e.close(|_| { active = false; });

                    let win_size: Size = self.window.window.size();

                    self.window.draw_2d(&e, |c, g, device| {
                        clear(bgc, g);

                        draw_background(win_size, bgc, fgc, use_filter, c, g);
                        draw_message(&typed_message, glyphs, font_size, fgc, c, g);
                        draw_input(win_size, current_input, glyphs, font_size, fgc, c, g);
                        draw_foreground(win_size, bgc, use_filter, c, g);
                    
                        glyphs.factory.encoder.flush(device);
                    });
                    thread::sleep(TYPE_TIME);
                }
                typed_message[i].pop();
                typed_message[i].pop();
            }
        }
        self.active = active;
    }

    // Displays the current terminal until the user presses Enter.
    fn wait_for_continue(&mut self) {
        let mut ready: bool = false;

        let bgc: Color = self.bg_color;
        let fgc: Color = self.fg_color;

        let message: &Vec<String> = &self.message;
        let current_input: &str = &(self.input);
        let glyphs: &mut Glyphs = &mut self.glyphs;
        let font_size: FontSize = self.font_size;
        let use_filter: bool = self.scanlines;
        
        let mut start: Instant = Instant::now();
        let mut active: bool = self.active;
        while let Some(e) = self.window.next() {
            e.close(|_| { active = false; });

            let win_size: Size = self.window.window.size();

            e.button(|button_args| {
                if let Button::Keyboard(key) = button_args.button {
                    if button_args.state == ButtonState::Press && key == Key::Return { ready = true; }
                }
            });

            if ready { break; }

            let now: Instant = Instant::now();
            self.window.draw_2d(&e, |c, g, device| {
                clear(bgc, g);

                draw_background(win_size, bgc, fgc, use_filter, c, g);
                draw_message(message, glyphs, font_size, fgc, c, g);
                draw_input_marker(win_size, glyphs, font_size, fgc, c, g);
                if check_flash(now, &mut start) { draw_input(win_size, current_input, glyphs, font_size, fgc, c, g); }
                draw_foreground(win_size, bgc, use_filter, c, g);
            
                glyphs.factory.encoder.flush(device);
            });
        }
        self.active = active;
    }

    // Displays the current terminal until the user submits some input.
    fn wait_for_input(&mut self) {
        let mut input_string: String = String::default();
        let mut input_accepted: bool = false;

        let bgc: Color = self.bg_color;
        let fgc: Color = self.fg_color;

        let message: &Vec<String> = &self.message;
        let glyphs: &mut Glyphs = &mut self.glyphs;
        let font_size: FontSize = self.font_size;
        let use_filter: bool = self.scanlines;
        
        let mut start: Instant = Instant::now();
        let mut active: bool = self.active;
        while let Some(e) = self.window.next() {
            e.close(|_| { active = false; });

            let win_size: Size = self.window.window.size();
            
            e.text(|text| input_string.push_str(text));
            e.button(|button_args| {
                if let Button::Keyboard(key) = button_args.button {
                    if button_args.state == ButtonState::Press {
                        if key == Key::Backspace { input_string.pop(); }
                        if key == Key::Return && input_string != "" { input_accepted = true; }
                    }
                }
            });

            if input_accepted {
                self.input = input_string.clone();
                input_string = String::default();
            }
            
            let now: Instant = Instant::now();
            self.window.draw_2d(&e, |c, g, device| {
                clear(bgc, g);

                draw_background(win_size, bgc, fgc, use_filter, c, g);
                draw_message(message, glyphs, font_size, fgc, c, g);
                draw_input_marker(win_size, glyphs, font_size, fgc, c, g);

                if check_flash(now, &mut start) {
                    input_string.push_str("[]");
                    draw_input(win_size, &input_string[..], glyphs, font_size, fgc, c, g);
                    input_string.pop();
                    input_string.pop();
                } else {
                    draw_input(win_size, &input_string[..], glyphs, font_size, fgc, c, g);
                }
                
                draw_foreground(win_size, bgc, use_filter, c, g);
            
                glyphs.factory.encoder.flush(device);
            });

            if input_accepted { break; }
        }
        self.active = active;
    }

    // Displays an the current terminal until the timer runs out.
    fn wait_for_timer(&mut self, timer: Duration) {
        let bgc: Color = self.bg_color;
        let fgc: Color = self.fg_color;

        let message: &Vec<String> = &self.message;
        let glyphs: &mut Glyphs = &mut self.glyphs;
        let font_size: FontSize = self.font_size;
        let use_filter: bool = self.scanlines;
        
        let start: Instant = Instant::now();
        let mut active: bool = self.active;
        while let Some(e) = self.window.next() {
            e.close(|_| { active = false; });

            let win_size: Size = self.window.window.size();

            let now: Instant = Instant::now();
            if now.duration_since(start) > timer { break; }

            self.window.draw_2d(&e, |c, g, device| {
                clear(bgc, g);

                draw_background(win_size, bgc, fgc, use_filter, c, g);
                draw_message(message, glyphs, font_size, fgc, c, g);
                draw_foreground(win_size, bgc, use_filter, c, g);
            
                glyphs.factory.encoder.flush(device);
            });
        }
        self.active = active;
    }

    // Processes a new message and types it out.
    fn new_message(&mut self, message: &str) {
        self.message = message.split('\n').map(String::from).collect();
        self.process_message();
        self.input = String::default();
        self.type_message();
    }

    // Splits a message into a vector of strings that can fit in the current window's bounds.
    fn process_message(&mut self) {
        let max_chars: usize = self.get_max_characters();

        let mut new_message_vec: Vec<String> = Vec::new();

        for old_message in self.message.iter() {
            let mut new_message: String = String::new();

            for word in old_message.split_whitespace() {
                let word_len: usize = word.len();
                let message_len: usize = new_message.len();

                if word_len > max_chars {
                    if message_len > 0 {
                        let word_vec = split_word(word, max_chars - (message_len + 1), max_chars);
                        let mut word_iter = word_vec.iter();
                        new_message_vec.push(format!("{} {}", new_message, word_iter.next().unwrap()));
                        for continued_word in word_iter {
                            new_message_vec.push(continued_word.to_string());
                        }
                        new_message = new_message_vec.pop().unwrap();
                    } else {
                        new_message_vec.append(&mut split_word(word, max_chars, max_chars));
                    }
                } else if message_len + word_len > max_chars {
                    new_message_vec.push(new_message);
                    new_message = String::from(word);
                } else if message_len > 0 {
                    new_message = format!("{} {}", new_message, word);
                } else {
                    new_message = String::from(word);
                }
            }
            if !new_message.is_empty() { new_message_vec.push(new_message); }
        }
        self.message = new_message_vec;
    }

    // Determines the max number of characters based on window and font size.
    fn get_max_characters(&self) -> usize {
        ((self.window.window.size().width / self.font_size as f64) * 2.15) as usize
    }
}