summaryrefslogtreecommitdiffstats
path: root/src/process/window_size_error.rs
blob: 8d9db11b9cd627401cbba78aa5d054ba842b4d92 (plain)
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
use lazy_static::lazy_static;

use crate::{
	input::{Event, EventHandler, InputOptions},
	process::{ProcessModule, ProcessResult, State},
	todo_file::TodoFile,
	view::{RenderContext, ViewData, ViewLine, ViewSender},
};

const HEIGHT_ERROR_MESSAGE: &str = "Window too small, increase height to continue";
const SHORT_ERROR_MESSAGE: &str = "Window too small";
const SIZE_ERROR_MESSAGE: &str = "Size!";

lazy_static! {
	static ref INPUT_OPTIONS: InputOptions = InputOptions::new().movement(true).resize(false);
}

pub struct WindowSizeError {
	return_state: State,
	view_data: ViewData,
}

impl ProcessModule for WindowSizeError {
	fn activate(&mut self, _: &TodoFile, previous_state: State) -> ProcessResult {
		self.return_state = previous_state;
		ProcessResult::new()
	}

	fn build_view_data(&mut self, context: &RenderContext, _: &TodoFile) -> &ViewData {
		let view_width = context.width();
		let message = if !context.is_minimum_view_width() {
			if view_width >= SHORT_ERROR_MESSAGE.len() {
				SHORT_ERROR_MESSAGE
			}
			else {
				// not much to do if the window gets too narrow
				SIZE_ERROR_MESSAGE
			}
		}
		else if view_width >= HEIGHT_ERROR_MESSAGE.len() {
			HEIGHT_ERROR_MESSAGE
		}
		else {
			// this message will always be safe here
			SHORT_ERROR_MESSAGE
		};

		self.view_data.update_view_data(|updater| {
			updater.clear();
			updater.push_line(ViewLine::from(message));
		});
		&mut self.view_data
	}

	fn handle_events(&mut self, event_handler: &EventHandler, _: &ViewSender, _: &mut TodoFile) -> ProcessResult {
		let event = event_handler.read_event(&INPUT_OPTIONS, |event, _| event);
		let mut result = ProcessResult::from(event);

		if let Event::Resize(width, height) = event {
			let render_context = RenderContext::new(width, height);
			if !render_context.is_window_too_small() {
				result = result.state(self.return_state);
			}
		}

		result
	}
}

impl WindowSizeError {
	pub fn new() -> Self {
		Self {
			return_state: State::List,
			view_data: ViewData::new(|_| {}),
		}
	}
}

#[cfg(test)]
mod tests {
	use rstest::rstest;

	use super::*;
	use crate::{
		assert_process_result,
		assert_rendered_output,
		process::testutil::{process_module_test, TestContext},
	};

	const MINIMUM_WINDOW_HEIGHT: usize = 5;
	const MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH: usize = 45;

	#[rstest(
		width, height, expected,
		case::width_too_small_long_message(SHORT_ERROR_MESSAGE.len(), MINIMUM_WINDOW_HEIGHT + 1, "Window too small"),
		case::width_too_small_short_message(SHORT_ERROR_MESSAGE.len() - 1, MINIMUM_WINDOW_HEIGHT + 1, "Size!"),
		case::height_too_small_long_message(
			MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH, MINIMUM_WINDOW_HEIGHT, "Window too small, increase height to continue"
		),
		case::height_too_small_short_message(
			MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH - 1, MINIMUM_WINDOW_HEIGHT, "Window too small"
		)
	)]
	#[allow(clippy::cast_possible_wrap)]
	fn build_view_data(width: usize, height: usize, expected: &str) {
		process_module_test(&[], &[], |mut test_context: TestContext<'_>| {
			test_context.render_context.update(width as u16, height as u16);
			let mut module = WindowSizeError::new();
			let view_data = test_context.build_view_data(&mut module);
			assert_rendered_output!(view_data, "{BODY}", format!("{{Normal}}{}", expected));
		});
	}

	#[test]
	fn event_resize_window_still_small() {
		process_module_test(&[], &[Event::Resize(1, 1)], |mut test_context: TestContext<'_>| {
			let mut module = WindowSizeError::new();
			test_context.activate(&mut module, State::ConfirmRebase);
			assert_process_result!(test_context.handle_event(&mut module), event = Event::Resize(1, 1));
		});
	}

	#[test]
	fn event_resize_window_no_longer_too_small() {
		process_module_test(&[], &[Event::Resize(100, 100)], |mut test_context: TestContext<'_>| {
			let mut module = WindowSizeError::new();
			test_context.activate(&mut module, State::ConfirmRebase);
			assert_process_result!(
				test_context.handle_event(&mut module),
				event = Event::Resize(100, 100),
				state = State::ConfirmRebase
			);
		});
	}

	#[test]
	fn event_other_character() {
		process_module_test(&[], &[Event::from('a')], |mut test_context: TestContext<'_>| {
			let mut module = WindowSizeError::new();
			test_context.activate(&mut module, State::ConfirmRebase);
			assert_process_result!(test_context.handle_event(&mut module), event = Event::from('a'));
		});
	}
}