Skip to content

Commit

Permalink
Show error message if copy to clipboard fails
Browse files Browse the repository at this point in the history
- Draw `msg` after `inspect_commit_popup` to make sure the error message
  is visible
- Move `try_or_popup!` to `utils`
  • Loading branch information
cruessler committed Aug 19, 2020
1 parent a6c89bf commit a436b6c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ impl App {
self.stashmsg_popup.draw(f, size)?;
self.reset.draw(f, size)?;
self.help.draw(f, size)?;
self.msg.draw(f, size)?;
self.inspect_commit_popup.draw(f, size)?;
self.msg.draw(f, size)?;
self.external_editor_popup.draw(f, size)?;
self.tag_commit_popup.draw(f, size)?;

Expand Down
18 changes: 1 addition & 17 deletions src/components/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
components::{CommandInfo, Component},
keys,
queue::{Action, InternalEvent, NeedsUpdate, Queue, ResetItem},
strings,
strings, try_or_popup,
ui::style::SharedTheme,
};
use anyhow::Result;
Expand All @@ -17,22 +17,6 @@ use std::path::Path;
use strings::commands;
use tui::{backend::Backend, layout::Rect, Frame};

/// macro to simplify running code that might return Err.
/// It will show a popup in that case
#[macro_export]
macro_rules! try_or_popup {
($self:ident, $msg:literal, $e:expr) => {
if let Err(err) = $e {
$self.queue.borrow_mut().push_back(
InternalEvent::ShowErrorMsg(format!(
"{}\n{}",
$msg, err
)),
);
}
};
}

///
pub struct ChangesComponent {
title: String,
Expand Down
78 changes: 42 additions & 36 deletions src/components/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
keys,
queue::{Action, InternalEvent, NeedsUpdate, Queue, ResetItem},
strings::{self, commands},
try_or_popup,
ui::{calc_scroll_top, style::SharedTheme},
};
use asyncgit::{hash, sync, DiffLine, DiffLineType, FileDiff, CWD};
Expand All @@ -21,7 +22,7 @@ use tui::{
Frame,
};

use anyhow::Result;
use anyhow::{anyhow, Result};

#[derive(Default)]
struct Current {
Expand Down Expand Up @@ -103,13 +104,18 @@ pub struct DiffComponent {
focused: bool,
current: Current,
scroll_top: Cell<usize>,
queue: Option<Queue>,
queue: Queue,
theme: SharedTheme,
is_immutable: bool,
}

impl DiffComponent {
///
pub fn new(queue: Option<Queue>, theme: SharedTheme) -> Self {
pub fn new(
queue: Queue,
theme: SharedTheme,
is_immutable: bool,
) -> Self {
Self {
focused: false,
queue,
Expand All @@ -121,6 +127,7 @@ impl DiffComponent {
selection: Selection::Single(0),
scroll_top: Cell::new(0),
theme,
is_immutable,
}
}
///
Expand Down Expand Up @@ -228,6 +235,18 @@ impl DiffComponent {
Ok(())
}

fn copy_string(string: String) -> Result<()> {
let mut ctx: ClipboardContext = ClipboardProvider::new()
.map_err(|_| {
anyhow!("failed to get access to clipboard")
})?;
ctx.set_contents(string).map_err(|_| {
anyhow!("failed to set clipboard contents")
})?;

Ok(())
}

fn copy_selection(&self) -> Result<()> {
if let Some(diff) = &self.diff {
let lines_to_copy: Vec<&str> = diff
Expand All @@ -250,10 +269,11 @@ impl DiffComponent {
})
.collect();

let mut ctx: ClipboardContext = ClipboardProvider::new()
.expect("failed to get access to clipboard");
ctx.set_contents(lines_to_copy.join("\n"))
.expect("failed to set clipboard contents");
try_or_popup!(
self,
"copy to clipboard error:",
Self::copy_string(lines_to_copy.join("\n"))
);
}

Ok(())
Expand Down Expand Up @@ -483,7 +503,6 @@ impl DiffComponent {
fn queue_update(&mut self) {
self.queue
.as_ref()
.expect("try using queue in immutable diff")
.borrow_mut()
.push_back(InternalEvent::Update(NeedsUpdate::ALL));
}
Expand All @@ -493,40 +512,28 @@ impl DiffComponent {
if let Some(hunk) = self.selected_hunk {
let hash = diff.hunks[hunk].header_hash;

self.queue
.as_ref()
.expect("try using queue in immutable diff")
.borrow_mut()
.push_back(InternalEvent::ConfirmAction(
Action::ResetHunk(
self.current.path.clone(),
hash,
),
));
self.queue.as_ref().borrow_mut().push_back(
InternalEvent::ConfirmAction(Action::ResetHunk(
self.current.path.clone(),
hash,
)),
);
}
}
Ok(())
}

fn reset_untracked(&self) -> Result<()> {
self.queue
.as_ref()
.expect("try using queue in immutable diff")
.borrow_mut()
.push_back(InternalEvent::ConfirmAction(Action::Reset(
ResetItem {
path: self.current.path.clone(),
is_folder: false,
},
)));
self.queue.as_ref().borrow_mut().push_back(
InternalEvent::ConfirmAction(Action::Reset(ResetItem {
path: self.current.path.clone(),
is_folder: false,
})),
);

Ok(())
}

fn is_immutable(&self) -> bool {
self.queue.is_none()
}

const fn is_stage(&self) -> bool {
self.current.is_stage
}
Expand Down Expand Up @@ -603,7 +610,7 @@ impl Component for DiffComponent {
.hidden(),
);

if !self.is_immutable() {
if !self.is_immutable {
out.push(CommandInfo::new(
commands::DIFF_HUNK_REMOVE,
self.selected_hunk.is_some(),
Expand Down Expand Up @@ -660,7 +667,7 @@ impl Component for DiffComponent {
self.move_selection(ScrollType::PageDown)?;
Ok(true)
}
keys::ENTER if !self.is_immutable() => {
keys::ENTER if !self.is_immutable => {
if self.current.is_stage {
self.unstage_hunk()?;
} else {
Expand All @@ -669,8 +676,7 @@ impl Component for DiffComponent {
Ok(true)
}
keys::DIFF_RESET_HUNK
if !self.is_immutable()
&& !self.is_stage() =>
if !self.is_immutable && !self.is_stage() =>
{
if let Some(diff) = &self.diff {
if diff.untracked {
Expand Down
2 changes: 1 addition & 1 deletion src/components/inspect_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl InspectCommitComponent {
sender,
theme.clone(),
),
diff: DiffComponent::new(None, theme),
diff: DiffComponent::new(queue.clone(), theme, true),
commit_id: None,
tags: None,
git_diff: AsyncDiff::new(sender.clone()),
Expand Down
16 changes: 16 additions & 0 deletions src/components/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ pub mod filetree;
pub mod logitems;
pub mod statustree;

/// macro to simplify running code that might return Err.
/// It will show a popup in that case
#[macro_export]
macro_rules! try_or_popup {
($self:ident, $msg:literal, $e:expr) => {
if let Err(err) = $e {
$self.queue.borrow_mut().push_back(
InternalEvent::ShowErrorMsg(format!(
"{}\n{}",
$msg, err
)),
);
}
};
}

/// helper func to convert unix time since epoch to formated time string in local timezone
pub fn time_to_string(secs: i64, short: bool) -> String {
let time = DateTime::<Local>::from(DateTime::<Utc>::from_utc(
Expand Down
2 changes: 1 addition & 1 deletion src/tabs/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Status {
queue.clone(),
theme.clone(),
),
diff: DiffComponent::new(Some(queue.clone()), theme),
diff: DiffComponent::new(queue.clone(), theme, false),
git_diff: AsyncDiff::new(sender.clone()),
git_status_workdir: AsyncStatus::new(sender.clone()),
git_status_stage: AsyncStatus::new(sender.clone()),
Expand Down

0 comments on commit a436b6c

Please sign in to comment.