summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: b167af90c14ac61741fac8639bf8c1755d1dea4e (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
extern crate pancurses;

use pancurses::*;

use std::env;

// open.rs
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

const COLOR_TABLE: [i16; 8] = [
	COLOR_WHITE,
	COLOR_YELLOW,
	COLOR_BLUE,
	COLOR_GREEN,
	COLOR_CYAN,
	COLOR_MAGENTA,
	COLOR_RED,
	COLOR_BLACK
];

// Prints each argument on a separate line
fn main() {
	let args: Vec<String> = env::args().collect();
	
	// Create a path to the desired file
	let path = Path::new(&args[1]);
	let display = path.display();
	
	// Open the path in read-only mode, returns `io::Result<File>`
	let mut file = match File::open(&path) {
		Err(why) => panic!("couldn't open {}: {}", display, why.description()),
		Ok(file) => file,
	};
	
	// Read the file contents into a string, returns `io::Result<usize>`
	let mut s = String::new();
	match file.read_to_string(&mut s) {
		Err(why) => panic!("couldn't read {}: {}", display, why.description()),
		Ok(_) => {},
	}
	
	let mut v: Vec<Vec<&str>> = s
		.lines()
		.filter(|l| !l.starts_with("#") && !l.is_empty())
		.map(|l| l.splitn(3, " ").collect())
		.collect();
	
	let v_len: i16 = v.len() as i16;
	
	/* Setup ncurses. */
	let window = initscr();
	window.refresh();
	window.keypad(true);
	noecho();
	
	if has_colors() {
		start_color();
	}

	use_default_colors();
	for (i, color) in COLOR_TABLE.into_iter().enumerate() {
		init_pair(i as i16, *color, -1);
	}
	
	let mut selected_line: i16 = 0;
	
	loop {
		window.clear();
		window.addstr("git interactive rebase tool\n\n");
		let mut i: i16 = 0;
		for ss in &v {
			if ss[0] == "pick" {
				window.attrset(COLOR_PAIR(0));
			} else if ss[0] == "reword" {
				window.attrset(COLOR_PAIR(1));
			} else if ss[0] == "edit" {
				window.attrset(COLOR_PAIR(2));
			} else if ss[0] == "squash" {
				window.attrset(COLOR_PAIR(4));
			} else if ss[0] == "fixup" {
				window.attrset(COLOR_PAIR(5));
			} else if ss[0] == "exec" {
				window.attrset(COLOR_PAIR(6));
			} else if ss[0] == "drop" {
				window.attrset(COLOR_PAIR(7));
			}
			if i == selected_line {
				window.attron(A_STANDOUT);
			}
			window.addstr(&format!("{:6}", &ss[0]).as_ref());
			window.addstr(&format!(" {} {}\n", &ss[1], &ss[2]).as_ref());
			window.attroff(A_STANDOUT);
			i += 1;
		}
		window.addstr("\n\n");
		window.addstr("Up - Move selection up, Down - Move selection down\n");
		window.addstr("j - Move selected line up, k - Move selected line down\n");
		window.addstr("(p)ick, (r)eword, (e)dit, (s)quash, (f)ixup, e(x)ec, (d)rop\n");
		window.refresh();
		match window.getch() {
			Some(Input::Character(q)) if q == 'q' || q == 'Q' => {
				curs_set(1);
				endwin();
				break;
			},
			Some(Input::Character(c)) if c == 'p' || c == 'P' => {
				v[selected_line as usize].remove(0);
				v[selected_line as usize].insert(0, "pick");
			},
			Some(Input::Character(c)) if c == 'r' || c == 'R' => {
				v[selected_line as usize].remove(0);
				v[selected_line as usize].insert(0, "reword");
			},
			Some(Input::Character(c)) if c == 'e' || c == 'E' => {
				v[selected_line as usize].remove(0);
				v[selected_line as usize].insert(0, "edit");
			},
			Some(Input::Character(c)) if c == 's' || c == 'S' => {
				v[selected_line as usize].remove(0);
				v[selected_line as usize].insert(0, "squash");
			},
			Some(Input::Character(c)) if c == 'f' || c == 'F' => {
				v[selected_line as usize].remove(0);
				v[selected_line as usize].insert(0, "fixup");
			},
			Some(Input::Character(c)) if c == 'x' || c == 'X' => {
				v[selected_line as usize].remove(0);
				v[selected_line as usize].insert(0, "exec");
			},
			Some(Input::Character(c)) if c == 'd' || c == 'D' => {
				v[selected_line as usize].remove(0);
				v[selected_line as usize].insert(0, "drop");
			},
			Some(Input::Character(c)) if c == 'd' || c == 'D' => {
				v[selected_line as usize].remove(0);
				v[selected_line as usize].insert(0, "drop");
			},
			Some(Input::Character(c)) if c == 'j' || c == 'J' => {
				if selected_line != 0 {
					v.swap(selected_line as usize, selected_line as usize - 1);
					selected_line -= 1;
				}
			},
			Some(Input::Character(c)) if c == 'k' || c == 'K' => {
				if selected_line != v_len - 1 {
					v.swap(selected_line as usize, selected_line as usize + 1);
					selected_line += 1;
				}
			},
			Some(Input::KeyUp) => {
				selected_line -= 1;
				if selected_line < 0 {
					selected_line = 0;
				}
			},
			Some(Input::KeyDown) => {
				selected_line += 1;
				if selected_line > v_len - 1 {
					selected_line = v_len - 1;
				}
			}
			_ => {}
		}
	}
	
	// Create a path to the desired file
//	let outfilepath = Path::new("git-rebase-output.txt");
//	let outfiledisplay = path.display();

	// Open a file in write-only mode, returns `io::Result<File>`
	let mut outfile = match File::create(path) {
		Err(why) => panic!("couldn't create {}: {}", display, why.description()),
		Ok(outfile) => outfile,
	};
	
	for ss in &v {
		if let Err(e) = writeln!(outfile, "{} {} {}", &ss[0], &ss[1], &ss[2]) {
			println!("{}", e);
		}
	}
}