Open a file and display the first line

This commit is contained in:
Timothy Warren 2019-08-27 12:22:19 -04:00
parent a67562b3a2
commit 4167088f81
2 changed files with 65 additions and 20 deletions

View File

@ -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,26 +244,35 @@ impl Editor {
fn draw_rows(&mut self) {
for y in 0..self.screen_rows {
if y == (self.screen_rows / 3) {
let mut welcome = format!("Kilo editor -- version {}", env!("CARGO_PKG_VERSION"));
if welcome.len() > self.screen_cols {
welcome.truncate(self.screen_cols)
}
if y >= self.num_rows {
if y == (self.screen_rows / 3) {
let mut welcome =
format!("Kilo editor -- version {}", env!("CARGO_PKG_VERSION"));
if welcome.len() > self.screen_cols {
welcome.truncate(self.screen_cols)
}
// Center welcome message
let mut padding = (self.screen_cols - welcome.len()) / 2;
if padding > 0 {
// Center welcome message
let mut padding = (self.screen_cols - welcome.len()) / 2;
if padding > 0 {
self.append_out("~");
padding -= 1;
}
while padding > 0 {
self.append_out(" ");
padding -= 1;
}
self.append_out(&welcome);
} else {
self.append_out("~");
padding -= 1;
}
while padding > 0 {
self.append_out(" ");
padding -= 1;
}
self.append_out(&welcome);
} else {
self.append_out("~");
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");
@ -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(())
}
}

View File

@ -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