From a0a587b8fd916d294c88b5a7d0405c6cedf75662 Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 8 Jul 2021 15:25:02 +0200 Subject: [PATCH] Added cancellation dialog to rust-analyzer installation --- src/notebook.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/notebook.cpp b/src/notebook.cpp index 47bdcd4..70d4e50 100644 --- a/src/notebook.cpp +++ b/src/notebook.cpp @@ -1,5 +1,6 @@ #include "notebook.hpp" #include "config.hpp" +#include "dialog.hpp" #include "filesystem.hpp" #include "project.hpp" #include "selection_dialog.hpp" @@ -198,12 +199,22 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position dialog.set_default_response(Gtk::RESPONSE_YES); dialog.set_secondary_text("Would you like to install rust-analyzer through rustup?"); int result = dialog.run(); + dialog.hide(); if(result == Gtk::RESPONSE_YES) { + bool canceled = false; + Dialog::Message message("Installing rust-analyzer", [&canceled] { + canceled = true; + }); boost::optional exit_status; - Terminal::get().async_process(std::string(command), "", [&exit_status](int exit_status_) { + auto process = Terminal::get().async_process(std::string(command), "", [&exit_status](int exit_status_) { exit_status = exit_status_; }); + bool killed = false; while(!exit_status) { + if(canceled && !killed) { + process->kill(); + killed = true; + } while(Gtk::Main::events_pending()) Gtk::Main::iteration(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); @@ -220,16 +231,17 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position first = false; std::stringstream stdin_stream, stdout_stream; if(Terminal::get().process(stdin_stream, stdout_stream, "rustup component list") == 0) { + bool rust_analyzer_in_toolchain = false; std::string line; while(std::getline(stdout_stream, line)) { if(starts_with(line, "rust-analyzer")) { if(install_rust_analyzer(std::string("rustup component add rust-src ") + (starts_with(line, "rust-analyzer-preview") ? "rust-analyzer-preview" : "rust-analyzer"))) source_views.emplace_back(new Source::LanguageProtocolView(file_path, language, language_protocol_language_id, filesystem::escape_argument(rust_analyzer.string()))); + rust_analyzer_in_toolchain = true; break; } } - if(source_views_previous_size == source_views.size() && - install_rust_analyzer("rustup component add rust-src && rustup toolchain install nightly && rustup component add --toolchain nightly rust-src rust-analyzer-preview")) + if(!rust_analyzer_in_toolchain && install_rust_analyzer("rustup component add rust-src && rustup toolchain install nightly && rustup component add --toolchain nightly rust-src rust-analyzer-preview")) source_views.emplace_back(new Source::LanguageProtocolView(file_path, language, language_protocol_language_id, "rustup run nightly rust-analyzer")); } }