Display hello world
This commit is contained in:
parent
d9ed5c631a
commit
7e8493410f
@ -3,4 +3,20 @@ use crate::Row;
|
|||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Document {
|
pub struct Document {
|
||||||
rows: Vec<Row>,
|
rows: Vec<Row>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Document {
|
||||||
|
pub fn open() -> Self {
|
||||||
|
let mut rows = Vec::new();
|
||||||
|
rows.push(Row::from("Hello, World!"));
|
||||||
|
Self { rows }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn row(&self, index: usize) -> Option<&Row> {
|
||||||
|
self.rows.get(index)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.rows.is_empty()
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
use crate::Document;
|
use crate::Document;
|
||||||
|
use crate::Row;
|
||||||
use crate::Terminal;
|
use crate::Terminal;
|
||||||
use termion::event::Key;
|
use termion::event::Key;
|
||||||
|
|
||||||
@ -36,7 +37,7 @@ impl Editor {
|
|||||||
Self {
|
Self {
|
||||||
should_quit: false,
|
should_quit: false,
|
||||||
terminal: Terminal::default().expect("Failed to initialize terminal"),
|
terminal: Terminal::default().expect("Failed to initialize terminal"),
|
||||||
document: Document::default(),
|
document: Document::open(),
|
||||||
cursor_position: Position::default(),
|
cursor_position: Position::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -112,13 +113,22 @@ impl Editor {
|
|||||||
println!("{}\r", welcome_message);
|
println!("{}\r", welcome_message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn draw_row(&self, row: &Row) {
|
||||||
|
let start = 0;
|
||||||
|
let end = self.terminal.size().width as usize;
|
||||||
|
let row = row.render(start, end);
|
||||||
|
println!("{}\r", row);
|
||||||
|
}
|
||||||
|
|
||||||
fn draw_rows(&self) {
|
fn draw_rows(&self) {
|
||||||
let height = self.terminal.size().height;
|
let height = self.terminal.size().height;
|
||||||
|
|
||||||
for row in 0..height - 1 {
|
for terminal_row in 0..height - 1 {
|
||||||
Terminal::clear_current_line();
|
Terminal::clear_current_line();
|
||||||
|
|
||||||
if row == height / 3 {
|
if let Some(row) = self.document.row(terminal_row as usize) {
|
||||||
|
self.draw_row(row);
|
||||||
|
} else if self.document.is_empty() && terminal_row == height / 3 {
|
||||||
self.draw_welcome_message();
|
self.draw_welcome_message();
|
||||||
} else {
|
} else {
|
||||||
println!("~\r");
|
println!("~\r");
|
||||||
|
18
src/row.rs
18
src/row.rs
@ -1,3 +1,21 @@
|
|||||||
|
use std::cmp;
|
||||||
|
|
||||||
pub struct Row {
|
pub struct Row {
|
||||||
string: String,
|
string: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for Row {
|
||||||
|
fn from(slice: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
string: String::from(slice),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Row {
|
||||||
|
pub fn render(&self, start: usize, end: usize) -> String {
|
||||||
|
let end = cmp::min(end, self.string.len());
|
||||||
|
let start = cmp::min(start, end);
|
||||||
|
self.string.get(start..end).unwrap_or_default().to_string()
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user