Merge code for desktop notification between MacOS and Linux (#438)

This commit is contained in:
Kevin Gavrois
2023-05-26 10:07:14 +02:00
committed by GitHub
parent d75782892e
commit 7db991db9d
6 changed files with 194 additions and 160 deletions

View File

@@ -397,8 +397,6 @@ pub struct Misc {
display_time: Option<bool>,
display_preamble: Option<bool>,
assume_yes: Option<bool>,
#[merge(strategy = crate::utils::merge_strategies::string_append_opt)]
@@ -787,7 +785,7 @@ pub struct CommandLineArgs {
/// Tracing filter directives.
///
/// See: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/struct.EnvFilter.html
#[clap(long, default_value = "info")]
#[clap(long, default_value = "warn")]
pub log_filter: String,
/// Print completion script for the given shell and exit
@@ -1489,14 +1487,6 @@ impl Config {
.unwrap_or(true)
}
pub fn display_preamble(&self) -> bool {
self.config_file
.misc
.as_ref()
.and_then(|misc| misc.display_preamble)
.unwrap_or(true)
}
pub fn should_run_custom_command(&self, name: &str) -> bool {
if self.opt.custom_commands.is_empty() {
return true;

View File

@@ -94,16 +94,6 @@ fn run() -> Result<()> {
debug!("Binary path: {:?}", std::env::current_exe());
debug!("Self Update: {:?}", cfg!(feature = "self-update"));
#[cfg(target_os = "linux")]
{
if config.display_preamble() && terminal::supports_notify_send() && !config.skip_notify() {
print_warning("Due to a design issue with notify-send it could be that topgrade hangs when it's finished.
If this is the case on your system add the --skip-notify flag to the topgrade command or set skip_notify = true in the config file.
If you don't want this message to appear any longer set display_preamble = false in the config file.
For more information about this issue see https://askubuntu.com/questions/110969/notify-send-ignores-timeout and https://github.com/topgrade-rs/topgrade/issues/288.");
}
}
if config.run_in_tmux() && env::var("TOPGRADE_INSIDE_TMUX").is_err() {
#[cfg(unix)]
{

View File

@@ -1,8 +1,6 @@
use std::cmp::{max, min};
use std::env;
use std::io::{self, Write};
#[cfg(target_os = "linux")]
use std::path::PathBuf;
use std::process::Command;
use std::sync::Mutex;
use std::time::Duration;
@@ -12,7 +10,6 @@ use color_eyre::eyre;
use color_eyre::eyre::Context;
use console::{style, Key, Term};
use lazy_static::lazy_static;
#[cfg(target_os = "macos")]
use notify_rust::{Notification, Timeout};
use tracing::{debug, error};
#[cfg(windows)]
@@ -20,10 +17,7 @@ use which_crate::which;
use crate::command::CommandExt;
use crate::report::StepResult;
#[cfg(target_os = "linux")]
use crate::terminal;
#[cfg(target_os = "linux")]
use crate::utils::which;
lazy_static! {
static ref TERMINAL: Mutex<Terminal> = Mutex::new(Terminal::new());
}
@@ -49,8 +43,6 @@ struct Terminal {
set_title: bool,
display_time: bool,
desktop_notification: bool,
#[cfg(target_os = "linux")]
notify_send: Option<PathBuf>,
}
impl Terminal {
@@ -65,8 +57,6 @@ impl Terminal {
set_title: true,
display_time: true,
desktop_notification: false,
#[cfg(target_os = "linux")]
notify_send: which("notify-send"),
}
}
@@ -82,35 +72,18 @@ impl Terminal {
self.display_time = display_time
}
#[allow(unused_variables)]
fn notify_desktop<P: AsRef<str>>(&self, message: P, timeout: Option<Duration>) {
debug!("Desktop notification: {}", message.as_ref());
cfg_if::cfg_if! {
if #[cfg(target_os = "macos")] {
let mut notification = Notification::new();
notification.summary("Topgrade")
.body(message.as_ref())
.appname("topgrade");
let mut notification = Notification::new();
notification
.summary("Topgrade")
.body(message.as_ref())
.appname("topgrade");
if let Some(timeout) = timeout {
notification.timeout(Timeout::Milliseconds(timeout.as_millis() as u32));
}
notification.show().ok();
} else if #[cfg(target_os = "linux")] {
if let Some(ns) = self.notify_send.as_ref() {
let mut command = Command::new(ns);
if let Some(timeout) = timeout {
command.arg("-t");
command.arg(format!("{}", timeout.as_millis()));
}
command.args(["-a", "Topgrade", "Topgrade"]);
command.arg(message.as_ref());
if let Err(err) = command.output_checked() {
terminal::print_warning("Sending notification failed with {err:?}");
}
}
}
if let Some(timeout) = timeout {
notification.timeout(Timeout::Milliseconds(timeout.as_millis() as u32));
}
notification.show().ok();
}
fn print_separator<P: AsRef<str>>(&mut self, message: P) {
@@ -345,8 +318,3 @@ pub fn notify_desktop<P: AsRef<str>>(message: P, timeout: Option<Duration>) {
pub fn display_time(display_time: bool) {
TERMINAL.lock().unwrap().display_time(display_time);
}
#[cfg(target_os = "linux")]
pub fn supports_notify_send() -> bool {
TERMINAL.lock().unwrap().notify_send.is_some()
}