Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Display available stories in storybook CLI (#3021)
Marshall Bowersad62a966a65d
diff --git a/Cargo.lock b/Cargo.lockindex 186427e863b..878604f3609 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -7374,7 +7374,7 @@ name = "storybook"version = "0.1.0"dependencies = ["anyhow",- "clap 3.2.25",+ "clap 4.4.4","gpui2","log","rust-embed",diff --git a/crates/storybook/Cargo.toml b/crates/storybook/Cargo.tomlindex 49dd05ba30b..5c73235d3e6 100644--- a/crates/storybook/Cargo.toml+++ b/crates/storybook/Cargo.toml@@ -10,7 +10,7 @@ path = "src/storybook.rs"[dependencies]anyhow.workspace = true-clap = { version = "3.1", features = ["derive"] }+clap = { version = "4.4", features = ["derive", "string"] }gpui2 = { path = "../gpui2" }log.workspace = truerust-embed.workspace = truediff --git a/crates/storybook/src/story_selector.rs b/crates/storybook/src/story_selector.rsnew file mode 100644index 00000000000..d12368706a6--- /dev/null+++ b/crates/storybook/src/story_selector.rs@@ -0,0 +1,76 @@+use std::{str::FromStr, sync::OnceLock};++use anyhow::{anyhow, Context};+use clap::builder::PossibleValue;+use clap::ValueEnum;+use strum::{EnumIter, EnumString, IntoEnumIterator};++#[derive(Debug, Clone, Copy, strum::Display, EnumString, EnumIter)]+#[strum(serialize_all = "snake_case")]+pub enum ElementStory {+ Avatar,+}++#[derive(Debug, Clone, Copy, strum::Display, EnumString, EnumIter)]+#[strum(serialize_all = "snake_case")]+pub enum ComponentStory {+ Breadcrumb,+ Facepile,+ Toolbar,+ TrafficLights,+}++#[derive(Debug, Clone, Copy)]+pub enum StorySelector {+ Element(ElementStory),+ Component(ComponentStory),+}++impl FromStr for StorySelector {+ type Err = anyhow::Error;++ fn from_str(raw_story_name: &str) -> std::result::Result<Self, Self::Err> {+ let story = raw_story_name.to_ascii_lowercase();++ if let Some((_, story)) = story.split_once("elements/") {+ let element_story = ElementStory::from_str(story)+ .with_context(|| format!("story not found for element '{story}'"))?;++ return Ok(Self::Element(element_story));+ }++ if let Some((_, story)) = story.split_once("components/") {+ let component_story = ComponentStory::from_str(story)+ .with_context(|| format!("story not found for component '{story}'"))?;++ return Ok(Self::Component(component_story));+ }++ Err(anyhow!("story not found for '{raw_story_name}'"))+ }+}++/// The list of all stories available in the storybook.+static ALL_STORIES: OnceLock<Vec<StorySelector>> = OnceLock::new();++impl ValueEnum for StorySelector {+ fn value_variants<'a>() -> &'a [Self] {+ let stories = ALL_STORIES.get_or_init(|| {+ let element_stories = ElementStory::iter().map(Self::Element);+ let component_stories = ComponentStory::iter().map(Self::Component);++ element_stories.chain(component_stories).collect::<Vec<_>>()+ });++ stories+ }++ fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {+ let value = match self {+ Self::Element(story) => format!("elements/{story}"),+ Self::Component(story) => format!("components/{story}"),+ };++ Some(PossibleValue::new(value))+ }+}diff --git a/crates/storybook/src/storybook.rs b/crates/storybook/src/storybook.rsindex 59aca22be7b..d52219f61ef 100644--- a/crates/storybook/src/storybook.rs+++ b/crates/storybook/src/storybook.rs@@ -3,10 +3,9 @@mod collab_panel;mod stories;mod story;+mod story_selector;mod workspace;-use std::str::FromStr;-use ::theme as legacy_theme;use clap::Parser;use gpui2::{serde_json, vec2f, view, Element, IntoElement, RectF, ViewContext, WindowBounds};@@ -19,61 +18,19 @@ use stories::components::facepile::FacepileStory;use stories::components::toolbar::ToolbarStory;use stories::components::traffic_lights::TrafficLightsStory;use stories::elements::avatar::AvatarStory;-use strum::EnumString;use ui::{ElementExt, Theme};+use crate::story_selector::{ComponentStory, ElementStory, StorySelector};+gpui2::actions! {storybook,[ToggleInspector]}-#[derive(Debug, Clone, Copy)]-enum StorySelector {- Element(ElementStory),- Component(ComponentStory),-}--impl FromStr for StorySelector {- type Err = anyhow::Error;-- fn from_str(raw_story_name: &str) -> std::result::Result<Self, Self::Err> {- let story = raw_story_name.to_ascii_lowercase();-- if let Some((_, story)) = story.split_once("elements/") {- let element_story = ElementStory::from_str(story)- .with_context(|| format!("story not found for element '{story}'"))?;-- return Ok(Self::Element(element_story));- }-- if let Some((_, story)) = story.split_once("components/") {- let component_story = ComponentStory::from_str(story)- .with_context(|| format!("story not found for component '{story}'"))?;-- return Ok(Self::Component(component_story));- }-- Err(anyhow!("story not found for '{raw_story_name}'"))- }-}--#[derive(Debug, Clone, Copy, EnumString)]-#[strum(serialize_all = "snake_case")]-enum ElementStory {- Avatar,-}--#[derive(Debug, Clone, Copy, EnumString)]-#[strum(serialize_all = "snake_case")]-enum ComponentStory {- Breadcrumb,- Facepile,- Toolbar,- TrafficLights,-}-#[derive(Parser)]+#[command(author, version, about, long_about = None)]struct Args {+ #[arg(value_enum)]story: Option<StorySelector>,}@@ -146,7 +103,7 @@ fn current_theme<V: 'static>(cx: &mut ViewContext<V>) -> Theme {.clone()}-use anyhow::{anyhow, Context, Result};+use anyhow::{anyhow, Result};use gpui2::AssetSource;use rust_embed::RustEmbed;use workspace::WorkspaceElement;