summary refs log tree commit diff
path: root/src/main.rs
blob: ea151c845edeb2be1120316e91a25bf0eb7ba4a0 (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
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
#![forbid(unsafe_code)]
use crate::types::*;
use smol::prelude::*;

use smol::fs::File;
use smol::lock::RwLock;
use std::path::PathBuf;
use std::process::ExitCode;
use std::ops::Range;

mod encoding;
mod terminal;
mod types;


struct Ivy {
  terminal: RwLock<Terminal>,
  buffer: RwLock<Buffer>,
  window: RwLock<Window>,
  argument_files: Vec<PathBuf>,
}

struct Buffer {
  path: Option<PathBuf>,
  contents: RwLock<Vec<u8>>,
  lines: RwLock<Vec<usize>>,
  has_end_newline: RwLock<bool>,
}

struct Window {
  /* The traditional order of writing "row, column" is the opposite of the
   * traditional order of writing "x, y"; be mindful. An alternate approach
   * would use "x" and "y" for all the names, which might arguably reduce
   * confusion, but personally we find the ordering concern to be
   * aesthetically pleasing, a quiet reminder of how much humans care about
   * everything.
   */
  cursor_column: RwLock<usize>,
  cursor_row: RwLock<usize>,
}


fn main() -> ExitCode {
  smol::block_on(async {
    if let Err(e) = Ivy::new().await.run().await {
      println!("{:?}", e);
      ExitCode::FAILURE
    } else {
      ExitCode::SUCCESS
    }
  })
}


impl Ivy {
  async fn new() -> Self {
    Ivy {
      terminal: RwLock::new(Terminal::new().await),
      buffer: RwLock::new(Buffer::new().await),
      window: RwLock::new(Window::new().await),
      argument_files: Vec::new(),
    }
  }

  async fn run(mut self) -> Result<()> {
    /* The awkward structure here is because it's important that cleanup still
     * happen even if something fails in the middle.
     */
    let mut error = None;

    error = error.or(self.init().await.err());
    error = error.or(self.interact().await.err());
    error = error.or(self.zap().await.err());

    if let Some(error) = error {
      Err(error)
    } else {
      Ok(())
    }
  }

  async fn init(&mut self) -> Result<()> {
    self.init_arguments().await?;
    self.init_editor().await?;

    self.terminal.write().await.init_termios().await?;
    self.terminal.write().await.init_full_screen().await?;

    Ok(())
  }

  async fn zap(&mut self) -> Result<()> {
    let mut error = None;

    error = error.or(
        self.terminal.write().await.zap_full_screen().await.err());
    error = error.or(
        self.terminal.write().await.zap_termios().await.err());

    if let Some(error) = error {
      Err(error)
    } else {
      Ok(())
    }
  }

  async fn init_arguments(&mut self) -> Result<()> {
    self.argument_files.clear();

    let mut args = std::env::args();

    let _ = args.next();

    for argument in args {
      self.argument_files.push(argument.into());
    }

    Ok(())
  }

  async fn init_editor(&mut self) -> Result<()> {
    if !self.argument_files.is_empty() {
      let path = self.argument_files.remove(0);
      let new_buffer = Buffer::from_file(path).await?;
      *self.buffer.write().await = new_buffer;
    }

    Ok(())
  }

  async fn draw(&mut self) -> Result<()> {
    let mut terminal = self.terminal.write().await;
    let buffer = self.buffer.read().await;

    let height = *terminal.height.read().await;

    let mut screen_y = 0;
    for logical_y in 0 .. height - 1 {
      if let Some(span) = buffer.line_span(logical_y).await {
        if logical_y > 0 {
          terminal.stdout.write_all("\n".as_bytes()).await?;
        }

        terminal.stdout.write_all(&buffer.contents.read().await[span]).await?;

        screen_y = logical_y + 1;
      } else {
        break;
      }
    }

    for _ in screen_y .. height - 1 {
      terminal.stdout.write_all("\n~".as_bytes()).await?;
    }

    terminal.stdout.write_all("\n        status goes here".as_bytes()).await?;

    let window = self.window.read().await;
    terminal.do_cursor_position(*window.cursor_column.read().await,
                                *window.cursor_row.read().await).await?;
    terminal.stdout.flush().await?;

    Ok(())
  }

  async fn interact(&mut self) -> Result<()> {
    self.draw().await?;

    loop {
      let c = self.terminal.write().await.read_char().await?;

      match c {
        'h' => {
          self.handle_movement(async |ivy: &mut Ivy| {
            let window = ivy.window.write().await;

            let mut column = window.cursor_column.write().await;
            if *column > 0 {
              *column -= 1;
            }

            Ok(())
          }).await?;
        },

        'j' => {
          self.handle_movement(async |ivy: &mut Ivy| {
            let window = ivy.window.write().await;

            let mut row = window.cursor_row.write().await;
            *row += 1;

            Ok(())
          }).await?;
        },

        'k' => {
          self.handle_movement(async |ivy: &mut Ivy| {
            let window = ivy.window.write().await;

            let mut row = window.cursor_row.write().await;
            if *row > 0 {
              *row -= 1;
            }

            Ok(())
          }).await?;
        },

        'l' => {
          self.handle_movement(async |ivy: &mut Ivy| {
            let window = ivy.window.write().await;

            let mut column = window.cursor_column.write().await;
            *column += 1;

            Ok(())
          }).await?;
        },

        _ => {
          return Ok(());
        },
      }
    }
  }

  async fn handle_movement(&mut self,
                           movement: impl AsyncFn(&mut Ivy) -> Result<()>)
      -> Result<()>
  {
    let (old_row, old_column) = {
      let window = self.window.read().await;

      (*window.cursor_row.read().await,
       *window.cursor_column.read().await)
    };

    movement(self).await?;

    let (new_row, new_column) = {
      let window = self.window.read().await;

      (*window.cursor_row.read().await,
       *window.cursor_column.read().await)
    };

    if old_row != new_row || old_column != new_column {
      let mut terminal = self.terminal.write().await;

      terminal.do_cursor_position(new_column, new_row).await?;
      terminal.stdout.flush().await?;
    }

    Ok(())
  }
}


impl Buffer {
  pub async fn new() -> Self {
    Buffer {
      path: None,
      contents: RwLock::new("\n".to_string().into_bytes()),
      lines: RwLock::new(vec![0]),
      has_end_newline: RwLock::new(true),
    }
  }

  pub async fn from_file(path: PathBuf) -> Result<Self> {
    let mut file = File::open(&path).await?;

    let mut contents = Vec::new();
    let _ = file.read_to_end(&mut contents).await?;

    let mut result = Buffer {
      path: Some(path),
      contents: RwLock::new(contents),
      lines: RwLock::new(vec![0]),
      has_end_newline: RwLock::new(true),
    };

    result.count_lines().await;

    Ok(result)
  }

  pub async fn count_lines(&mut self) {
    let contents = self.contents.read().await;

    *self.lines.write().await = vec![0];

    if contents.len() == 0 {
      *self.has_end_newline.write().await = false;
    } else {
      let mut offset = 0;

      while offset < contents.len() {
        let c = contents[offset];

        if c == b'\n' {
          offset += 1;

          if offset < contents.len() {
            self.lines.write().await.push(offset);
          } else {
            *self.has_end_newline.write().await = true;
          }
        } else if offset + 1 == contents.len() {
          *self.has_end_newline.write().await = false;
        }

        offset += 1;
      }
    }
  }

  pub async fn line_span(&self, index: usize) -> Option<Range<usize>> {
    let lines = self.lines.read().await;

    if index < lines.len() {
      let start = lines[index];

      if index + 1 < lines.len() {
        let end = lines[index + 1] - 1;

        Some(start .. end)
      } else {
        let end = if *self.has_end_newline.read().await {
          self.contents.read().await.len() - 1
        } else {
          self.contents.read().await.len()
        };

        Some(start .. end)
      }
    } else {
      None
    }
  }
}


impl Window {
  pub async fn new() -> Self {
    Window {
      cursor_column: RwLock::new(0),
      cursor_row: RwLock::new(0),
    }
  }
}