Skip to content

Commit 7ea1155

Browse files
author
Noam Lewis
committed
basic load
1 parent 42b0e83 commit 7ea1155

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/main.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
extern crate crossterm;
22
extern crate ratatui;
3-
use std::{io, iter::FromIterator};
3+
use std::{
4+
fs::OpenOptions,
5+
io::{self, Read, Seek},
6+
iter::FromIterator,
7+
};
48

59
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
610
use ratatui::{
@@ -19,9 +23,33 @@ struct State {
1923
cursor: Position, // relative to the screen (or current view window), not to the whole file
2024
insert_mode: bool,
2125
status_text: String,
26+
file: Option<std::fs::File>,
27+
offset: u64,
2228
}
2329

2430
impl State {
31+
fn load(&mut self) -> io::Result<()> {
32+
if let Some(ref mut f) = self.file {
33+
f.seek(io::SeekFrom::Start(self.offset))?;
34+
let mut buf = [0; 1024 * 1024];
35+
f.read(&mut buf)?;
36+
37+
self.lines.clear();
38+
self.lines.push(vec![]);
39+
let mut y = 0;
40+
for byte in buf {
41+
let c: char = byte.into();
42+
if c == '\n' {
43+
y += 1;
44+
self.lines.push(vec![]);
45+
} else {
46+
self.lines[y].push(c);
47+
}
48+
}
49+
}
50+
Ok(())
51+
}
52+
2553
fn run(&mut self, mut terminal: DefaultTerminal) -> io::Result<()> {
2654
loop {
2755
self.render(&mut terminal)?;
@@ -327,13 +355,23 @@ impl State {
327355
}
328356

329357
fn main() -> io::Result<()> {
358+
let args: Vec<String> = std::env::args().collect();
359+
let file: Option<std::fs::File> = if args.len() > 1 {
360+
let filename = &args[1];
361+
Some(OpenOptions::new().read(true).open(filename)?)
362+
} else {
363+
None
364+
};
330365
let terminal = ratatui::init();
331366
let mut state: State = State {
332367
lines: vec![vec![]],
333368
cursor: Position::new(0, 0),
334369
insert_mode: true,
335370
status_text: String::new(),
371+
file: file,
372+
offset: 0,
336373
};
374+
state.load()?;
337375
let result = state.run(terminal);
338376
ratatui::restore();
339377
result

0 commit comments

Comments
 (0)