about summary refs log tree commit diff stats
path: root/themes/whiteness
blob: ff7d370a73aa648895971b71196f9173c12c3858 (plain) (blame)
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
[colours]
bkgnd=white
titlebar=blue
titlebar.text=white
titlebar.brackets=white
titlebar.unencrypted=red
titlebar.encrypted=white
titlebar.untrusted=yellow
titlebar.trusted=white
titlebar.online=white
titlebar.offline=red
titlebar.away=white
titlebar.chat=white
titlebar.dnd=red
titlebar.xa=white
statusbar=blue
statusbar.text=white
statusbar.brackets=white
statusbar.active=megenta
statusbar.new=red
main.text=black
main.text.me=black
main.text.them=black
main.splash=black
main.time=black
input.text=black
subscribed=green
unsubscribed=red
otr.started.trusted=green
otr.started.untrusted=yellow
otr.ended=red
otr.trusted=green
otr.untrusted=yellow
online=green
away=cyan
chat=green
dnd=red
xa=cyan
offline=red
incoming=yellow
typing=yellow
gone=red
error=red
roominfo=yellow
roommention=yellow
me=black
them=black
roster.header=black
occupants.header=black
receipt.sent=red
ous revision' href='/gbmor/clinte/blame/src/posts.rs?h=v2.1.1&id=eca1e0ad03f3bf83f8548509291905363096f0af'>^
841e6a3 ^
2cb85a4 ^
896f987 ^
2cb85a4 ^

23a7db0 ^

8b3c06f ^
23a7db0 ^
8b3c06f ^
23a7db0 ^
2cb85a4 ^



bb327d3 ^










8b3c06f ^

fd2f770 ^
8b3c06f ^


bb327d3 ^
9b749b9 ^






bb327d3 ^
39894be ^

87bf08a ^
9b749b9 ^














bb327d3 ^




9b749b9 ^
bb327d3 ^

39894be ^






fd2f770 ^

39894be ^

8b3c06f ^
bb327d3 ^

8b3c06f ^


fd2f770 ^

8b3c06f ^



bb327d3 ^
fd2f770 ^
87bf08a ^
bb327d3 ^
87bf08a ^
bb327d3 ^

8b3c06f ^
87bf08a ^


bb327d3 ^


8b3c06f ^
bb327d3 ^
8b3c06f ^

87bf08a ^



8b3c06f ^
8b3c06f ^


bb327d3 ^
8b3c06f ^

87bf08a ^

fd2f770 ^
8b3c06f ^
87bf08a ^



bb327d3 ^
87bf08a ^
c3c1db6 ^







bb327d3 ^







d098a93 ^
bb327d3 ^
d098a93 ^
bb327d3 ^
d098a93 ^


8b3c06f ^
bb327d3 ^

8b3c06f ^


fd2f770 ^
99c5537 ^
fd2f770 ^
8b3c06f ^



bb327d3 ^
8b3c06f ^
bb327d3 ^

8b3c06f ^
bb327d3 ^
8b3c06f ^
bb327d3 ^
8b3c06f ^
bb327d3 ^
8b3c06f ^

bb327d3 ^

d098a93 ^
bb327d3 ^
d098a93 ^
87bf08a ^




























2cb85a4 ^














87bf08a ^
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

            
                
              
                 
                
 


                 












                                               
                                      

                                        
 



                                           
                                  

                     
                                       
 







                                          
 
                     
                                              

                     

                                        
            
                 
      
 



                                         










                                               

               
          


                               
                                       






                                
                                           

                                 
                                                          














                                                      




                                      
                

                              






                                                                                  

          

 
                                 

                                                       


                                                    

                                               



            
                   
 
                     
                            
 

                                               
 


                            


                                                          
                   
                              

     



                                               
                                      


                                            
                                              

                                 

                     
                                           
 



                                             
                                                      
 







                                        







                                           
 
                
 
               


          
                                 

                                                       


                                              
                                               
                   
                                 



            
                   
 

                                               
 
                                   
                   
                                                          
                   
                              

     

                          
 
          
 




























                                                         














                                                         
 
use std::io;

use crate::conf;
use crate::db;
use crate::error;
use crate::user;

#[cfg(not(test))]
use crate::ed;

// Make sure nobody encodes narsty characters
// into a message to negatively affect other
// users
fn str_to_utf8(str: &str) -> String {
    str.chars()
        .map(|c| {
            let mut buf = [0; 4];
            c.encode_utf8(&mut buf).to_string()
        })
        .collect::<String>()
}

// First handler for creating a new post.
pub fn create() -> error::Result<()> {
    println!();
    println!("Title of the new post: ");

    #[cfg(test)]
    let title = String::from("TEST_TITLE");

    #[cfg(not(test))]
    let mut title = String::new();

    #[cfg(not(test))]
    io::stdin().read_line(&mut title)?;

    let title = str_to_utf8(title.trim());
    let title = if title.len() > 30 {
        &title[..30]
    } else {
        &title
    };

    println!();

    #[cfg(not(test))]
    let body_raw = str_to_utf8(&ed::call(""));

    #[cfg(not(test))]
    let body = if body_raw.len() > 500 {
        &body_raw[..500]
    } else {
        &body_raw
    };

    #[cfg(test)]
    let body = String::from("TEST_BODY");

    let trimmed_body = body.trim();
    let user = &*user::NAME;

    let mut all = db::Posts::get_all(db::PATH);
    let new = db::Post {
        author: user.into(),
        title: title.to_string(),
        body: trimmed_body.to_string(),
    };

    all.append(new);
    all.write();

    println!();
    Ok(())
}

// Shows the most recent posts.
pub fn display() -> error::Result<()> {
    let args = &*conf::ARGS;
    let line_len = args
        .value_of("line")
        .unwrap_or_else(|| "80")
        .parse::<usize>()
        .unwrap_or_else(|_| 80);

    let all = db::Posts::get_all(db::PATH);

    let mut postvec = Vec::new();
    all.posts().iter().enumerate().for_each(|(id, post)| {
        let body = post
            .body
            .trim()
            .chars()
            .into_iter()
            .enumerate()
            .map(|(i, e)| {
                let i = i + 1;
                if line_len > 9 && i % line_len == 0 {
                    return format!("{}\n", e);
                }
                e.to_string()
            })
            .collect::<String>();

        let newpost = format!(
            "{}. {} -> by {}\n{}\n\n",
            id + 1,
            post.title.trim(),
            post.author,
            body
        );
        postvec.push(newpost);
    });

    for (i, e) in postvec.iter().enumerate() {
        if (postvec.len() > 14 && i >= postvec.len() - 15) || postvec.len() < 15 {
            print!("{}", e);
        }
    }

    Ok(())
}

// First handler to update posts.
pub fn update_handler(id: usize) -> error::Result<()> {
    let mut id_num_in = if id == 0 {
        println!();
        println!("ID number of your post to edit?");
        let mut id_num_in = String::new();
        io::stdin().read_line(&mut id_num_in)?;
        id_num_in.trim().parse()?
    } else {
        id
    };

    id_num_in -= 1;

    #[cfg(not(test))]
    let user = &*user::NAME;

    let mut all = db::Posts::get_all(db::PATH);
    let post = all.get(id_num_in);

    #[cfg(test)]
    let user = &post.author;

    if *user != post.author {
        println!();
        println!("Users don't match. Can't update post!");
        println!();
        std::process::exit(1);
    }

    #[cfg(test)]
    let new_title = String::from("TEST_TITLE");

    #[cfg(not(test))]
    let mut new_title = String::new();

    println!("Updating post {}", id_num_in);
    println!();
    println!("Current Title: {}", post.title);
    println!();
    println!("Enter new title:");

    #[cfg(not(test))]
    io::stdin().read_line(&mut new_title)?;

    #[cfg(test)]
    let body_raw = String::from("TEST_BODY");

    #[cfg(not(test))]
    let body_raw = str_to_utf8(&ed::call(&post.body));

    let body = if body_raw.len() > 500 {
        &body_raw[..500]
    } else {
        &body_raw
    };

    let trimmed_body = body.trim();

    all.replace(
        id_num_in,
        db::Post {
            author: user.into(),
            title: new_title,
            body: trimmed_body.to_string(),
        },
    );

    all.write();

    println!();
    Ok(())
}

// First handler to remove a post
pub fn delete_handler(id: usize) -> error::Result<()> {
    let mut id_num_in = if id == 0 {
        println!();
        println!("ID of the post to delete?");
        let mut id_num_in = String::new();
        io::stdin().read_line(&mut id_num_in)?;
        println!();
        id_num_in.trim().parse()?
    } else {
        id
    };

    id_num_in -= 1;

    let mut all = db::Posts::get_all(db::PATH);
    let post = all.get(id_num_in);

    if *user::NAME != post.author {
        println!();
        println!("Users don't match. Can't delete post!");
        println!();
        std::process::exit(1);
    }

    all.delete(id_num_in);
    all.write();

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::fs;

    #[test]
    fn test_str_to_utf8() {
        let lhs = "foobar";
        let rhs = str_to_utf8(lhs);
        assert_eq!(lhs, rhs);
    }

    #[test]
    fn display_doesnt_explode() {
        assert!(display().is_ok());
    }

    #[test]
    fn test_update_handler() {
        fs::copy(db::PATH, "clinte_bak.json").unwrap();
        update_handler(1).unwrap();
        let all = db::Posts::get_all(db::PATH);
        let post = all.get(0);
        assert_eq!(post.title, "TEST_TITLE");
        assert_eq!(post.body, "TEST_BODY");
        fs::rename("clinte_bak.json", db::PATH).unwrap();
    }

    #[test]
    fn test_create_delete() {
        fs::copy(db::PATH, "clinte_bak.json").unwrap();
        create().unwrap();
        let all = db::Posts::get_all(db::PATH);
        let post = all.get(1);

        assert_eq!(post.title, "TEST_TITLE");
        assert_eq!(post.body, "TEST_BODY");

        delete_handler(2).unwrap();

        fs::rename("clinte_bak.json", db::PATH).unwrap();
    }
}