Skip to content

Commit

Permalink
Reduce size
Browse files Browse the repository at this point in the history
- screen size / position: (u16, u16)
- tabstop: u8
- indent size: u8
- repeat count: u16
  • Loading branch information
gwenn committed Oct 27, 2024
1 parent bdc2f60 commit 376a6fc
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 62 deletions.
20 changes: 10 additions & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub struct Config {
/// Whether to use stdio or not
behavior: Behavior,
/// Horizontal space taken by a tab.
tab_stop: usize,
tab_stop: u8,
/// Indentation size for indent/dedent commands
indent_size: usize,
indent_size: u8,
/// Check if cursor position is at leftmost before displaying prompt
check_cursor_position: bool,
/// Bracketed paste on unix platform
Expand Down Expand Up @@ -160,11 +160,11 @@ impl Config {
///
/// By default, 8.
#[must_use]
pub fn tab_stop(&self) -> usize {
pub fn tab_stop(&self) -> u8 {
self.tab_stop
}

pub(crate) fn set_tab_stop(&mut self, tab_stop: usize) {
pub(crate) fn set_tab_stop(&mut self, tab_stop: u8) {
self.tab_stop = tab_stop;
}

Expand All @@ -180,11 +180,11 @@ impl Config {
///
/// By default, 2.
#[must_use]
pub fn indent_size(&self) -> usize {
pub fn indent_size(&self) -> u8 {
self.indent_size
}

pub(crate) fn set_indent_size(&mut self, indent_size: usize) {
pub(crate) fn set_indent_size(&mut self, indent_size: u8) {
self.indent_size = indent_size;
}

Expand Down Expand Up @@ -433,7 +433,7 @@ impl Builder {
///
/// By default, `8`
#[must_use]
pub fn tab_stop(mut self, tab_stop: usize) -> Self {
pub fn tab_stop(mut self, tab_stop: u8) -> Self {
self.set_tab_stop(tab_stop);
self
}
Expand All @@ -451,7 +451,7 @@ impl Builder {
///
/// By default, `2`
#[must_use]
pub fn indent_size(mut self, indent_size: usize) -> Self {
pub fn indent_size(mut self, indent_size: u8) -> Self {
self.set_indent_size(indent_size);
self
}
Expand Down Expand Up @@ -568,7 +568,7 @@ pub trait Configurer {
/// Horizontal space taken by a tab.
///
/// By default, `8`
fn set_tab_stop(&mut self, tab_stop: usize) {
fn set_tab_stop(&mut self, tab_stop: u8) {
self.config_mut().set_tab_stop(tab_stop);
}

Expand All @@ -581,7 +581,7 @@ pub trait Configurer {
/// Indentation size for indent/dedent commands
///
/// By default, `2`
fn set_indent_size(&mut self, size: usize) {
fn set_indent_size(&mut self, size: u8) {
self.config_mut().set_indent_size(size);
}

Expand Down
6 changes: 3 additions & 3 deletions src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl<H: Helper> State<'_, '_, H> {
let prompt_size = self.prompt_size;
let no_previous_hint = self.hint.is_none();
self.hint();
let width = ch.width().unwrap_or(0);
let width = u16::try_from(ch.width().unwrap_or(0)).unwrap();
if n == 1
&& width != 0 // Ctrl-V + \t or \n ...
&& self.layout.cursor.col + width < self.out.get_columns()
Expand Down Expand Up @@ -382,7 +382,7 @@ impl<H: Helper> State<'_, '_, H> {
pub fn edit_replace_char(&mut self, ch: char, n: RepeatCount) -> Result<()> {
self.changes.begin();
let succeed = if let Some(chars) = self.line.delete(n, &mut self.changes) {
let count = chars.graphemes(true).count();
let count = u16::try_from(chars.graphemes(true).count()).unwrap();
self.line.insert(ch, count, &mut self.changes);
self.line.move_backward(1);
true
Expand Down Expand Up @@ -732,7 +732,7 @@ impl<H: Helper> State<'_, '_, H> {
}

/// Change the indentation of the lines covered by movement
pub fn edit_indent(&mut self, mvt: &Movement, amount: usize, dedent: bool) -> Result<()> {
pub fn edit_indent(&mut self, mvt: &Movement, amount: u8, dedent: bool) -> Result<()> {
if self.line.indent(mvt, amount, dedent, &mut self.changes) {
self.refresh_line()
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{Config, EditMode};
use crate::{Event, EventContext, EventHandler};

/// The number of times one command should be repeated.
pub type RepeatCount = usize;
pub type RepeatCount = u16;

/// Commands
#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -183,7 +183,9 @@ impl Cmd {
let last_insert = wrt.last_insert();
if let Movement::ForwardChar(0) = mvt {
Self::Replace(
Movement::ForwardChar(last_insert.as_ref().map_or(0, String::len)),
Movement::ForwardChar(
u16::try_from(last_insert.as_ref().map_or(0, String::len)).unwrap(),
),
last_insert,
)
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::cmp::Ordering;

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Position {
pub col: usize, // The leftmost column is number 0.
pub row: usize, // The highest row is number 0.
pub col: u16, // The leftmost column is number 0.
pub row: u16, // The highest row is number 0.
}

impl PartialOrd for Position {
Expand Down
17 changes: 9 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,16 @@ fn page_completions<C: Candidate, H: Helper>(
cols,
candidates
.iter()
.map(|s| s.display().width())
.map(|s| u16::try_from(s.display().width()).unwrap())
.max()
.unwrap()
+ min_col_pad,
);
let num_cols = cols / max_width;
let nbc = u16::try_from(candidates.len()).unwrap();

let mut pause_row = s.out.get_rows() - 1;
let num_rows = candidates.len().div_ceil(num_cols);
let num_rows = nbc.div_ceil(num_cols);
let mut ab = String::new();
for row in 0..num_rows {
if row == pause_row {
Expand Down Expand Up @@ -334,15 +335,15 @@ fn page_completions<C: Candidate, H: Helper>(
ab.clear();
for col in 0..num_cols {
let i = (col * num_rows) + row;
if i < candidates.len() {
let candidate = &candidates[i].display();
let width = candidate.width();
if i < nbc {
let candidate = &candidates[i as usize].display();
let width = u16::try_from(candidate.width()).unwrap();
if let Some(highlighter) = s.highlighter() {
ab.push_str(&highlighter.highlight_candidate(candidate, CompletionType::List));
} else {
ab.push_str(candidate);
}
if ((col + 1) * num_rows) + row < candidates.len() {
if ((col + 1) * num_rows) + row < nbc {
for _ in width..max_width {
ab.push(' ');
}
Expand Down Expand Up @@ -776,7 +777,7 @@ impl<H: Helper, I: History> Editor<H, I> {
cmd,
Cmd::AcceptLine | Cmd::Newline | Cmd::AcceptOrInsertLine { .. }
) {
self.term.cursor = s.layout.cursor.col;
self.term.cursor = s.layout.cursor.col as usize;
}

// Execute things can be done solely on a state object
Expand Down Expand Up @@ -894,7 +895,7 @@ impl<H: Helper, I: History> Editor<H, I> {

/// If output stream is a tty, this function returns its width and height as
/// a number of characters.
pub fn dimensions(&mut self) -> Option<(usize, usize)> {
pub fn dimensions(&mut self) -> Option<(u16, u16)> {
if self.term.is_output_tty() {
let out = self.term.create_writer();
Some((out.get_columns(), out.get_rows()))
Expand Down
10 changes: 7 additions & 3 deletions src/line_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl LineBuffer {
}
self.buf[self.pos..]
.grapheme_indices(true)
.take(n)
.take(usize::from(n))
.last()
.map(|(i, s)| i + self.pos + s.len())
}
Expand All @@ -216,7 +216,7 @@ impl LineBuffer {
self.buf[..self.pos]
.grapheme_indices(true)
.rev()
.take(n)
.take(usize::from(n))
.last()
.map(|(i, _)| i)
}
Expand All @@ -231,6 +231,7 @@ impl LineBuffer {
n: RepeatCount,
cl: &mut C,
) -> Option<bool> {
let n = usize::from(n);
let shift = ch.len_utf8() * n;
if self.must_truncate(self.buf.len() + shift) {
return None;
Expand All @@ -257,6 +258,7 @@ impl LineBuffer {
n: RepeatCount,
cl: &mut C,
) -> Option<bool> {
let n = usize::from(n);
let shift = text.len() * n;
if text.is_empty() || self.must_truncate(self.buf.len() + shift) {
return None;
Expand Down Expand Up @@ -682,6 +684,7 @@ impl LineBuffer {
}

fn search_char_pos(&self, cs: CharSearch, n: RepeatCount) -> Option<usize> {
let n = usize::from(n);
let mut shift = 0;
let search_result = match cs {
CharSearch::Backward(c) | CharSearch::BackwardAfter(c) => self.buf[..self.pos]
Expand Down Expand Up @@ -1083,7 +1086,7 @@ impl LineBuffer {
pub fn indent<C: ChangeListener>(
&mut self,
mvt: &Movement,
amount: usize,
amount: u8,
dedent: bool,
cl: &mut C,
) -> bool {
Expand All @@ -1108,6 +1111,7 @@ impl LineBuffer {
Movement::LineUp(n) => self.n_lines_up(n),
Movement::LineDown(n) => self.n_lines_down(n),
};
let amount = usize::from(amount);
let (start, end) = pair.unwrap_or((self.pos, self.pos));
let start = self.buf[..start].rfind('\n').map_or(0, |pos| pos + 1);
let end = self.buf[end..]
Expand Down
10 changes: 5 additions & 5 deletions src/tty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ pub trait Renderer {
/// Update the number of columns/rows in the current terminal.
fn update_size(&mut self);
/// Get the number of columns in the current terminal.
fn get_columns(&self) -> usize;
fn get_columns(&self) -> u16;
/// Get the number of rows in the current terminal.
fn get_rows(&self) -> usize;
fn get_rows(&self) -> u16;
/// Check if output supports colors.
fn colors_enabled(&self) -> bool;

Expand All @@ -119,7 +119,7 @@ pub trait Renderer {
}

// ignore ANSI escape sequence
fn width(s: &str, esc_seq: &mut u8) -> usize {
fn width(s: &str, esc_seq: &mut u8) -> u16 {
if *esc_seq == 1 {
if s == "[" {
// CSI
Expand All @@ -145,7 +145,7 @@ fn width(s: &str, esc_seq: &mut u8) -> usize {
} else if s == "\n" {
0
} else {
s.width()
u16::try_from(s.width()).unwrap()
}
}

Expand All @@ -168,7 +168,7 @@ pub trait Term {
fn new(
color_mode: ColorMode,
behavior: Behavior,
tab_stop: usize,
tab_stop: u8,
bell_style: BellStyle,
enable_bracketed_paste: bool,
enable_signals: bool,
Expand Down
8 changes: 4 additions & 4 deletions src/tty/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Renderer for Sink {

fn calculate_position(&self, s: &str, orig: Position) -> Position {
let mut pos = orig;
pos.col += s.len();
pos.col += u16::try_from(s.len()).unwrap();
pos
}

Expand All @@ -136,11 +136,11 @@ impl Renderer for Sink {

fn update_size(&mut self) {}

fn get_columns(&self) -> usize {
fn get_columns(&self) -> u16 {
80
}

fn get_rows(&self) -> usize {
fn get_rows(&self) -> u16 {
24
}

Expand Down Expand Up @@ -183,7 +183,7 @@ impl Term for DummyTerminal {
fn new(
color_mode: ColorMode,
_behavior: Behavior,
_tab_stop: usize,
_tab_stop: u8,
bell_style: BellStyle,
_enable_bracketed_paste: bool,
_enable_signals: bool,
Expand Down
Loading

0 comments on commit 376a6fc

Please sign in to comment.