Open a file and display the first line
This commit is contained in:
parent
a67562b3a2
commit
4167088f81
@ -2,12 +2,24 @@
|
||||
use crate::helpers::*;
|
||||
|
||||
use std::cmp::PartialEq;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufReader;
|
||||
|
||||
use self::EditorKey::*;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct EditorRow {
|
||||
row_chars: String,
|
||||
chars: String,
|
||||
}
|
||||
|
||||
impl EditorRow {
|
||||
pub fn new(str: &str) -> Self {
|
||||
EditorRow {
|
||||
chars: str.to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Main structure for the editor
|
||||
@ -48,8 +60,6 @@ impl EditorKey<char> {
|
||||
}
|
||||
}
|
||||
|
||||
use self::EditorKey::*;
|
||||
|
||||
// init
|
||||
impl Editor {
|
||||
pub fn new() -> Self {
|
||||
@ -57,6 +67,7 @@ impl Editor {
|
||||
let size = instance.get_window_size();
|
||||
instance.cursor_x = 0;
|
||||
instance.cursor_y = 0;
|
||||
instance.num_rows = 0;
|
||||
instance.screen_cols = size.cols as usize;
|
||||
instance.screen_rows = size.rows as usize;
|
||||
|
||||
@ -233,8 +244,10 @@ impl Editor {
|
||||
|
||||
fn draw_rows(&mut self) {
|
||||
for y in 0..self.screen_rows {
|
||||
if y >= self.num_rows {
|
||||
if y == (self.screen_rows / 3) {
|
||||
let mut welcome = format!("Kilo editor -- version {}", env!("CARGO_PKG_VERSION"));
|
||||
let mut welcome =
|
||||
format!("Kilo editor -- version {}", env!("CARGO_PKG_VERSION"));
|
||||
if welcome.len() > self.screen_cols {
|
||||
welcome.truncate(self.screen_cols)
|
||||
}
|
||||
@ -254,6 +267,13 @@ impl Editor {
|
||||
} else {
|
||||
self.append_out("~");
|
||||
}
|
||||
} else {
|
||||
let mut output = self.row.chars.clone();
|
||||
if output.len() > self.screen_cols {
|
||||
output.truncate(self.screen_cols);
|
||||
}
|
||||
self.append_out(&output);
|
||||
}
|
||||
|
||||
self.append_out("\x1b[K");
|
||||
if y < (self.screen_rows - 1) {
|
||||
@ -286,5 +306,22 @@ impl Editor {
|
||||
|
||||
// File I/O
|
||||
impl Editor {
|
||||
pub fn open(&mut self) {}
|
||||
pub fn open(&mut self, filename: &str) -> io::Result<()> {
|
||||
let file = File::open(filename)?;
|
||||
let buf_reader = BufReader::new(file);
|
||||
|
||||
let mut lines = buf_reader.lines().map(|l| l.unwrap());
|
||||
|
||||
let line = lines.next().unwrap();
|
||||
self.row = EditorRow::new(&line);
|
||||
self.num_rows = 1;
|
||||
|
||||
/*for line in lines {
|
||||
let
|
||||
}*/
|
||||
// let line_res = buf_reader.read_line()
|
||||
// self.row = EditorRow::new("Hello, world!");
|
||||
// self.num_rows += 1;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -4,10 +4,13 @@ mod helpers;
|
||||
use crate::editor::Editor;
|
||||
use crate::helpers::*;
|
||||
use libc::STDIN_FILENO;
|
||||
use std::env;
|
||||
use std::io::Error;
|
||||
use std::result::Result;
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
// Save original terminal flags
|
||||
let original_termios = get_termios(STDIN_FILENO);
|
||||
|
||||
@ -17,6 +20,11 @@ fn main() -> Result<(), Error> {
|
||||
// Initialize the editor
|
||||
let mut editor = Editor::new();
|
||||
|
||||
// Open the file if specified, from the command line
|
||||
if args.len() >= 2 {
|
||||
editor.open(&args[1])?;
|
||||
}
|
||||
|
||||
// Main input loop. Editor::process_keypress uses an Option Enum as a sentinel.
|
||||
// `None` is returned on a quit action, in other cases, `Some(())` is returned,
|
||||
// continuing the loop
|
||||
|
Loading…
Reference in New Issue
Block a user