aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: dd20c3fef19e3648194ee3a59f4200b8c871b0f9 (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
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
mod rope;
mod term;

use rope::Rope;
use std::ffi::{OsStr, OsString};
use std::io::{Stdout, Write};
use std::path::Path;
use term::Key;

fn move_cursor(stdout: &mut Stdout, row: usize, col: usize) {
    let _ = write!(stdout, "\x1b[{};{}H", row + 1, col + 1);
    let _ = stdout.flush();
}

fn edit(file: &OsStr) -> Result<(), String> {
    let _raw_handle = match term::raw() {
        Some(h) => h,
        None => {
            return Err(String::from("cannot put terminal in raw mode"));
        }
    };

    let mut r = match Rope::open(Path::new(file)) {
        Ok(r) => r,
        Err(err) => {
            return Err(format!("open file {}: {}", file.to_string_lossy(), err));
        }
    };
    let size = match term::size() {
        Some(size) => size,
        None => {
            return Err(String::from("cannot get terminal size"));
        }
    };
    let mut stdout = std::io::stdout();
    let _ = stdout.write(b"\x1b[J");
    for i in 0..usize::from(size.ws_row) {
        if i > 0 {
            let _ = stdout.write(b"\r\n");
        }
        let line = match r.line(i) {
            Some(line) => line,
            None => {
                break;
            }
        };
        // TODO: handle line-wrapping.
        let _ = line.print(&mut stdout);
    }
    let _ = stdout.write(b"\x1b[H");
    let _ = stdout.flush();

    let mut cursor_row = 0;
    let mut cursor_col = 0;

    let mut stdin = std::io::stdin();
    loop {
        let c = match term::read_key(&mut stdin) {
            Ok(c) => c,
            Err(err) => {
                return Err(err);
            }
        };
        match c {
            Key::CtrlQ => {
                break;
            }
            Key::Up => {
                if cursor_row == 0 {
                    continue;
                }
                cursor_row -= 1;

                let len = r
                    .line(cursor_row)
                    .expect("cursor_row should always be valid")
                    .len();
                if cursor_col > len {
                    move_cursor(&mut stdout, cursor_row, len);
                } else {
                    move_cursor(&mut stdout, cursor_row, cursor_col);
                }
            }
            Key::Down => {
                if cursor_row == usize::from(size.ws_row - 1) || cursor_row == r.lines() {
                    continue;
                }
                cursor_row += 1;

                let len = r
                    .line(cursor_row)
                    .expect("cursor_row should always be valid")
                    .len();
                if cursor_col > len {
                    move_cursor(&mut stdout, cursor_row, len);
                } else {
                    move_cursor(&mut stdout, cursor_row, cursor_col);
                }
            }
            Key::Left => {
                let len = r
                    .line(cursor_row)
                    .expect("cursor_row should always be valid")
                    .len();
                cursor_col = std::cmp::min(cursor_col, len);
                if cursor_col == 0 {
                    continue;
                }
                cursor_col -= 1;
                move_cursor(&mut stdout, cursor_row, cursor_col);
            }
            Key::Right => {
                let len = r
                    .line(cursor_row)
                    .expect("cursor_row should always be valid")
                    .len();
                if cursor_col >= len {
                    continue;
                }
                cursor_col += 1;
                move_cursor(&mut stdout, cursor_row, cursor_col);
            }
            Key::Char(c) => {
                let line = r
                    .line(cursor_row)
                    .expect("cursor_row should always be valid");
                cursor_col = std::cmp::min(cursor_col, line.len());
                r = line.insert(cursor_col, c);
                move_cursor(&mut stdout, cursor_row, 0);
                let _ = r
                    .line(cursor_row)
                    .expect("cursor_row should always be valid")
                    .print(&mut stdout);
                cursor_col += 1;
                move_cursor(&mut stdout, cursor_row, cursor_col);
            }
        }
    }
    return Ok(());
}

fn main() {
    let args: Vec<OsString> = std::env::args_os().collect();
    if args.len() != 2 {
        println!("Usage: edit <filename>");
        std::process::exit(1);
    }
    if let Err(err) = edit(&args[1]) {
        println!("FAIL: {}", err);
        std::process::exit(1);
    }
}