Skip to content

Commit 0bad550

Browse files
author
Stephan Dilly
committed
fix error when showing diff of giant file - trading error for dog slow drawing (closes #96)
1 parent 5aa5814 commit 0bad550

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

src/components/diff.rs

+24-25
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
};
99
use asyncgit::{hash, DiffLine, DiffLineType, FileDiff};
1010
use crossterm::event::Event;
11-
use std::{borrow::Cow, cmp, convert::TryFrom};
11+
use std::{borrow::Cow, cmp};
1212
use strings::commands;
1313
use tui::{
1414
backend::Backend,
@@ -30,11 +30,11 @@ struct Current {
3030
///
3131
pub struct DiffComponent {
3232
diff: FileDiff,
33-
scroll: u16,
33+
scroll: usize,
3434
current_height: u16,
3535
focused: bool,
3636
current: Current,
37-
selected_hunk: Option<u16>,
37+
selected_hunk: Option<usize>,
3838
queue: Queue,
3939
theme: Theme,
4040
}
@@ -100,18 +100,18 @@ impl DiffComponent {
100100
fn scroll(&mut self, scroll: ScrollType) -> Result<()> {
101101
let old = self.scroll;
102102

103-
let scroll_max = self.diff.lines.saturating_sub(1);
103+
let scroll_max = self.diff.lines.saturating_sub(1) as usize;
104104

105105
self.scroll = match scroll {
106106
ScrollType::Down => self.scroll.saturating_add(1),
107107
ScrollType::Up => self.scroll.saturating_sub(1),
108108
ScrollType::Home => 0,
109109
ScrollType::End => scroll_max,
110110
ScrollType::PageDown => self.scroll.saturating_add(
111-
self.current_height.saturating_sub(1),
111+
self.current_height.saturating_sub(1) as usize,
112112
),
113113
ScrollType::PageUp => self.scroll.saturating_sub(
114-
self.current_height.saturating_sub(1),
114+
self.current_height.saturating_sub(1) as usize,
115115
),
116116
};
117117

@@ -127,19 +127,19 @@ impl DiffComponent {
127127

128128
fn find_selected_hunk(
129129
diff: &FileDiff,
130-
line_selected: u16,
131-
) -> Result<Option<u16>> {
132-
let mut line_cursor = 0_u16;
130+
line_selected: usize,
131+
) -> Result<Option<usize>> {
132+
let mut line_cursor = 0_usize;
133133
for (i, hunk) in diff.hunks.iter().enumerate() {
134-
let hunk_len = u16::try_from(hunk.lines.len())?;
134+
let hunk_len = hunk.lines.len();
135135
let hunk_min = line_cursor;
136136
let hunk_max = line_cursor + hunk_len;
137137

138138
let hunk_selected =
139139
hunk_min <= line_selected && hunk_max > line_selected;
140140

141141
if hunk_selected {
142-
return Ok(Some(u16::try_from(i)?));
142+
return Ok(Some(i));
143143
}
144144

145145
line_cursor += hunk_len;
@@ -150,25 +150,23 @@ impl DiffComponent {
150150

151151
fn get_text(&self, width: u16, height: u16) -> Result<Vec<Text>> {
152152
let selection = self.scroll;
153-
let height_d2 = height / 2;
153+
let height_d2 = (height / 2) as usize;
154154
let min = self.scroll.saturating_sub(height_d2);
155-
let max = min + height;
155+
let max = min + height as usize;
156156

157157
let mut res = Vec::new();
158-
let mut line_cursor = 0_u16;
159-
let mut lines_added = 0_u16;
158+
let mut line_cursor = 0_usize;
159+
let mut lines_added = 0_usize;
160160

161161
for (i, hunk) in self.diff.hunks.iter().enumerate() {
162162
let hunk_selected =
163-
self.selected_hunk.map_or(false, |s| {
164-
s == u16::try_from(i).unwrap_or_default()
165-
});
163+
self.selected_hunk.map_or(false, |s| s == i);
166164

167-
if lines_added >= height {
165+
if lines_added >= height as usize {
168166
break;
169167
}
170168

171-
let hunk_len = u16::try_from(hunk.lines.len())?;
169+
let hunk_len = hunk.lines.len();
172170
let hunk_min = line_cursor;
173171
let hunk_max = line_cursor + hunk_len;
174172

@@ -193,6 +191,7 @@ impl DiffComponent {
193191
line_cursor += hunk_len;
194192
}
195193
}
194+
196195
Ok(res)
197196
}
198197

@@ -247,10 +246,10 @@ impl DiffComponent {
247246
}
248247

249248
fn hunk_visible(
250-
hunk_min: u16,
251-
hunk_max: u16,
252-
min: u16,
253-
max: u16,
249+
hunk_min: usize,
250+
hunk_max: usize,
251+
min: usize,
252+
max: usize,
254253
) -> bool {
255254
// full overlap
256255
if hunk_min <= min && hunk_max >= max {
@@ -269,7 +268,7 @@ impl DiffComponent {
269268

270269
fn add_hunk(&self) -> Result<()> {
271270
if let Some(hunk) = self.selected_hunk {
272-
let hash = self.diff.hunks[usize::from(hunk)].header_hash;
271+
let hash = self.diff.hunks[hunk].header_hash;
273272
self.queue
274273
.borrow_mut()
275274
.push_back(InternalEvent::AddHunk(hash));

0 commit comments

Comments
 (0)