summaryrefslogtreecommitdiff
path: root/src/ui.rs
blob: e189ff01701ce05753a4b5aa32717bb2ca1b4b2f (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
use ncurses::*;

use server_int::Message;

#[derive(Debug)]
pub struct UI {
    input_x: i32,
    input_y: i32,
    output_x: i32,
    output_y: i32,
    chan: String,
    nick: String,
    buffer: String,
}

impl UI {
    pub fn new() -> UI {
        UI {
            input_x: 0,
            input_y: 0,
            output_x: 0,
            output_y: 0,
            chan: String::new(),
            nick: String::new(),
            buffer: String::new(),
        }
    }

    pub fn init() {
        initscr();
        keypad(stdscr(), true);
        nodelay(stdscr(), true);
    }

    pub fn close() {
        endwin();
    }

    pub fn nick(&mut self, nick: &str) {
        self.nick = format!("[{}] ", nick);
        let mut max_x = 0;
        let mut max_y = 0;
        getmaxyx(stdscr(), &mut max_y, &mut max_x);
        mv(max_y-1, 0);
        deleteln();
        printw(&self.nick);
        printw(" ");
        getyx(stdscr(), &mut self.input_y, &mut self.input_x);
    }

    pub fn put_str(&mut self, text: &str) {
        let mut max_x = 0;
        let mut max_y = 0;
        getmaxyx(stdscr(), &mut max_y, &mut max_x);

        if self.output_y >= max_y-1 {
            mv(1, 0);
            deleteln();
            self.output_y-=1;
            mv(self.output_y, 0);
        }

        printw(text);
        getyx(stdscr(), &mut self.output_y, &mut self.output_x);
    }

    pub fn read_line(&mut self) -> Option<String> {
        let mut max_x = 0;
        let mut max_y = 0;
        getmaxyx(stdscr(), &mut max_y, &mut max_x);
        mv(max_y-1, 0);
        printw(&self.nick);
        printw(&self.buffer);
        self.getstr_unblocking()
    }

    fn getstr_unblocking(&mut self) -> Option<String> {
        let mut new_x = 0;
        let mut new_y = 0;
        getyx(stdscr(), &mut new_y, &mut new_x);
        while let Some(WchResult::Char(wch)) = get_wch() {
            let ch = wch as u8 as char;
            match ch {
                '\n' => {
                    self.input_x = new_x;
                    self.input_y = new_y;
                    deleteln();
                    mv(new_y, new_x);
                    printw(&self.nick);
                    let ret = self.buffer.clone();
                    self.buffer.clear();
                    return Some(ret);
                },
                '\x7f' => {
                    self.buffer.pop();
                    if self.input_x > new_x {
                        self.input_x -= 1;
                        deleteln();
                        mv(new_y, 0);
                        printw(&self.nick);
                        printw(&self.buffer);
                    }
                }
                _ => {
                    self.buffer.push(ch);
                    //self.buffer.push_str((&format!("{:output_x}", ch as i32)));
                    self.input_x += 1;
                },
            }
        }
        None
    }

    pub fn print_message(&mut self, message: &Message) {
        match message.command.as_str() {
            "PRIVMSG" => {
                let prefix = message.prefix.clone().unwrap_or(String::new());
                let mut iter = prefix.split("!");
                let nick = iter.next().unwrap_or("");
                //let context = &message.params[0];
                let content = &message.params[1];
                self.put_str(&format!("<{}> {}\n", nick, content));
            },
            _ => {
                /*
                   let prefix = message.prefix.unwrap_or(String::new());
                   print!("{}: ", prefix);
                   self.print_params_2(message.params);
                   */
                let cmd = &message.command;
                {
                    let context = &message.params.get(0).unwrap_or(&String::new()).clone();
                    match message.prefix {
                        Some(ref prefix) =>
                            self.put_str(&format!("{}({}) -> {}: ", prefix, cmd, context)),
                        None =>
                            self.put_str(&format!("({}) -> {}: ", cmd, context)),
                    }
                }
                self.print_params_2(&message.params);
            },
        }
    }

    fn print_params_2(&mut self, params: &[String]) {
        let mut iter = params.iter().skip(1);
        while let Some(output_x) = iter.next() {
            self.put_str(&format!("{} ", output_x));
        }
        self.put_str("\n");
    }

}