Compare commits

..

No commits in common. "a5ce49576b73bfb1ffb3ae5c4345034967575e9d" and "295982f6e6e01e316f58666e187fd7e03b236c1c" have entirely different histories.

3 changed files with 37 additions and 35 deletions

View file

@ -2,6 +2,7 @@ use crossterm::{
cursor::{self, MoveTo},
event::{self, Event, KeyCode, KeyEvent, KeyModifiers},
queue,
style::{Color, Colors, ResetColor, SetColors},
terminal::{self, Clear, ClearType},
};
use std::{
@ -14,7 +15,7 @@ use std::{
};
use crate::clipboard::Clipboard;
use crate::util::{color_highlight, color_reset, read_line};
use crate::util::read_line;
const TAB_SIZE: usize = 4;
@ -169,22 +170,25 @@ impl Editor {
let end = (self.scroll + max_rows).min(self.lines.len());
let visible_rows = self.scroll..end;
let selection = self.selection().unwrap_or_default();
let cursor = self.char_index();
let marker = self.marker.unwrap_or(0);
let selection = (marker.min(cursor))..(marker.max(cursor));
for (line_index, line) in self.lines[visible_rows].iter().enumerate() {
let text = &self.text[line.clone()];
queue!(stdout(), MoveTo(0, line_index as u16)).unwrap();
if self.marker.is_none() {
print!("{}", text.replace('\t', &" ".repeat(TAB_SIZE)));
} else {
let mut in_selection = false;
for (i, char) in text.char_indices() {
let char_i = line.start + i;
if selection.contains(&char_i) {
if !in_selection {
color_highlight();
if char_i >= selection.start && char_i <= selection.end && !in_selection {
color_selection();
in_selection = true;
}
} else if in_selection {
} else if char_i > selection.end && in_selection {
color_reset();
in_selection = false;
}
@ -196,6 +200,7 @@ impl Editor {
}
color_reset();
}
}
self.status_line();
queue!(
stdout(),
@ -203,8 +208,7 @@ impl Editor {
self.physical_column() as u16,
(self.cursor.line - self.scroll) as u16
),
cursor::Show,
cursor::SetCursorStyle::BlinkingBar
cursor::Show
)
.unwrap();
stdout().flush().unwrap();
@ -455,3 +459,11 @@ impl Editor {
}
}
}
fn color_selection() {
queue!(stdout(), SetColors(Colors::new(Color::Black, Color::White))).unwrap();
}
fn color_reset() {
queue!(stdout(), ResetColor).unwrap();
}

View file

@ -2,6 +2,7 @@ use crossterm::{
cursor::{self, MoveTo},
event::{self, Event, KeyCode, KeyModifiers},
execute, queue,
style::{Color, Colors, ResetColor, SetColors},
terminal::{
self, disable_raw_mode, enable_raw_mode, Clear, ClearType, EnterAlternateScreen,
LeaveAlternateScreen,
@ -17,7 +18,6 @@ use std::{
mod clipboard;
mod editor;
mod util;
use crate::util::{color_highlight, color_reset};
use clipboard::Clipboard;
use editor::Editor;
@ -97,11 +97,11 @@ impl Navigator {
for (index, editor) in self.editors.iter().enumerate() {
if index == self.selected {
color_highlight();
queue!(stdout(), SetColors(Colors::new(Color::Black, Color::White))).unwrap();
}
queue!(stdout(), MoveTo(1, index as u16 + 1)).unwrap();
print!("{}", editor.title());
color_reset();
queue!(stdout(), ResetColor).unwrap();
}
let offset = self.editors.len() as u16 + 2;
@ -114,7 +114,7 @@ impl Navigator {
for (index, path) in self.files[visible_rows].iter().enumerate() {
if index + self.scroll == self.selected.wrapping_sub(self.editors.len()) {
color_highlight();
queue!(stdout(), SetColors(Colors::new(Color::Black, Color::White))).unwrap();
}
queue!(stdout(), MoveTo(1, index as u16 + 1 + offset)).unwrap();
if let Some(name) = path.file_name() {
@ -125,7 +125,7 @@ impl Navigator {
if path.is_dir() {
print!("/");
}
color_reset();
queue!(stdout(), ResetColor).unwrap();
}
if let Some(text) = &self.message {

View file

@ -1,9 +1,7 @@
use crossterm::{
cursor,
event::{self, Event, KeyCode},
queue,
style::{Color, Colors, ResetColor, SetColors},
terminal,
queue, terminal,
};
use std::io::{stdout, Write};
@ -39,11 +37,3 @@ pub fn read_line(prompt: &str) -> Option<String> {
}
Some(response.trim().into())
}
pub fn color_highlight() {
queue!(stdout(), SetColors(Colors::new(Color::Black, Color::White))).unwrap();
}
pub fn color_reset() {
queue!(stdout(), ResetColor).unwrap();
}