Skip to content

Commit c9f79c9

Browse files
committed
Update select_branch component
1 parent ccf719e commit c9f79c9

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/components/select_branch.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl DrawableComponent for SelectBranchComponent {
7171
)
7272
.split(area)[0];
7373
f.render_widget(
74-
Paragraph::new(self.get_text(&self.theme).iter())
74+
Paragraph::new(self.get_text(&self.theme)?.iter())
7575
.scroll(scroll)
7676
.alignment(Alignment::Left),
7777
chunk,
@@ -162,7 +162,7 @@ impl SelectBranchComponent {
162162
key_config: SharedKeyConfig,
163163
) -> Self {
164164
Self {
165-
branch_names: Self::get_branch_names(),
165+
branch_names: Vec::new(),
166166
visible: false,
167167
selection: 0,
168168
queue,
@@ -171,20 +171,23 @@ impl SelectBranchComponent {
171171
}
172172
}
173173
/// Get all the names of the branches in the repo
174-
pub fn get_branch_names() -> Vec<BranchForDisplay> {
174+
pub fn get_branch_names() -> Result<Vec<BranchForDisplay>> {
175175
get_branches_to_display(CWD)
176+
.map_err(|e| anyhow::Error::new(e))
176177
}
177178

178179
///
179180
pub fn open(&mut self) -> Result<()> {
181+
self.update_branches()?;
180182
self.show()?;
181183

182184
Ok(())
183185
}
184186

185187
////
186-
pub fn update_branches(&mut self) {
187-
self.branch_names = Self::get_branch_names();
188+
pub fn update_branches(&mut self) -> Result<()> {
189+
self.branch_names = Self::get_branch_names()?;
190+
Ok(())
188191
}
189192

190193
///
@@ -206,22 +209,24 @@ impl SelectBranchComponent {
206209
}
207210

208211
/// Get branches to display
209-
fn get_text(&self, theme: &SharedTheme) -> Vec<Text> {
212+
fn get_text(&self, theme: &SharedTheme) -> Result<Vec<Text>> {
210213
let mut txt = Vec::new();
211214

212215
let max_branch_name = self
213216
.branch_names
214217
.iter()
215218
.map(|displaybranch| displaybranch.name.len())
216219
.max()
217-
.expect("Failed to find max branch length");
220+
.ok_or_else(|| anyhow::Error::msg(
221+
"Couldn't get max_branch_name, is there a problem with the branches in the repo?",
222+
))?;
218223

219224
for (i, displaybranch) in self.branch_names.iter().enumerate()
220225
{
221226
let mut commit_message =
222227
displaybranch.top_commit_message.clone();
223-
if commit_message.len() > 30 {
224-
commit_message.truncate(30);
228+
if commit_message.len() > 20 {
229+
commit_message.truncate(20);
225230
commit_message += "...";
226231
}
227232

@@ -252,13 +257,13 @@ impl SelectBranchComponent {
252257
));
253258
}
254259

255-
txt
260+
Ok(txt)
256261
}
257262

258263
///
259264
fn switch_to_selected_branch(&self) {
260265
checkout_branch(
261-
"./",
266+
asyncgit::CWD,
262267
&self.branch_names[self.selection as usize].reference,
263268
)
264269
.expect("Failed to checkout branch, does the branch exist?");

0 commit comments

Comments
 (0)