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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// @adjivas - github.com/adjivas. See the LICENSE
// file at the top-level directory of this distribution and at
// https://github.com/adjivas/Neko
//
// This file may not be copied, modified, or distributed

//! # neko
//!
//! This library contains the module `graphic` and `dynamic`.

#![feature(range_contains)]
#![feature(slice_patterns)]
#![feature(untagged_unions)]
#![feature(advanced_slice_patterns)]
#![feature(result_unwrap_or_default)]

#![crate_type= "lib"]
#![cfg_attr(feature = "nightly", feature(plugin))]

#![feature(plugin)]
#![cfg_attr(feature = "clippy", plugin(clippy(conf_file="clippy.toml")))]

#![cfg_attr(feature = "lints", plugin(clippy))]
#![cfg_attr(feature = "lints", deny(warnings))]
#![cfg_attr(not(any(feature = "lints", feature = "nightly")), deny())]
#![deny(
        missing_debug_implementations,
        missing_copy_implementations,
        trivial_casts,
        trivial_numeric_casts,
        unused_import_braces,
        unused_qualifications
)]

#![doc(html_logo_url = "https://arukana.github.io/Neko/images/neko.png")]

//! ![uml](ml.svg)

extern crate pty_proc;
extern crate editeur;
extern crate git2;
extern crate toml;
extern crate libc;

#[macro_use]
/// The macros of crate.
mod macros;
/// The module `display` is a extension of pty-proc's display module.
pub mod display;
/// The module `prelude` is for public.
pub mod prelude;
/// The module `dynamic` is the compositer of extern libraries.
pub mod dynamic;

mod err;

use std::io::{self, Write};
use std::ops::{Not, BitAnd};
use std::fmt;
use std::char;

use dynamic::Compositer;
use dynamic::library::state::LibraryState;
use dynamic::library::state::Relative;
use dynamic::library::state::persona::{Position, Cardinal};

pub use display::Display;

pub use editeur::prelude as graphic;
pub use pty_proc::prelude as pty;
pub use pty_proc::parent::Parent;

pub use self::err::{NekoError, Result};

/// The first directory.
pub const SPEC_ROOT: &'static str = editeur::SPEC_ROOT;
/// The default first directory.
pub const SPEC_ROOT_DEFAULT: &'static str = editeur::SPEC_ROOT_DEFAULT;

pub type PtyDisplay = pty::Display;

/// The module `neko` is the first interface level.
pub struct Neko <T> where T: Parent {
    dynamic: Compositer,
    /// Overload of Display interface from Pty.
    screen: Display,
    /// Interface of Pseudo terminal.
    shell: T,
    /// Interface on a Sprite partition.
    graphic: editeur::Graphic,
    /// The current pid.
    pid: libc::pid_t,
    /// The current Command.
    line: io::Cursor<Vec<char>>,
}

impl <T> Neko<T> where T: Parent {
    /// The constructor method `new` returns a Neko interface for a Shell, 
    /// a Compositer of dynamic libraries and a dictionnary of sprite.
    pub fn new(
        repeat: Option<i64>,
        interval: Option<i64>,
        command: Option<&str>,
        windows: Option<pty::Winszed>,
    ) -> Result<Neko<pty::Shell>> {
        let dynamic: Compositer = try!(Compositer::new());
        let shell: pty::Shell = try!(pty::Shell::new(repeat, interval, command, windows));
        let graphic: editeur::Graphic = try!(editeur::Graphic::new());
        let pid = shell.get_pid();
        let size: pty::Winszed = *shell.get_window_size();
        let mut neko = Neko {
            screen: Display::from_window_size(&size),
            dynamic: dynamic,
            shell: shell,
            graphic: graphic,
            line: io::Cursor::new(Vec::new()),
            pid: pid,
        };
        neko.call();
        Ok(neko)
    }

    /// The constructor method `from_shell` returns a Neko interface for a Shell, 
    /// a Compositer of dynamic libraries and a dictionnary of sprite.
    pub fn from_shell(
        shell: T,
    ) -> Result<Neko<T>> {
        let dynamic: Compositer = try!(Compositer::new());
        let graphic: editeur::Graphic = try!(editeur::Graphic::new());
        let pid = shell.get_pid();
        let size: pty::Winszed = *shell.get_window_size();
        let mut neko = Neko {
            screen: Display::from_window_size(&size),
            dynamic: dynamic,
            shell: shell,
            graphic: graphic,
            line: io::Cursor::new(Vec::new()),
            pid: pid,
        };
        neko.call();
        Ok(neko)
    }

    /// The method `call` updates the Neko's Display for a LibraryState
    /// and Graphic Dictionary.
    fn call(&mut self) {
        let lib: &LibraryState = self.dynamic.get_state();

        self.screen.set_state(lib, &mut self.graphic)
    }

    pub fn from_graphic(
        graphic: editeur::Graphic,
        repeat: Option<i64>,
        interval: Option<i64>,
        command: Option<&str>,
        windows: Option<pty::Winszed>,
    ) -> Result<Neko<pty::Shell>> {
        let dynamic: Compositer = try!(Compositer::new());
        let shell: pty::Shell = try!(pty::Shell::new(repeat, interval, command, windows));
        let pid = shell.get_pid();

        let neko = Neko {
            screen: Display::default(),
            dynamic: dynamic,
            shell: shell,
            graphic: graphic,
            line: io::Cursor::new(Vec::new()),
            pid: pid,
        };
        Ok(neko)
    }

    /// The method `neko` runs a neko command for first level of shell.
    #[allow(unused_must_use)]
    fn neko(&mut self, key: pty::Key, state: &mut pty::ShellState) {
        if key.is_enter().bitand(
            self.pid.eq(&self.shell.get_pid())
        ) {
            match &self.line.get_ref()
                        .iter()
                        .cloned()
                        .collect::<String>()
                        .to_string()
                        .split_whitespace()
                        .collect::<Vec<&str>>()
                        .as_slice()[..] {
                &["neko", ref arguments..] => {
                    state.set_input_keyown('\u{3}');
                    match arguments {
                        &["debug"] => {
                            format_subneko!(self,
                                format!("{}\n{}",
                                    self.screen.get_persona(),
                                    self.screen.get_tooltip(),
                                ).as_bytes()
                            );
                        },
                        &["install", ref repository] => {
                            format_subneko!(self, repository, "install",
                                self.dynamic.install(repository)
                            );
                        },
                        &["uninstall", ref libraryname] => {
                            format_subneko!(self, libraryname, "uninstall",
                                self.dynamic.uninstall(libraryname)
                            );
                        },
                        &["mount", ref libraryname, ref priority] => {
                            format_subneko!(self, libraryname, "mount",
                                self.dynamic.mount(
                                    libraryname,
                                    priority.parse::<i64>().ok()
                                )
                            );
                        },
                        &["mount", ref libraryname] => {
                            format_subneko!(self, libraryname, "mount",
                                self.dynamic.mount(
                                    libraryname,
                                    None
                                )
                            );
                        },
                        &["unmount", ref libraryname] => {
                            format_subneko!(self, libraryname, "unmount",
                                self.dynamic.unmount(libraryname)
                            );
                        },
                        &["update", ref libraryname] => {
                            format_subneko!(self, libraryname, "update the library",
                                self.dynamic.update(libraryname)
                            );
                        },
                        &["persona", ref x, ref y] => {
                            match (x.parse::<u16>(), y.parse::<u16>()) {
                                (Ok(x), Ok(y)) => {
                                    self.dynamic.set_persona_position(
                                        Position::from([x, y])
                                    );
                                },
                                (Err(why), _) => {
                                    format_subneko_err!(
                                        self, x, "update the Persona's coordinate", why
                                    );
                                },
                                (_, Err(why)) => {
                                    format_subneko_err!(
                                        self, y, "update the Persona's coordinate", why
                                    );
                                },
                            }
                        },
                        &["persona", "UpperLeft"] => {
                            self.dynamic.set_persona_position(
                                Position::from(Cardinal::UpperLeft)
                            );
                        },
                        &["persona", "UpperMiddle"] => {
                            self.dynamic.set_persona_position(
                                Position::from(Cardinal::UpperMiddle)
                            );
                        },
                        &["persona", "UpperRight"] => {
                            self.dynamic.set_persona_position(
                                Position::from(Cardinal::UpperRight)
                            );
                        },
                        &["persona", "MiddleLeft"] => {
                            self.dynamic.set_persona_position(
                                Position::from(Cardinal::MiddleLeft)
                            );
                        },
                        &["persona", "MiddleCentral"] => {
                            self.dynamic.set_persona_position(
                                Position::from(Cardinal::MiddleCentral)
                            );
                        },
                        &["persona", "MiddleRight"] => {
                            self.dynamic.set_persona_position(
                                Position::from(Cardinal::MiddleRight)
                            );
                        },
                        &["persona", "LowerLeft"] => {
                            self.dynamic.set_persona_position(
                                Position::from(Cardinal::LowerLeft)
                            );
                        },
                        &["persona", "LowerMiddle"] => {
                            self.dynamic.set_persona_position(
                                Position::from(Cardinal::LowerMiddle)
                            );
                        },
                        &["persona", "LowerRight"] => {
                            self.dynamic.set_persona_position(
                                Position::from(Cardinal::LowerRight)
                            );
                        },
                        &["persona", ref sheetname] => {
                            match graphic::Sheet::new(sheetname) {
                                Ok(sheet) => {
                                    self.dynamic.set_persona_sheet(sheet);
                                },
                                Err(why) => {
                                    format_subneko_err!(
                                        self, sheetname, "update the Persona's sheet", why
                                    );
                                },
                            }
                        },
                        &["tooltip", "Top"] => {
                            self.dynamic.set_tooltip_cardinal(Relative::Top)
                        },
                        &["tooltip", "Bottom"] => {
                            self.dynamic.set_tooltip_cardinal(Relative::Bottom)
                        },
                        &["tooltip", "Right"] => {
                            self.dynamic.set_tooltip_cardinal(Relative::Right);
                        },
                        &["tooltip", "Left"] => {
                            self.dynamic.set_tooltip_cardinal(Relative::Left);
                        },
                        &["tooltip", ref text] => {
                            self.dynamic.set_tooltip_message(text.to_string());
                        },
                        _ => format_subneko!(self, b"The command's argument is unvalid."),
                    }
                },
                _ => {},
            };
            self.line.get_mut().clear();
            self.line.set_position(0);
        }
    }

    /// The method `line` updates the current command line of proccess.
    fn line(&mut self, key: pty::Key) {
        let position: usize = self.line.position() as usize;
        match key {
            key if key.is_left() => {
                self.line.set_position(position.checked_sub(1).unwrap_or_default() as u64);
            },
            key if key.is_right() => {
                let position: usize = position.checked_add(1).unwrap_or_default();
                if position < self.line.get_ref().len() {
                    self.line.set_position(position as u64);
                }
            },
            key if key.is_start_heading() => {
                self.line.set_position(0);
            },
            key if key.is_enquiry() => {
                let position: usize = self.line.get_ref().len().checked_sub(1).unwrap_or_default();
                self.line.set_position(position as u64);
            },
            key if key.is_backspace() => {
                let position: usize = self.line.position() as usize;
                if position != 0 {
                    let position: usize = position-1;
                    if position < self.line.get_ref().len() {
                        self.line.get_mut().remove(position);
                        self.line.set_position(position as u64);
                    }
                }
            },
            key if key.is_enter() => {},
            key if key.is_c0()  => {
                self.line.get_mut().clear();
                self.line.set_position(0);
            },
            pty::Key::Str(line) => unsafe {
                let position: usize = self.line.position() as usize;
                let glyphs: String = String::from_utf8_unchecked(
                    line.iter()
                          .filter(|c: &&u8| c.eq(&&b'\0').not())
                          .map(|c: &u8| *c)
                          .collect::<Vec<u8>>()
                );

                if let Some(count) = glyphs.len().checked_add(position) {
                    self.line.get_mut().extend(glyphs.chars());
                    self.line.set_position(count as u64);
                }
            },
            _ => {
                if let Some(glyph) = key.is_utf8() {
                    if position.eq(&self.line.get_ref().len()) {
                        self.line.get_mut().insert(position, glyph);
                        self.line.set_position(position.checked_add(1).unwrap_or_default() as u64);
                    }
                }
            },
        };
    }

    /// The accessor method `get_screen` returns a reference on the Display interface.
    pub fn get_screen(&self) -> (&pty::Display, &Display) {
        (self.shell.get_screen(), &self.screen)
    }
}

impl <T> Parent for Neko<T> where T: Parent {

    /// The accessor method `get_pid` returns the pid from the master.
    fn get_pid(&self) -> libc::pid_t {
        self.shell.get_pid()
    }

    /// The accessor method `get_speudo` returns the master interface.
    fn get_speudo(&self) -> &pty::Master {
        self.shell.get_speudo()
    }

    /// The accessor method `get_screen` returns a reference on the Display interface.
    fn get_screen(&self) -> &pty::Display {
        self.shell.get_screen()
    }

    /// The accessor method `get_window_size` returns a reference on the window size of
    /// terminal.
    fn get_window_size(&self) -> &pty::Winszed {
        self.shell.get_window_size()
    }

    /// The mutator method `set_window_size` redimentionnes the window
    /// with a default size.
    fn set_window_size(&mut self) {
        self.shell.set_window_size()
    }

    /// The mutator method `set_window_size_with` redimentionnes the window
    /// with a argument size.
    fn set_window_size_with(&mut self, size: &pty::Winszed) {
        self.shell.set_window_size_with(size);
    }

    /// The mutator method `write` set a buffer to the display
    /// without needing to print it
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        pty::Parent::write(&mut self.shell, buf)
    }

    fn next(&mut self, event: pty::DeviceState) -> pty::ShellState {
        pty::Parent::next(&mut self.shell, event)
    }
}

impl <T> Iterator for Neko<T> where T: Parent {
    type Item = pty::ShellState;

    fn next(&mut self) -> Option<pty::ShellState> {
        <T as Iterator>::next(&mut self.shell).and_then(|mut shell| {
            if let Some(&(pid, _)) = shell.is_task() {
                self.pid = pid;
            }
            if let Some(key) = shell.is_input_keydown() {
                self.line(key);
                self.neko(key, &mut shell);
            }
            if let Some(ref size) = shell.is_resized() {
                self.screen.set_window_size(size);
                self.dynamic.resized(size);
            }
            self.dynamic.call(&shell);
            self.call();
            Some(shell)
        })
    }
}

impl <T> Write for Neko<T> where T: Parent {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        if self.dynamic.get_state().is_locked().not() {
            <T as io::Write>::write(&mut self.shell, buf)
        } else {
            Ok(0)
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        if self.dynamic.get_state().is_locked().not() {
           self.shell.flush()
        } else {
            Ok(())
        }
    }
}

impl <T> fmt::Display for Neko<T> where T: Parent {
    /// The function `fmt` formats the value using
    /// the given formatter.
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut disp: String = String::new();
            
        self.shell.get_screen()
            .into_iter()
            .zip(self.screen.into_iter())
            .all(|(pty_character, character)| {
                if character.is_null().not() {
                    disp.push_str(format!("{}", character).as_str());
                    true
                } else {
                    disp.push_str(format!("{}", pty_character).as_str());
                    true
                }
            });
         write!(f, "{}", disp)
    }
}

impl <T> fmt::Debug for Neko<T> where T: Parent {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Neko {{ dynamic: {:?}, graphic: {:?}, display: {:?} }}",
               self.dynamic,
               self.graphic,
               self.screen)
    }
}

impl <T> Drop for Neko<T> where T: Parent {
    fn drop(&mut self) {
    }
}