-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,10 +60,14 @@ impl Editor { | |
} | ||
fn draw_welcome_message() -> Result<(), Error> { | ||
let mut welcome_message = format!("{NAME} editor -- version {VERSION}"); | ||
let width = Terminal::size()?.width as usize; | ||
let width = Terminal::size()?.width; | ||
let len = welcome_message.len(); | ||
let padding = (width - len) / 2; | ||
let spaces = " ".repeat(padding - 1); | ||
// we allow this since we don't care if our welcome message is put _exactly_ in the middle. | ||
// it's allowed to be a bit to the left or right. | ||
#[allow(clippy::integer_division)] | ||
let padding = (width.saturating_sub(len)) / 2; | ||
|
||
let spaces = " ".repeat(padding.saturating_sub(1)); | ||
welcome_message = format!("~{spaces}{welcome_message}"); | ||
welcome_message.truncate(width); | ||
Terminal::print(welcome_message)?; | ||
|
@@ -77,12 +81,15 @@ impl Editor { | |
let Size { height, .. } = Terminal::size()?; | ||
for current_row in 0..height { | ||
Terminal::clear_line()?; | ||
// we allow this since we don't care if our welcome message is put _exactly_ in the middle. | ||
// it's allowed to be a bit up or down | ||
This comment has been minimized.
Sorry, something went wrong. |
||
#[allow(clippy::integer_division)] | ||
if current_row == height / 3 { | ||
Self::draw_welcome_message()?; | ||
} else { | ||
Self::draw_empty_row()?; | ||
} | ||
if current_row + 1 < height { | ||
if current_row.saturating_add(1) < height { | ||
This comment has been minimized.
Sorry, something went wrong.
pflenker
Author
Owner
|
||
Terminal::print("\r\n")?; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,26 @@ | ||
use core::fmt::Display; | ||
use crossterm::cursor::{Hide, MoveTo, Show}; | ||
use crossterm::style::Print; | ||
use crossterm::terminal::{disable_raw_mode, enable_raw_mode, size, Clear, ClearType}; | ||
use crossterm::{queue, Command}; | ||
use std::io::{stdout, Error, Write}; | ||
use core::fmt::Display; | ||
|
||
#[derive(Copy, Clone)] | ||
pub struct Size { | ||
pub height: u16, | ||
pub width: u16, | ||
pub height: usize, | ||
pub width: usize, | ||
} | ||
#[derive(Copy, Clone)] | ||
pub struct Position { | ||
pub x: u16, | ||
pub y: u16, | ||
pub x: usize, | ||
pub y: usize, | ||
} | ||
/// Represents the Terminal. | ||
/// Edge Case for platforms where `usize` < `u16`: | ||
/// Regardless of the actual size of the Terminal, this representation | ||
/// only spans over at most `usize::MAX` or `u16::size` rows/columns, whichever is smaller. | ||
/// Each size returned truncates to min(`usize::MAX`, `u16::MAX`) | ||
/// And should you attempt to set the cursor out of these bounds, it will also be truncated. | ||
This comment has been minimized.
Sorry, something went wrong.
pflenker
Author
Owner
|
||
pub struct Terminal; | ||
|
||
impl Terminal { | ||
|
@@ -38,8 +44,13 @@ impl Terminal { | |
Self::queue_command(Clear(ClearType::CurrentLine))?; | ||
Ok(()) | ||
} | ||
/// Moves the cursor to the given Position. | ||
/// # Arguments | ||
/// * `Position` - the `Position`to move the cursor to. Will be truncated to `u16::MAX` if bigger. | ||
pub fn move_cursor_to(position: Position) -> Result<(), Error> { | ||
Self::queue_command(MoveTo(position.x, position.y))?; | ||
// clippy::as_conversions: See doc above | ||
#[allow(clippy::as_conversions, clippy::cast_possible_truncation)] | ||
Self::queue_command(MoveTo(position.x as u16, position.y as u16))?; | ||
Ok(()) | ||
} | ||
pub fn hide_cursor() -> Result<(), Error> { | ||
|
@@ -54,16 +65,26 @@ impl Terminal { | |
Self::queue_command(Print(string))?; | ||
Ok(()) | ||
} | ||
|
||
/// Returns the current size of this Terminal. | ||
/// Edge Case for systems with `usize` < `u16`: | ||
/// * A `Size` representing the terminal size. Any coordinate `z` truncated to `usize` if `usize` < `z` < `u16` | ||
This comment has been minimized.
Sorry, something went wrong.
pflenker
Author
Owner
|
||
pub fn size() -> Result<Size, Error> { | ||
let (width, height) = size()?; | ||
let (width_u16, height_u16) = size()?; | ||
// clippy::as_conversions: See doc above | ||
This comment has been minimized.
Sorry, something went wrong.
pflenker
Author
Owner
|
||
#[allow(clippy::as_conversions)] | ||
let height = height_u16 as usize; | ||
// clippy::as_conversions: See doc above | ||
This comment has been minimized.
Sorry, something went wrong.
pflenker
Author
Owner
|
||
#[allow(clippy::as_conversions)] | ||
let width = width_u16 as usize; | ||
Ok(Size { height, width }) | ||
} | ||
pub fn execute() -> Result<(), Error> { | ||
stdout().flush()?; | ||
Ok(()) | ||
} | ||
|
||
fn queue_command<T:Command>(command: T) -> Result<(), Error> { | ||
fn queue_command<T: Command>(command: T) -> Result<(), Error> { | ||
queue!(stdout(), command)?; | ||
Ok(()) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#![warn(clippy::all, clippy::pedantic, clippy::print_stdout)] | ||
#![warn(clippy::all, clippy::pedantic, clippy::print_stdout, clippy::arithmetic_side_effects, clippy::as_conversions, clippy::integer_division)] | ||
This comment has been minimized.
Sorry, something went wrong.
pflenker
Author
Owner
|
||
mod editor; | ||
use editor::Editor; | ||
|
||
|
These comments, starting with a
//
, are aimed at anyone reviewing the internals of this function. They are placed right on top of what they relate to.