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
use {
chrono::Local,
crossterm::style::Color,
std::{borrow::Cow, env},
};
pub static DEFAULT_PROMPT_COLOR: Color = Color::Blue;
pub static DEFAULT_PROMPT_INDICATOR: &str = "〉";
pub static DEFAULT_VI_INSERT_PROMPT_INDICATOR: &str = ": ";
pub static DEFAULT_VI_VISUAL_PROMPT_INDICATOR: &str = "v ";
pub static DEFAULT_MULTILINE_INDICATOR: &str = "::: ";
pub enum PromptHistorySearchStatus {
Passing,
Failing,
}
pub struct PromptHistorySearch {
pub status: PromptHistorySearchStatus,
pub term: String,
}
impl PromptHistorySearch {
pub fn new(status: PromptHistorySearchStatus, search_term: String) -> Self {
PromptHistorySearch {
status,
term: search_term,
}
}
}
pub enum PromptEditMode {
Default,
Emacs,
Vi(PromptViMode),
Custom(String),
}
pub enum PromptViMode {
Normal,
Insert,
Visual,
}
pub trait Prompt {
fn render_prompt(&self, screen_width: usize) -> Cow<str>;
fn render_prompt_indicator(&self, prompt_mode: PromptEditMode) -> Cow<str>;
fn render_prompt_multiline_indicator(&self) -> Cow<str>;
fn render_prompt_history_search_indicator(
&self,
history_search: PromptHistorySearch,
) -> Cow<str>;
fn get_prompt_color(&self) -> Color {
DEFAULT_PROMPT_COLOR
}
}
impl Prompt for DefaultPrompt {
fn render_prompt(&self, screen_width: usize) -> Cow<str> {
DefaultPrompt::render_prompt(self, screen_width)
}
fn render_prompt_indicator(&self, edit_mode: PromptEditMode) -> Cow<str> {
match edit_mode {
PromptEditMode::Default => DEFAULT_PROMPT_INDICATOR.into(),
PromptEditMode::Emacs => DEFAULT_PROMPT_INDICATOR.into(),
PromptEditMode::Vi(vi_mode) => match vi_mode {
PromptViMode::Normal => DEFAULT_PROMPT_INDICATOR.into(),
PromptViMode::Insert => DEFAULT_VI_INSERT_PROMPT_INDICATOR.into(),
PromptViMode::Visual => DEFAULT_VI_VISUAL_PROMPT_INDICATOR.into(),
},
PromptEditMode::Custom(str) => self.default_wrapped_custom_string(str).into(),
}
}
fn render_prompt_multiline_indicator(&self) -> Cow<str> {
Cow::Borrowed(DEFAULT_MULTILINE_INDICATOR)
}
fn render_prompt_history_search_indicator(
&self,
history_search: PromptHistorySearch,
) -> Cow<str> {
let prefix = match history_search.status {
PromptHistorySearchStatus::Passing => "",
PromptHistorySearchStatus::Failing => "failing ",
};
Cow::Owned(format!(
"({}reverse-search: {})",
prefix, history_search.term
))
}
}
impl Default for DefaultPrompt {
fn default() -> Self {
DefaultPrompt::new(1)
}
}
#[derive(Clone)]
pub struct DefaultPrompt {
min_center_spacing: u16,
}
impl DefaultPrompt {
pub fn new(min_center_spacing: u16) -> DefaultPrompt {
DefaultPrompt { min_center_spacing }
}
fn render_prompt(&self, cols: usize) -> Cow<str> {
let mut prompt_str = String::new();
let mut left_prompt = get_working_dir().unwrap_or_else(|_| String::from("no path"));
left_prompt.truncate(cols);
let left_prompt_width = left_prompt.chars().count();
prompt_str.push_str(&left_prompt);
let right_prompt = get_now();
let right_prompt_width = right_prompt.chars().count();
if left_prompt_width + usize::from(self.min_center_spacing) + right_prompt_width <= cols {
let right_prompt = format!("{:>width$}", get_now(), width = cols - left_prompt_width);
prompt_str.push_str(&right_prompt);
} else if left_prompt_width < cols {
let right_padding = format!("{:>width$}", "", width = cols - left_prompt_width);
prompt_str.push_str(&right_padding);
}
Cow::Owned(prompt_str)
}
fn default_wrapped_custom_string(&self, str: String) -> String {
format!("({})", str)
}
}
fn get_working_dir() -> Result<String, std::io::Error> {
let path = env::current_dir()?;
Ok(path.display().to_string())
}
fn get_now() -> String {
let now = Local::now();
format!("{}", now.format("%m/%d/%Y %I:%M:%S %p"))
}