Skip to content

Commit

Permalink
refactor(clippy): clippy::comparison_chain
Browse files Browse the repository at this point in the history
Signed-off-by: Hunar Roop Kahlon <[email protected]>
  • Loading branch information
kinggoesgaming committed Feb 20, 2024
1 parent fbdf0d2 commit 771a1cb
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{
collections::HashMap,
io::{stdout, Write},
mem,
time::Duration,
time::Duration, cmp,
};

use crossterm::{
Expand Down Expand Up @@ -1304,24 +1304,28 @@ impl Editor {
let viewport_center = self.vheight() / 2;
let distance_to_center = self.cy as isize - viewport_center as isize;

if distance_to_center > 0 {
// if distance > 0 we need to scroll up
let distance_to_center = distance_to_center.unsigned_abs();
if self.vtop > distance_to_center {
let new_vtop = self.vtop + distance_to_center;
self.vtop = new_vtop;
self.cy = viewport_center;
self.draw_viewport(buffer)?;
}
} else if distance_to_center < 0 {
// if distance < 0 we need to scroll down
let distance_to_center = distance_to_center.unsigned_abs();
let new_vtop = self.vtop.saturating_sub(distance_to_center);
let distance_to_go = self.vtop + distance_to_center;
if self.current_buffer().len() > distance_to_go && new_vtop != self.vtop {
self.vtop = new_vtop;
self.cy = viewport_center;
self.draw_viewport(buffer)?;
match distance_to_center.cmp(&0) {
cmp::Ordering::Less => {
// if distance < 0 we need to scroll down
let distance_to_center = distance_to_center.unsigned_abs();
let new_vtop = self.vtop.saturating_sub(distance_to_center);
let distance_to_go = self.vtop + distance_to_center;
if self.current_buffer().len() > distance_to_go && new_vtop != self.vtop {
self.vtop = new_vtop;
self.cy = viewport_center;
self.draw_viewport(buffer)?;
}
},
cmp::Ordering::Equal => {},
cmp::Ordering::Greater => {
// if distance > 0 we need to scroll up
let distance_to_center = distance_to_center.unsigned_abs();
if self.vtop > distance_to_center {
let new_vtop = self.vtop + distance_to_center;
self.vtop = new_vtop;
self.cy = viewport_center;
self.draw_viewport(buffer)?;
}
}
}
}
Expand Down

0 comments on commit 771a1cb

Please sign in to comment.