-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathmod.rs
196 lines (178 loc) · 5.93 KB
/
mod.rs
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
use crate::{state::State, Res};
use ratatui::{backend::Backend, prelude::Terminal};
use std::{borrow::Cow, fmt::Display};
use strum::EnumIter;
use tui_prompts::prelude::Status;
pub(crate) mod checkout;
pub(crate) mod commit;
pub(crate) mod discard;
pub(crate) mod editor;
pub(crate) mod fetch;
pub(crate) mod log;
pub(crate) mod pull;
pub(crate) mod push;
pub(crate) mod rebase;
pub(crate) mod show_refs;
pub(crate) trait OpTrait<B: Backend> {
fn trigger(&self, state: &mut State, term: &mut Terminal<B>) -> Res<()>;
fn format_prompt(&self, _state: &State) -> Cow<'static, str> {
unimplemented!()
}
fn prompt_update(
&self,
_status: Status,
_state: &mut State,
_term: &mut Terminal<B>,
) -> Res<()> {
unimplemented!()
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Op {
Quit,
Refresh,
ToggleSection(editor::ToggleSection),
SelectNext(editor::SelectNext),
SelectPrevious(editor::SelectPrevious),
HalfPageUp(editor::HalfPageUp),
HalfPageDown(editor::HalfPageDown),
Checkout(checkout::Checkout),
CheckoutNewBranch(checkout::CheckoutNewBranch),
Commit(commit::Commit),
CommitAmend(commit::CommitAmend),
FetchAll(fetch::FetchAll),
LogCurrent(log::LogCurrent),
Pull(pull::Pull),
Push(push::Push),
RebaseAbort(rebase::RebaseAbort),
RebaseContinue(rebase::RebaseContinue),
ShowRefs(show_refs::ShowRefs),
Submenu(SubmenuOp),
Target(TargetOp),
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum SubmenuOp {
Any,
Branch,
Commit,
Fetch,
Help,
Log,
None,
Pull,
Push,
Rebase,
Reset,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, EnumIter)]
pub(crate) enum TargetOp {
CommitFixup,
Discard(discard::Discard),
LogOther,
RebaseAutosquash,
RebaseInteractive,
ResetSoft,
ResetMixed,
ResetHard,
Show,
Stage,
Unstage,
}
impl Op {
pub fn implementation<B: Backend>(self) -> Box<dyn OpTrait<B>> {
match self {
Op::ToggleSection(op_trait) => Box::new(op_trait),
Op::SelectNext(op_trait) => Box::new(op_trait),
Op::SelectPrevious(op_trait) => Box::new(op_trait),
Op::HalfPageUp(op_trait) => Box::new(op_trait),
Op::HalfPageDown(op_trait) => Box::new(op_trait),
Op::Checkout(op_trait) => Box::new(op_trait),
Op::CheckoutNewBranch(op_trait) => Box::new(op_trait),
Op::Commit(op_trait) => Box::new(op_trait),
Op::CommitAmend(op_trait) => Box::new(op_trait),
Op::FetchAll(op_trait) => Box::new(op_trait),
Op::LogCurrent(op_trait) => Box::new(op_trait),
Op::Pull(op_trait) => Box::new(op_trait),
Op::Push(op_trait) => Box::new(op_trait),
Op::RebaseAbort(op_trait) => Box::new(op_trait),
Op::RebaseContinue(op_trait) => Box::new(op_trait),
Op::ShowRefs(op_trait) => Box::new(op_trait),
Op::Target(TargetOp::Discard(op_trait)) => Box::new(op_trait),
_ => unimplemented!(),
}
}
}
impl<B: Backend> OpTrait<B> for Op {
fn trigger(&self, state: &mut State, term: &mut Terminal<B>) -> Res<()> {
self.implementation::<B>().trigger(state, term)?;
Ok(())
}
fn format_prompt(&self, state: &State) -> Cow<'static, str> {
self.implementation::<B>().format_prompt(state)
}
fn prompt_update(&self, status: Status, arg: &mut State, term: &mut Terminal<B>) -> Res<()> {
self.implementation::<B>()
.prompt_update(status, arg, term)?;
Ok(())
}
}
impl Display for Op {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Op::Checkout(_) => "Checkout branch/revision",
Op::CheckoutNewBranch(_) => "Checkout new branch",
Op::Commit(_) => "Commit",
Op::CommitAmend(_) => "Commit amend",
Op::FetchAll(_) => "Fetch all",
Op::HalfPageDown(_) => "Half page down",
Op::HalfPageUp(_) => "Half page up",
Op::LogCurrent(_) => "Log current",
Op::Pull(_) => "Pull",
Op::Push(_) => "Push",
Op::Quit => "Quit",
Op::RebaseAbort(_) => "Rebase abort",
Op::RebaseContinue(_) => "Rebase continue",
Op::Refresh => "Refresh",
Op::SelectNext(_) => "Select next",
Op::SelectPrevious(_) => "Select previous",
Op::ShowRefs(_) => "Show refs",
Op::Submenu(_) => "Submenu",
Op::Target(_) => "Target",
Op::ToggleSection(_) => "Toggle section",
})
}
}
impl Display for SubmenuOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
SubmenuOp::Any => "Any",
SubmenuOp::Branch => "Branch",
SubmenuOp::Commit => "Commit",
SubmenuOp::Fetch => "Fetch",
SubmenuOp::Help => "Help",
SubmenuOp::Log => "Log",
SubmenuOp::None => "None",
SubmenuOp::Pull => "Pull",
SubmenuOp::Push => "Push",
SubmenuOp::Rebase => "Rebase",
SubmenuOp::Reset => "Reset",
})
}
}
impl Display for TargetOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
TargetOp::CommitFixup => "Commit fixup",
TargetOp::Discard(_) => "Discard",
TargetOp::LogOther => "Log other",
TargetOp::RebaseAutosquash => "Rebase autosquash",
TargetOp::RebaseInteractive => "Rebase interactive",
TargetOp::ResetSoft => "Reset soft",
TargetOp::ResetMixed => "Reset mixed",
TargetOp::ResetHard => "Reset hard",
TargetOp::Show => "Show",
TargetOp::Stage => "Stage",
TargetOp::Unstage => "Unstage",
})
}
}