gpui-cn 0.5.0

shadcn-style components for GPUI, built on gpui-base.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Where a folder picker gets its folders: a trait any file system,
//! remote machine, or API can implement, and the local disk as the
//! default.

use std::{cell::RefCell, collections::HashMap, rc::Rc};

use gpui_kit::{App, AppContext as _, SharedString, Task};

use crate::path_browser::{
    CreateFolderError, Entry, ListError, Page, PageToken, PathStyle, Source, SourcePath,
};
use smol::stream::StreamExt as _;

/// A folder in a listing.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FolderEntry {
    name: SharedString,
    hidden: bool,
}

impl FolderEntry {
    /// A folder with a name. A name that starts with a dot is hidden.
    pub fn new(name: impl Into<SharedString>) -> Self {
        let name = name.into();
        let hidden = name.starts_with('.');
        Self { name, hidden }
    }

    /// Whether the folder is hidden, which sorts it after the others.
    pub fn with_hidden(mut self, hidden: bool) -> Self {
        self.hidden = hidden;
        self
    }

    /// The folder's name, without a path.
    pub fn name(&self) -> &SharedString {
        &self.name
    }

    /// Whether the folder is hidden.
    pub fn hidden(&self) -> bool {
        self.hidden
    }
}

/// One page of a listing: folders, and where the next page starts.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FolderPage {
    entries: Vec<FolderEntry>,
    next: Option<PageToken>,
}

impl FolderPage {
    /// The last page of a listing, with `entries` in the order to show.
    pub fn new(entries: impl IntoIterator<Item = FolderEntry>) -> Self {
        Self {
            entries: entries.into_iter().collect(),
            next: None,
        }
    }

    /// Says that more folders follow, from `token`. The picker shows a
    /// "Load more" row and asks for that page when the user reaches it.
    pub fn with_next(mut self, token: PageToken) -> Self {
        self.next = Some(token);
        self
    }

    /// The folders of the page.
    pub fn entries(&self) -> &[FolderEntry] {
        &self.entries
    }

    /// Where the next page starts, when there is one.
    pub fn next(&self) -> Option<&PageToken> {
        self.next.as_ref()
    }
}

/// Where a [`FolderPicker`](super::FolderPicker) lists folders from: the
/// local disk, an SSH session, a bucket, an API. The picker never waits
/// for a source. It calls `list` and awaits the task, and it drops the
/// task to cancel it.
///
/// A source lists one directory a page at a time. It should give entries
/// in the order to show: the picker keeps that order and sorts nothing,
/// so a source that pages must sort across its pages itself. Folders
/// that repeat across pages, by name, show once.
///
/// ```
/// use gpui_cn::{FolderEntry, FolderPage, FolderSource, ListError, PageToken, SourcePath};
/// use gpui_kit::{App, AppContext as _, Task};
///
/// /// A source over a list of names, ten to a page.
/// struct Names(Vec<String>);
///
/// impl FolderSource for Names {
///     fn list(
///         &self,
///         _dir: &SourcePath,
///         page: Option<PageToken>,
///         cx: &mut App,
///     ) -> Task<Result<FolderPage, ListError>> {
///         let start = page.and_then(|t| t.as_str().parse().ok()).unwrap_or(0);
///         let end = (start + 10).min(self.0.len());
///         let names = self.0[start..end].to_vec();
///         let more = (end < self.0.len()).then(|| PageToken::new(end.to_string()));
///         // Any work goes on a task: a request, a query, a disk read.
///         cx.background_spawn(async move {
///             let page = FolderPage::new(names.into_iter().map(FolderEntry::new));
///             Ok(match more {
///                 Some(token) => page.with_next(token),
///                 None => page,
///             })
///         })
///     }
/// }
/// ```
pub trait FolderSource: 'static {
    /// Lists the folders of `dir`, from the start when `page` is `None`,
    /// or else from the page `token` a previous page named. A folder whose
    /// name is empty, `.` or `..`, or has a separator of the source's style
    /// or a control character is dropped by the picker.
    fn list(
        &self,
        dir: &SourcePath,
        page: Option<PageToken>,
        cx: &mut App,
    ) -> Task<Result<FolderPage, ListError>>;

    /// The source's home folder, which a leading `~` in the typed path
    /// stands for and where a picker with no start of its own begins. The
    /// default is `None`: a source with no home does not expand `~`, so a
    /// typed `~` is an ordinary name, and the picker starts at the root.
    /// A remote or virtual source gives its own, made with
    /// [`path_style`](Self::path_style).
    fn home(&self) -> Option<SourcePath> {
        None
    }

    /// How the source writes its paths. The default is the style of the
    /// machine the application runs on. A source for another machine
    /// names its own: a Linux server is [`PathStyle::posix`] whatever
    /// the client is.
    fn path_style(&self) -> PathStyle {
        PathStyle::host()
    }

    /// The value of the variable `name`, for `%NAME%` in a Windows-style
    /// path that is pasted. The default knows none: a remote source never
    /// reads the host's environment.
    fn env(&self, name: &str) -> Option<String> {
        let _ = name;
        None
    }

    /// Whether the source can make folders. The default is `false`, which
    /// hides the picker's "New folder" button.
    fn can_create_folders(&self) -> bool {
        false
    }

    /// Makes the folder `name` inside `parent` and answers with its path.
    /// The picker has checked `name` already: it is not empty, has no
    /// separator, and is not `.` or `..`. Answer
    /// [`CreateFolderError::Exists`] when something has that name. The
    /// picker lists the directory again afterward, so the new folder
    /// shows in the order the source lists. The default answers
    /// [`CreateFolderError::Unsupported`].
    fn create_folder(
        &self,
        parent: &SourcePath,
        name: &str,
        cx: &mut App,
    ) -> Task<Result<SourcePath, CreateFolderError>> {
        let _ = (parent, name, cx);
        Task::ready(Err(CreateFolderError::Unsupported))
    }
}

/// The local file system, in one page. It reads on a background task,
/// counts a symlink to a folder as a folder, and puts the visible folders
/// first and the hidden ones after, each in alphabetical order without
/// regard to case.
#[derive(Clone, Copy, Debug, Default)]
pub struct LocalFolders;

impl FolderSource for LocalFolders {
    /// The home directory of the user running the application.
    fn home(&self) -> Option<SourcePath> {
        std::env::home_dir().map(|home| PathStyle::host().path(home.to_string_lossy()))
    }

    fn env(&self, name: &str) -> Option<String> {
        std::env::var(name).ok()
    }

    fn can_create_folders(&self) -> bool {
        true
    }

    fn create_folder(
        &self,
        parent: &SourcePath,
        name: &str,
        cx: &mut App,
    ) -> Task<Result<SourcePath, CreateFolderError>> {
        let path = parent.join(name);
        let made = path.to_path_buf();
        cx.background_spawn(async move {
            smol::fs::create_dir(&made).await?;
            Ok(path)
        })
    }

    fn list(
        &self,
        dir: &SourcePath,
        _page: Option<PageToken>,
        cx: &mut App,
    ) -> Task<Result<FolderPage, ListError>> {
        let dir = dir.to_path_buf();
        cx.background_spawn(async move { Ok(list_local(dir).await?) })
    }
}

async fn list_local(dir: std::path::PathBuf) -> std::io::Result<FolderPage> {
    let mut read = smol::fs::read_dir(&dir).await?;
    let mut folders = Vec::new();
    while let Some(entry) = read.next().await {
        let Ok(entry) = entry else { continue };
        let is_folder = match entry.file_type().await {
            Ok(kind) if kind.is_dir() => true,
            Ok(kind) if kind.is_symlink() => smol::fs::metadata(entry.path())
                .await
                .is_ok_and(|meta| meta.is_dir()),
            _ => false,
        };
        if is_folder {
            folders.push(FolderEntry::new(
                entry.file_name().to_string_lossy().into_owned(),
            ));
        }
    }
    let style = PathStyle::host();
    folders.sort_by(|a, b| {
        a.hidden()
            .cmp(&b.hidden())
            .then_with(|| style.compare_names(a.name(), b.name()))
    });
    Ok(FolderPage::new(folders))
}

impl Entry for FolderEntry {
    fn name(&self) -> &SharedString {
        &self.name
    }

    fn hidden(&self) -> bool {
        self.hidden
    }

    fn is_folder(&self) -> bool {
        true
    }
}

/// A folder source seen as the browser's source.
pub(super) struct Adapter(pub(super) Rc<dyn FolderSource>);

impl Source<FolderEntry> for Adapter {
    fn list(
        &self,
        dir: &SourcePath,
        page: Option<PageToken>,
        cx: &mut App,
    ) -> Task<Result<Page<FolderEntry>, ListError>> {
        let task = self.0.list(dir, page, cx);
        cx.spawn(async move |_| {
            let page = task.await?;
            Ok(Page {
                entries: page.entries,
                next: page.next,
            })
        })
    }

    fn home(&self) -> Option<SourcePath> {
        self.0.home()
    }

    fn style(&self) -> PathStyle {
        self.0.path_style()
    }

    fn env(&self, name: &str) -> Option<String> {
        self.0.env(name)
    }

    fn can_create_folders(&self) -> bool {
        self.0.can_create_folders()
    }

    fn create_folder(
        &self,
        parent: &SourcePath,
        name: &str,
        cx: &mut App,
    ) -> Task<Result<SourcePath, CreateFolderError>> {
        self.0.create_folder(parent, name, cx)
    }
}

/// Folders held in memory, for fixtures, demos, and tests. It lists a
/// directory the application named, visible folders first and hidden ones
/// after, fails with [`ListError::NotFound`] for any other, and makes
/// folders: a new one shows in its parent's next listing and is an empty
/// directory itself. Clones share the folders.
#[derive(Clone, Debug, Default)]
pub struct MemoryFolders {
    style: PathStyle,
    home: Option<SourcePath>,
    dirs: Rc<RefCell<HashMap<SourcePath, Vec<FolderEntry>>>>,
}

impl MemoryFolders {
    /// A file system with no directories.
    pub fn new() -> Self {
        Self::default()
    }

    /// Writes the paths in `style`. Set it before the home and the
    /// directories. The default is the host's style.
    pub fn with_style(mut self, style: PathStyle) -> Self {
        self.style = style;
        self
    }

    /// Sets the home folder, which `~` stands for.
    pub fn with_home(mut self, home: impl AsRef<str>) -> Self {
        self.home = Some(self.style.path(home));
        self
    }

    /// Adds the directory `path` with the folders `names`.
    pub fn with_dir<S: Into<SharedString>>(
        self,
        path: impl AsRef<str>,
        names: impl IntoIterator<Item = S>,
    ) -> Self {
        let style = self.style;
        let mut entries: Vec<FolderEntry> = names
            .into_iter()
            .map(|name| {
                let name = name.into();
                let hidden = style.is_hidden_name(&name);
                FolderEntry::new(name).with_hidden(hidden)
            })
            .collect();
        sort_folders(&mut entries, &style);
        self.dirs.borrow_mut().insert(style.path(path), entries);
        self
    }
}

fn sort_folders(entries: &mut [FolderEntry], style: &PathStyle) {
    entries.sort_by(|a, b| {
        a.hidden()
            .cmp(&b.hidden())
            .then_with(|| style.compare_names(a.name(), b.name()))
    });
}

impl FolderSource for MemoryFolders {
    fn home(&self) -> Option<SourcePath> {
        self.home.clone()
    }

    fn path_style(&self) -> PathStyle {
        self.style
    }

    fn list(
        &self,
        dir: &SourcePath,
        _page: Option<PageToken>,
        cx: &mut App,
    ) -> Task<Result<FolderPage, ListError>> {
        let found = self.dirs.borrow().get(dir).cloned();
        cx.background_spawn(async move { found.map(FolderPage::new).ok_or(ListError::NotFound) })
    }

    fn can_create_folders(&self) -> bool {
        true
    }

    fn create_folder(
        &self,
        parent: &SourcePath,
        name: &str,
        _: &mut App,
    ) -> Task<Result<SourcePath, CreateFolderError>> {
        let mut dirs = self.dirs.borrow_mut();
        let style = self.style;
        let Some(entries) = dirs.get_mut(parent) else {
            return Task::ready(Err(CreateFolderError::Other(
                "That directory does not exist".into(),
            )));
        };
        if entries
            .iter()
            .any(|entry| style.fold(entry.name()) == style.fold(name))
        {
            return Task::ready(Err(CreateFolderError::Exists));
        }
        let hidden = style.is_hidden_name(name);
        entries.push(FolderEntry::new(name.to_string()).with_hidden(hidden));
        sort_folders(entries, &style);
        let path = parent.join(name);
        dirs.insert(path.clone(), Vec::new());
        Task::ready(Ok(path))
    }
}