summaryrefslogtreecommitdiffstats
path: root/src/git_interactive.rs
blob: a75e35007f204465f05a21fa2720c248d9236915 (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
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
use crate::commit::Commit;
use crate::list::{Action, Line};
use std::cmp;
use std::fs::File;
use std::io::Read;
use std::io::Write;
use std::path::PathBuf;

fn load_filepath(path: &PathBuf, config_comment_char: &str) -> Result<Vec<Line>, String> {
	let mut file = match File::open(&path) {
		Ok(file) => file,
		Err(why) => {
			return Err(format!("Error opening file, {}\nReason: {}", path.display(), why));
		},
	};

	let mut s = String::new();
	match file.read_to_string(&mut s) {
		Ok(_) => {},
		Err(why) => {
			return Err(format!("Error reading file, {}\nReason: {}", path.display(), why));
		},
	}
	let comment_char = if config_comment_char.eq("auto") {
		"#"
	}
	else {
		config_comment_char
	};

	// catch noop rebases
	s.lines()
		.filter(|l| !l.starts_with(comment_char) && !l.is_empty())
		.map(|l| {
			match Line::new(l) {
				Ok(line) => Ok(line),
				Err(e) => Err(format!("Error reading file, {}", e)),
			}
		})
		.collect()
}

pub struct GitInteractive {
	filepath: PathBuf,
	lines: Vec<Line>,
	selected_line_index: usize,
	visual_index_start: usize,
}

impl GitInteractive {
	pub fn new_from_filepath(filepath: &str, comment_char: &str) -> Result<Self, String> {
		let path = PathBuf::from(filepath);
		let lines = load_filepath(&path, comment_char)?;

		Ok(GitInteractive {
			filepath: path,
			lines,
			selected_line_index: 1,
			visual_index_start: 1,
		})
	}

	pub fn write_file(&self) -> Result<(), String> {
		let mut file = match File::create(&self.filepath) {
			Ok(file) => file,
			Err(why) => {
				return Err(format!(
					"Error opening file, {}\nReason: {}",
					self.filepath.display(),
					why
				));
			},
		};
		for line in self.lines.iter() {
			match writeln!(file, "{}", line.to_text()) {
				Ok(_) => {},
				Err(why) => {
					return Err(format!("Error writing to file, {}", why));
				},
			}
		}
		Ok(())
	}

	pub fn reload_file(&mut self, comment_char: &str) -> Result<(), String> {
		let lines = load_filepath(&self.filepath, comment_char)?;

		self.lines = lines;
		Ok(())
	}

	pub fn clear(&mut self) {
		self.lines.clear();
	}

	pub fn move_cursor_up(&mut self, amount: usize) {
		self.selected_line_index = match amount {
			a if a >= self.selected_line_index => 1,
			_ => self.selected_line_index - amount,
		};
	}

	pub fn move_cursor_down(&mut self, amount: usize) {
		self.selected_line_index = cmp::min(self.selected_line_index + amount, self.lines.len());
	}

	pub fn start_visual_mode(&mut self) {
		self.visual_index_start = self.selected_line_index;
	}

	#[allow(clippy::range_plus_one)]
	pub fn swap_visual_range_up(&mut self) {
		if self.selected_line_index == 1 || self.visual_index_start == 1 {
			return;
		}

		let range = if self.selected_line_index <= self.visual_index_start {
			self.selected_line_index..self.visual_index_start + 1
		}
		else {
			self.visual_index_start..self.selected_line_index + 1
		};

		for index in range {
			self.lines.swap(index - 1, index - 2);
		}
		self.visual_index_start -= 1;
		self.move_cursor_up(1);
	}

	pub fn swap_selected_up(&mut self) {
		if self.selected_line_index == 1 {
			return;
		}
		self.lines
			.swap(self.selected_line_index - 1, self.selected_line_index - 2);
		self.move_cursor_up(1);
	}

	#[allow(clippy::range_plus_one)]
	pub fn swap_visual_range_down(&mut self) {
		if self.selected_line_index == self.lines.len() || self.visual_index_start == self.lines.len() {
			return;
		}

		let range = if self.selected_line_index <= self.visual_index_start {
			self.selected_line_index..self.visual_index_start + 1
		}
		else {
			self.visual_index_start..self.selected_line_index + 1
		};

		for index in range.rev() {
			self.lines.swap(index - 1, index);
		}
		self.visual_index_start += 1;
		self.move_cursor_down(1);
	}

	pub fn swap_selected_down(&mut self) {
		if self.selected_line_index == self.lines.len() {
			return;
		}
		self.lines.swap(self.selected_line_index - 1, self.selected_line_index);
		self.move_cursor_down(1);
	}

	pub fn edit_selected_line(&mut self, content: &str) {
		self.lines[self.selected_line_index - 1].edit_content(content);
	}

	pub fn get_selected_line_edit_content(&self) -> &String {
		self.lines[self.selected_line_index - 1].get_edit_content()
	}

	#[allow(clippy::range_plus_one)]
	pub fn set_visual_range_action(&mut self, action: Action) {
		let range = if self.selected_line_index <= self.visual_index_start {
			self.selected_line_index..self.visual_index_start + 1
		}
		else {
			self.visual_index_start..self.selected_line_index + 1
		};

		for index in range {
			let selected_action = self.lines[index - 1].get_action();
			if *selected_action != Action::Exec && *selected_action != Action::Break {
				self.lines[index - 1].set_action(action);
			}
		}
	}

	pub fn set_selected_line_action(&mut self, action: Action) {
		let selected_action = self.lines[self.selected_line_index - 1].get_action();
		if *selected_action != Action::Exec && *selected_action != Action::Break {
			self.lines[self.selected_line_index - 1].set_action(action);
		}
	}

	pub fn toggle_break(&mut self) {
		let selected_action = self.lines[self.selected_line_index - 1].get_action();
		if *selected_action == Action::Break {
			self.lines.remove(self.selected_line_index - 1);
			if self.selected_line_index != 1 {
				self.selected_line_index -= 1;
			}
		}
		else {
			self.lines.insert(self.selected_line_index, Line::new_break());
			if self.selected_line_index != self.lines.len() {
				self.selected_line_index += 1;
			}
		}
	}

	pub fn load_commit_stats(&self) -> Result<Commit, String> {
		let selected_action = self.lines[self.selected_line_index - 1].get_action();
		if *selected_action != Action::Exec && *selected_action != Action::Break {
			return Ok(Commit::from_commit_hash(self.get_selected_line_hash().as_str())?);
		}
		Err(String::from("Cannot load commit for the selected action"))
	}

	pub fn is_noop(&self) -> bool {
		!self.lines.is_empty() && *self.lines[0].get_action() == Action::Noop
	}

	pub fn get_selected_line_hash(&self) -> &String {