girt-view 2.2.0

View modules for git-interactive-rebase-tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
mod render_action;

#[cfg(test)]
mod tests;

use std::{
	collections::{HashMap, VecDeque},
	mem,
};

pub(crate) use render_action::RenderAction;

use super::{scroll_position::ScrollPosition, LineSegment, ViewData, ViewLine};

#[derive(Debug)]
pub struct RenderSlice {
	actions: VecDeque<RenderAction>,
	height: usize,
	lines: Vec<ViewLine>,
	lines_count: usize,
	lines_leading_count: usize,
	lines_trailing_count: usize,
	padding_height: usize,
	scroll_position: ScrollPosition,
	scroll_position_cache: HashMap<String, ScrollPosition>,
	should_show_scrollbar: bool,
	show_help: bool,
	show_title: bool,
	version: u32,
	view_data_name: String,
	view_data_version: u32,
	width: usize,
}

impl RenderSlice {
	#[allow(clippy::new_without_default)]
	pub fn new() -> Self {
		Self {
			actions: VecDeque::new(),
			height: 0,
			lines: vec![],
			lines_count: 0,
			lines_leading_count: 0,
			lines_trailing_count: 0,
			padding_height: 0,
			scroll_position: ScrollPosition::new(),
			scroll_position_cache: HashMap::new(),
			should_show_scrollbar: false,
			show_help: false,
			show_title: false,
			version: 0,
			view_data_name: String::from(""),
			view_data_version: 0,
			width: 0,
		}
	}

	pub fn record_scroll_up(&mut self) {
		self.actions.push_back(RenderAction::ScrollUp);
	}

	pub fn record_scroll_down(&mut self) {
		self.actions.push_back(RenderAction::ScrollDown);
	}

	pub fn record_page_up(&mut self) {
		self.actions.push_back(RenderAction::PageUp);
	}

	pub fn record_page_down(&mut self) {
		self.actions.push_back(RenderAction::PageDown);
	}

	pub fn record_scroll_left(&mut self) {
		self.actions.push_back(RenderAction::ScrollLeft);
	}

	pub fn record_scroll_right(&mut self) {
		self.actions.push_back(RenderAction::ScrollRight);
	}

	pub fn record_scroll_top(&mut self) {
		self.actions.push_back(RenderAction::ScrollTop);
	}

	pub fn record_scroll_bottom(&mut self) {
		self.actions.push_back(RenderAction::ScrollBottom);
	}

	pub fn record_resize(&mut self, width: usize, height: usize) {
		self.actions.push_back(RenderAction::Resize(width, height));
	}

	pub fn sync_view_data(&mut self, view_data: &ViewData) {
		let cache_expired = self.cache_expired(view_data);
		// scroll position depends on padding, so if the view has changed it needs to be updated early
		if cache_expired {
			self.set_padding_height(view_data);
		}
		self.set_active_scroll_position(view_data);
		let has_actions = !self.actions.is_empty();
		while let Some(action) = self.actions.pop_front() {
			match action {
				RenderAction::ScrollDown => self.scroll_position.scroll_down(),
				RenderAction::ScrollUp => self.scroll_position.scroll_up(),
				RenderAction::ScrollRight => self.scroll_position.scroll_right(),
				RenderAction::ScrollLeft => self.scroll_position.scroll_left(),
				RenderAction::ScrollTop => self.scroll_position.scroll_top(),
				RenderAction::ScrollBottom => self.scroll_position.scroll_bottom(),
				RenderAction::PageUp => self.scroll_position.page_up(),
				RenderAction::PageDown => self.scroll_position.page_down(),
				RenderAction::Resize(width, height) => self.set_size(width, height),
			}
		}
		if has_actions || cache_expired {
			self.rebuild(view_data);
		}
	}

	pub(crate) const fn should_show_scroll_bar(&self) -> bool {
		self.should_show_scrollbar
	}

	#[allow(
		clippy::cast_precision_loss,
		clippy::cast_possible_truncation,
		clippy::cast_sign_loss,
		clippy::float_arithmetic
	)]
	pub(crate) fn get_scroll_index(&self) -> usize {
		if self.lines_count == 0 || self.scroll_position.get_top_position() == 0 {
			return 0;
		}

		let view_height = if self.padding_height < self.height {
			self.height - self.padding_height
		}
		else {
			0
		};

		if view_height <= 1 || view_height > self.lines_count {
			return 0;
		}

		// view_height >= 2
		// lines.len() > 2

		// if at bottom of list
		if self.scroll_position.get_top_position() >= self.lines_count - view_height {
			return view_height - 1;
		}

		if view_height <= 2 {
			return 0;
		}

		// 0 input range, if first and last item are pinned, so scroll center
		if self.lines_count - view_height <= 2 {
			return (0.5 * view_height as f64).round() as usize;
		}

		// linear range map from scroll range to view range. This only maps the range between the
		// first and last items, since those items are always returned as 0 or view_height
		let value = self.scroll_position.get_top_position() as f64;
		let input_start = 1.0;
		let input_end = (self.lines_count - view_height) as f64 - 1.0;
		let output_start = 1.0;
		let output_end = view_height as f64 - 2.0;
		let input_range = input_end - input_start;
		let output_range = output_end - output_start;
		let slope = output_range / input_range;
		slope.mul_add(value - input_start, output_start).round() as usize
	}

	pub(crate) const fn show_title(&self) -> bool {
		self.show_title
	}

	pub(crate) const fn show_help(&self) -> bool {
		self.show_help
	}

	pub(crate) const fn get_leading_lines_count(&self) -> usize {
		self.lines_leading_count
	}

	pub(crate) const fn get_trailing_lines_count(&self) -> usize {
		self.lines_trailing_count
	}

	pub(crate) const fn get_lines(&self) -> &Vec<ViewLine> {
		&self.lines
	}

	pub(crate) const fn get_version(&self) -> u32 {
		self.version
	}

	pub(super) const fn get_actions(&self) -> &VecDeque<RenderAction> {
		&self.actions
	}

	fn cache_expired(&self, view_data: &ViewData) -> bool {
		self.view_data_name != view_data.get_name() || self.view_data_version != view_data.get_version()
	}

	fn set_size(&mut self, view_width: usize, view_height: usize) {
		if self.height != view_height || self.width != view_width {
			self.height = view_height;
			self.width = view_width;
			self.update_scroll_position_size();
		}
	}

	fn set_padding_height(&mut self, view_data: &ViewData) {
		let padding_height = if view_data.show_title() { 1 } else { 0 }
			+ view_data.get_leading_lines().len()
			+ view_data.get_trailing_lines().len();

		if self.padding_height != padding_height {
			self.padding_height = padding_height;
		}
	}

	fn set_active_scroll_position(&mut self, view_data: &ViewData) {
		let name = view_data.get_name();
		if name != self.view_data_name {
			let previous_scroll_position = mem::replace(
				&mut self.scroll_position,
				self.scroll_position_cache
					.remove(&String::from(name))
					.unwrap_or_else(ScrollPosition::new),
			);
			let _ = self
				.scroll_position_cache
				.insert(String::from(self.view_data_name.as_str()), previous_scroll_position);
			let version = view_data.get_scroll_version();
			if self.scroll_position.get_version() != version || !view_data.retain_scroll_position() {
				self.scroll_position.reset();
				self.scroll_position.set_version(version);
			}
			self.update_scroll_position_size();
		}
	}

	fn update_scroll_position_size(&mut self) {
		self.scroll_position.resize(
			if self.height == 0 || self.padding_height > self.height {
				0
			}
			else {
				self.height - self.padding_height
			},
			if self.width == 0 {
				0
			}
			else {
				self.width - if self.should_show_scroll_bar() { 1 } else { 0 }
			},
		);
	}

	#[allow(clippy::cognitive_complexity, clippy::too_many_lines)]
	fn rebuild(&mut self, view_data: &ViewData) {
		let leading_lines_length = view_data.get_leading_lines().len();
		let trailing_lines_length = view_data.get_trailing_lines().len();
		let lines_length = view_data.get_lines().len();

		self.version += 1;
		self.view_data_name = String::from(view_data.get_name());
		self.view_data_version = view_data.get_version();
		self.show_title = view_data.show_title();
		self.show_help = view_data.show_help();
		self.should_show_scrollbar =
			self.padding_height < self.height && lines_length > (self.height - self.padding_height);

		self.scroll_position.set_lines_length(lines_length);
		if let Some(row) = view_data.get_visible_row().as_ref() {
			self.scroll_position.ensure_line_visible(*row);
		}

		let (leading_lines_end, max_leading_line_length) = if leading_lines_length == 0 {
			(0, 0)
		}
		else {
			// trailing lines have precedence over leading lines, title always has precedence
			let padding_height = if self.show_title { 1 } else { 0 } + trailing_lines_length;
			let available_height = if padding_height < self.height {
				self.height - padding_height
			}
			else {
				0
			};
			let leading_lines_end = if leading_lines_length < available_height {
				leading_lines_length
			}
			else {
				available_height
			};

			(
				leading_lines_end,
				Self::calculate_max_line_length(view_data.get_leading_lines(), 0, leading_lines_end),
			)
		};

		let (trailing_lines_end, max_trailing_line_length) = if trailing_lines_length == 0 {
			(0, 0)
		}
		else {
			// title always has precedence
			let padding_height = if self.show_title { 1 } else { 0 };
			let available_height = if padding_height < self.height {
				self.height - padding_height
			}
			else {
				0
			};

			let trailing_lines_end = if trailing_lines_length < available_height {
				trailing_lines_length
			}
			else {
				available_height
			};

			(
				trailing_lines_end,
				Self::calculate_max_line_length(view_data.get_trailing_lines(), 0, trailing_lines_end),
			)
		};

		let (lines_start, lines_end, max_line_length) = if lines_length == 0 {
			(0, 0, 0)
		}
		else {
			// all other lines take precedence over regular lines
			let available_height = if self.padding_height < self.height {
				self.height - self.padding_height
			}
			else {
				0
			};

			let lines_start = self.scroll_position.get_top_position();

			let lines_end = if lines_length < available_height {
				lines_length
			}
			else {
				available_height
			};

			let max_line_length = Self::calculate_max_line_length(view_data.get_lines(), lines_start, lines_end);
			(
				lines_start,
				lines_end,
				max_line_length + if self.should_show_scrollbar { 1 } else { 0 },
			)
		};

		self.lines_leading_count = leading_lines_end;
		self.lines_trailing_count = trailing_lines_end;
		self.lines_count = lines_length;
		self.scroll_position.set_max_line_length(
			max_line_length
				.max(max_leading_line_length)
				.max(max_trailing_line_length),
		);
		if let Some(column) = view_data.get_visible_column().as_ref() {
			self.scroll_position.ensure_column_visible(*column);
		}

		self.lines.clear();
		self.push_lines(view_data.get_leading_lines(), 0, leading_lines_end, false);
		self.push_lines(
			view_data.get_lines(),
			lines_start,
			lines_end,
			self.should_show_scrollbar,
		);
		self.push_lines(view_data.get_trailing_lines(), 0, trailing_lines_end, false);
	}

	fn calculate_max_line_length(view_lines: &[ViewLine], start: usize, length: usize) -> usize {
		view_lines
			.iter()
			.skip(start)
			.take(length)
			.fold(0, |longest, line| -> usize {
				if line.get_segments().len() <= line.get_number_of_pinned_segment() {
					longest
				}
				else {
					let sum = line.get_segments().iter().fold(0, |s, l| s + l.get_length());

					if sum > longest {
						sum
					}
					else {
						longest
					}
				}
			})
	}

	fn push_lines(&mut self, view_lines: &[ViewLine], start: usize, end: usize, scroll_bar: bool) {
		let window_width = if scroll_bar && self.width > 0 {
			self.width - 1
		}
		else {
			self.width
		};
		let left = self.scroll_position.get_left_position();

		view_lines.iter().skip(start).take(end).for_each(|line| {
			let mut cursor = 0;
			let mut left_start = 0;
			let mut segments = vec![];
			// window width can be zero when there is a scrollbar and a view width of 1
			if window_width > 0 {
				for (i, segment) in line.get_segments().iter().enumerate() {
					// set left on first non-pinned segment
					if i == line.get_number_of_pinned_segment() {
						left_start = left;
					}

					let partial = segment.get_partial_segment(left_start, window_width - cursor);

					if partial.get_length() > 0 {
						segments.push(LineSegment::new_with_color_and_style(
							partial.get_content(),
							segment.get_color(),
							segment.is_dimmed(),
							segment.is_underlined(),
							segment.is_reversed(),
						));

						cursor += partial.get_length();
						if cursor >= window_width {
							break;
						}
						left_start = 0;
					}
					else {
						left_start -= segment.get_length();
					}
				}

				if cursor < window_width {
					if let Some(padding) = line.get_padding().as_ref() {
						segments.push(LineSegment::new_with_color_and_style(
							padding.get_content().repeat(window_width - cursor).as_str(),
							padding.get_color(),
							padding.is_dimmed(),
							padding.is_underlined(),
							padding.is_reversed(),
						));
					}
				}
			}

			self.lines.push(
				ViewLine::new_with_pinned_segments(segments, line.get_number_of_pinned_segment())
					.set_selected(line.get_selected()),
			);
		});
	}
}