Factored data into a SystemSpecs struct

Joseph Lyons and Mikayla Maki created

Co-Authored-By: Mikayla Maki <mikayla.c.maki@gmail.com>

Change summary

crates/workspace/src/workspace.rs | 54 +++-----------------------------
crates/zed/src/menus.rs           |  4 +-
crates/zed/src/system_specs.rs    | 46 ++++++++++++++++++++++++++++
crates/zed/src/zed.rs             | 18 ++++++++++
4 files changed, 70 insertions(+), 52 deletions(-)

Detailed changes

crates/workspace/src/workspace.rs 🔗

@@ -34,16 +34,15 @@ use gpui::{
     elements::*,
     impl_actions, impl_internal_actions,
     platform::{CursorStyle, WindowOptions},
-    AnyModelHandle, AnyViewHandle, AppContext, AsyncAppContext, ClipboardItem, Entity,
-    ModelContext, ModelHandle, MouseButton, MutableAppContext, PathPromptOptions, PromptLevel,
-    RenderContext, Task, View, ViewContext, ViewHandle, WeakViewHandle,
+    AnyModelHandle, AnyViewHandle, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle,
+    MouseButton, MutableAppContext, PathPromptOptions, PromptLevel, RenderContext, Task, View,
+    ViewContext, ViewHandle, WeakViewHandle,
 };
 use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ProjectItem};
 use language::LanguageRegistry;
 use std::{
     any::TypeId,
     borrow::Cow,
-    env,
     future::Future,
     path::{Path, PathBuf},
     sync::Arc,
@@ -73,7 +72,7 @@ use status_bar::StatusBar;
 pub use status_bar::StatusItemView;
 use theme::{Theme, ThemeRegistry};
 pub use toolbar::{ToolbarItemLocation, ToolbarItemView};
-use util::{channel::ReleaseChannel, ResultExt};
+use util::ResultExt;
 
 #[derive(Clone, PartialEq)]
 pub struct RemoveWorktreeFromProject(pub WorktreeId);
@@ -96,8 +95,7 @@ actions!(
         ToggleLeftSidebar,
         ToggleRightSidebar,
         NewTerminal,
-        NewSearch,
-        CopySystemDetailsIntoClipboard
+        NewSearch
     ]
 );
 
@@ -301,12 +299,6 @@ pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
         },
     );
 
-    cx.add_action(
-        |workspace: &mut Workspace, _: &CopySystemDetailsIntoClipboard, cx| {
-            workspace.copy_system_details_into_clipboard(cx);
-        },
-    );
-
     let client = &app_state.client;
     client.add_view_request_handler(Workspace::handle_follow);
     client.add_view_message_handler(Workspace::handle_unfollow);
@@ -988,42 +980,6 @@ impl Workspace {
         })
     }
 
-    fn copy_system_details_into_clipboard(&mut self, cx: &mut ViewContext<Self>) {
-        let platform = cx.platform();
-
-        let os_name: String = platform.os_name().into();
-        let os_version = platform.os_version().ok();
-        let app_version = env!("CARGO_PKG_VERSION");
-        let release_channel = cx.global::<ReleaseChannel>().dev_name();
-        let architecture = env::var("CARGO_CFG_TARGET_ARCH");
-
-        let os_information = match os_version {
-            Some(os_version) => format!("OS: {os_name} {os_version}"),
-            None => format!("OS: {os_name}"),
-        };
-        let system_information = vec![
-            Some(os_information),
-            Some(format!("Zed: {app_version} ({release_channel})")),
-            architecture
-                .map(|architecture| format!("Architecture: {architecture}"))
-                .ok(),
-        ];
-
-        let system_information = system_information
-            .into_iter()
-            .flatten()
-            .collect::<Vec<_>>()
-            .join("\n");
-
-        let item = ClipboardItem::new(system_information.clone());
-        cx.prompt(
-            gpui::PromptLevel::Info,
-            &format!("Copied into clipboard:\n\n{system_information}"),
-            &["OK"],
-        );
-        cx.write_to_clipboard(item.clone());
-    }
-
     #[allow(clippy::type_complexity)]
     pub fn open_paths(
         &mut self,

crates/zed/src/menus.rs 🔗

@@ -339,8 +339,8 @@ pub fn menus() -> Vec<Menu<'static>> {
                 },
                 MenuItem::Separator,
                 MenuItem::Action {
-                    name: "Copy System Details Into Clipboard",
-                    action: Box::new(workspace::CopySystemDetailsIntoClipboard),
+                    name: "Copy System Specs Into Clipboard",
+                    action: Box::new(crate::CopySystemSpecsIntoClipboard),
                 },
                 MenuItem::Action {
                     name: "Give Feedback",

crates/zed/src/system_specs.rs 🔗

@@ -0,0 +1,46 @@
+use std::{env, fmt::Display};
+
+use gpui::AppContext;
+use util::channel::ReleaseChannel;
+
+pub struct SystemSpecs {
+    os_name: &'static str,
+    os_version: Option<String>,
+    app_version: &'static str,
+    release_channel: &'static str,
+    architecture: &'static str,
+}
+
+impl SystemSpecs {
+    pub fn new(cx: &AppContext) -> Self {
+        let platform = cx.platform();
+
+        SystemSpecs {
+            os_name: platform.os_name(),
+            os_version: platform
+                .os_version()
+                .ok()
+                .map(|os_version| os_version.to_string()),
+            app_version: env!("CARGO_PKG_VERSION"),
+            release_channel: cx.global::<ReleaseChannel>().dev_name(),
+            architecture: env::consts::ARCH,
+        }
+    }
+}
+
+impl Display for SystemSpecs {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        let os_information = match &self.os_version {
+            Some(os_version) => format!("OS: {} {}", self.os_name, os_version),
+            None => format!("OS: {}", self.os_name),
+        };
+        let system_specs = [
+            os_information,
+            format!("Zed: {} ({})", self.app_version, self.release_channel),
+            format!("Architecture: {}", self.architecture),
+        ]
+        .join("\n");
+
+        write!(f, "{system_specs}")
+    }
+}

crates/zed/src/zed.rs 🔗

@@ -1,6 +1,7 @@
 mod feedback;
 pub mod languages;
 pub mod menus;
+pub mod system_specs;
 #[cfg(any(test, feature = "test-support"))]
 pub mod test;
 
@@ -21,7 +22,7 @@ use gpui::{
     },
     impl_actions,
     platform::{WindowBounds, WindowOptions},
-    AssetSource, AsyncAppContext, TitlebarOptions, ViewContext, WindowKind,
+    AssetSource, AsyncAppContext, ClipboardItem, TitlebarOptions, ViewContext, WindowKind,
 };
 use language::Rope;
 use lazy_static::lazy_static;
@@ -33,6 +34,7 @@ use serde::Deserialize;
 use serde_json::to_string_pretty;
 use settings::{keymap_file_json_schema, settings_file_json_schema, Settings};
 use std::{env, path::Path, str, sync::Arc};
+use system_specs::SystemSpecs;
 use util::{channel::ReleaseChannel, paths, ResultExt};
 pub use workspace;
 use workspace::{sidebar::SidebarSide, AppState, Workspace};
@@ -67,6 +69,7 @@ actions!(
         ResetBufferFontSize,
         InstallCommandLineInterface,
         ResetDatabase,
+        CopySystemSpecsIntoClipboard
     ]
 );
 
@@ -245,6 +248,19 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::MutableAppContext) {
         },
     );
 
+    cx.add_action(
+        |_: &mut Workspace, _: &CopySystemSpecsIntoClipboard, cx: &mut ViewContext<Workspace>| {
+            let system_specs = SystemSpecs::new(cx).to_string();
+            let item = ClipboardItem::new(system_specs.clone());
+            cx.prompt(
+                gpui::PromptLevel::Info,
+                &format!("Copied into clipboard:\n\n{system_specs}"),
+                &["OK"],
+            );
+            cx.write_to_clipboard(item);
+        },
+    );
+
     activity_indicator::init(cx);
     call::init(app_state.client.clone(), app_state.user_store.clone(), cx);
     settings::KeymapFileContent::load_defaults(cx);