Skip to content

A treemap for finding and removing what fills your disk, for Omarchy. Rust + GPUI.

Rust

app: run on Windows

Oleg Shevchuk5aa98e38180b
diff --git a/Cargo.lock b/Cargo.lock
index 832669d..ee104b2 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1424,10 +1424,12 @@ version = "0.9.1"
dependencies = [
"anyhow",
"disktree-core",
+ "dunce",
"gpui-kit",
"gpui-omarchy",
"rustc-hash 2.1.3",
"tempfile",
+ "windows-sys 0.61.2",
]
[[package]]
diff --git a/crates/disktree-app/Cargo.toml b/crates/disktree-app/Cargo.toml
index 63644e0..02d93aa 100644
--- a/crates/disktree-app/Cargo.toml
+++ b/crates/disktree-app/Cargo.toml
@@ -18,8 +18,12 @@ gpui-kit = { version = "=0.6.1", default-features = false, features = ["assets"]
gpui-omarchy = "0.1.2"
disktree-core = { workspace = true }
anyhow = { workspace = true }
+dunce = "1.0.5"
rustc-hash = { workspace = true }
+[target.'cfg(windows)'.dependencies]
+windows-sys = { version = "0.61", features = ["Win32_System_Console"] }
+
[dev-dependencies]
# The window harness: it lets the tests draw a real frame and press real keys.
gpui-kit = { version = "=0.6.1", default-features = false, features = [
diff --git a/crates/disktree-app/src/main.rs b/crates/disktree-app/src/main.rs
index 22b8265..6d17cfd 100644
--- a/crates/disktree-app/src/main.rs
+++ b/crates/disktree-app/src/main.rs
@@ -5,6 +5,12 @@
//! live free-space meter. Marking is non-destructive until the review screen
//! is confirmed.
+// A window, not a console program: on Windows, opening it from Explorer or
+// the Start menu should not bring a console window along. `main` attaches to
+// the console of a terminal it was started from, so `--help` and errors still
+// reach one. Ignored elsewhere.
+#![windows_subsystem = "windows"]
+
mod git;
mod marks;
mod palette;
@@ -56,6 +62,15 @@ options:
";
fn main() -> Result<()> {
+ #[cfg(windows)]
+ console::attach();
+ let outcome = run();
+ #[cfg(windows)]
+ console::detach();
+ outcome
+}
+
+fn run() -> Result<()> {
let args = parse_args()?;
let root = args.root.clone();
let depth = args.depth;
@@ -82,9 +97,7 @@ fn main() -> Result<()> {
"disktree · {}",
marks::display_path(
&title_root,
- std::env::var_os("HOME")
- .map(PathBuf::from)
- .as_deref(),
+ std::env::home_dir().as_deref(),
)
)
.into(),
@@ -173,21 +186,21 @@ fn parse_args() -> Result<Args> {
!(disk && root.is_some()),
"--disk and a PATH cannot be combined"
);
- let home = std::env::var_os("HOME").map(PathBuf::from);
+ let home = std::env::home_dir();
let root = match root {
_ if disk => home
.as_deref()
.and_then(disktree_core::space::volume_root_for)
.unwrap_or_else(|| PathBuf::from("/")),
Some(root) => root,
- None => std::env::var_os("HOME")
- .map(PathBuf::from)
- .context("no path given and HOME is not set")?,
+ None => home.context("no path given and no home directory")?,
};
// Store the depth as the initial view setting rather than a scan option: it
// is a display choice the run-time `[` and `]` keys also change.
- // Canonical, so a later widening recognises this tree in the wider walk.
- let root = root.canonicalize().unwrap_or(root);
+ // Canonical, so a later widening recognises this tree in the wider walk;
+ // through dunce, so Windows gets `C:\Users\…` rather than the `\\?\C:\…`
+ // form nothing else is written in.
+ let root = dunce::canonicalize(&root).unwrap_or(root);
let metadata = std::fs::metadata(&root)
.with_context(|| format!("cannot read {}", root.display()))?;
anyhow::ensure!(metadata.is_dir(), "{} is not a directory", root.display());
@@ -198,3 +211,35 @@ fn parse_args() -> Result<Args> {
depth: depth.clamp(1, 6),
})
}
+
+/// The console of the terminal disktree was started from, if any: a
+/// windowed program on Windows gets none of its own.
+#[cfg(windows)]
+mod console {
+ #![allow(
+ unsafe_code,
+ reason = "two Win32 calls that take no pointers to get wrong"
+ )]
+
+ use windows_sys::Win32::System::Console::{
+ ATTACH_PARENT_PROCESS, AttachConsole, FreeConsole,
+ };
+
+ /// Borrow the parent's console so printed text reaches it. Does
+ /// nothing when started from Explorer, which has none.
+ pub fn attach() {
+ // SAFETY: takes a process id by value, and failure only means
+ // there was no console to attach to.
+ unsafe {
+ AttachConsole(ATTACH_PARENT_PROCESS);
+ }
+ }
+
+ /// Let go of it again, so the shell redraws its prompt.
+ pub fn detach() {
+ // SAFETY: no arguments; a process without a console is left as is.
+ unsafe {
+ FreeConsole();
+ }
+ }
+}
diff --git a/crates/disktree-app/src/marks.rs b/crates/disktree-app/src/marks.rs
index 51483ce..89141ae 100644
--- a/crates/disktree-app/src/marks.rs
+++ b/crates/disktree-app/src/marks.rs
@@ -107,13 +107,16 @@ pub fn is_hidden(path: &Path) -> bool {
}
/// Shorten a path for display: `~` for the home directory, and the path with
-/// the home prefix replaced when it is below it.
+/// the home prefix replaced when it is below it. The separator after `~` is
+/// the platform's, so Windows shows `~\AppData\Local`, not `~/AppData\Local`.
pub fn display_path(path: &Path, home: Option<&Path>) -> String {
match home
.and_then(|home| path.strip_prefix(home).ok().map(|rest| (home, rest)))
{
Some((_, rest)) if rest.as_os_str().is_empty() => "~".to_string(),
- Some((_, rest)) => format!("~/{}", rest.display()),
+ Some((_, rest)) => {
+ format!("~{}{}", std::path::MAIN_SEPARATOR, rest.display())
+ }
None => path.display().to_string(),
}
}
@@ -220,9 +223,10 @@ mod tests {
#[test]
fn display_path_shortens_the_home_prefix() {
let home = Path::new("/home/tobi");
+ let separator = std::path::MAIN_SEPARATOR;
assert_eq!(
- display_path(Path::new("/home/tobi/.cache/npm"), Some(home)),
- "~/.cache/npm"
+ display_path(&home.join(".cache").join("npm"), Some(home)),
+ format!("~{separator}.cache{separator}npm")
);
assert_eq!(display_path(home, Some(home)), "~");
assert_eq!(display_path(Path::new("/var/log"), Some(home)), "/var/log");
diff --git a/crates/disktree-app/src/state.rs b/crates/disktree-app/src/state.rs
index 6a2212a..4d8212c 100644
--- a/crates/disktree-app/src/state.rs
+++ b/crates/disktree-app/src/state.rs
@@ -414,7 +414,7 @@ impl Disktree {
depth: u32,
cx: &mut Context<'_, Self>,
) -> Self {
- let home = std::env::var_os("HOME").map(PathBuf::from);
+ let home = std::env::home_dir();
let space = space_info(&root_path).ok();
let trash_backend = detect_trash_backend();
let mut tree = Self {
diff --git a/crates/disktree-app/src/tests.rs b/crates/disktree-app/src/tests.rs
index 946b06a..846f359 100644
--- a/crates/disktree-app/src/tests.rs
+++ b/crates/disktree-app/src/tests.rs
@@ -943,9 +943,11 @@ fn widening_reuses_the_tree_it_has_and_reads_only_the_rest(
});
let before = read(&view, cx, |app| app.tree().map(|tree| tree.files));
- // The trail runs from "/", and the scanned root sits under its parents.
+ // The trail runs from the top of the filesystem — "/", or a drive such
+ // as "C:\" — and the scanned root sits under its parents.
let trail = read(&view, cx, Disktree::breadcrumbs);
- assert_eq!(trail[0].0, "/");
+ let top = temp.path().ancestors().last().expect("a top");
+ assert_eq!(trail[0].0, top.display().to_string());
assert!(
trail.contains(&(
temp.path()
diff --git a/crates/disktree-app/src/views.rs b/crates/disktree-app/src/views.rs
index ca7b193..4062ce5 100644
--- a/crates/disktree-app/src/views.rs
+++ b/crates/disktree-app/src/views.rs
@@ -363,11 +363,13 @@ fn trail(app: &Disktree, theme: &Theme, cx: &Context<'_, Disktree>) -> Div {
/// Steps a trail shows before it folds its middle into an ellipsis.
const TRAIL_STEPS: usize = 7;
+/// The platform's path separator, so the trail reads like the paths shown
+/// elsewhere: `/` here, `\` on Windows.
fn separator_glyph(theme: &Theme) -> Div {
div()
.text_color(theme.secondary.opacity(0.5))
.text_size(text::BODY)
- .child("/")
+ .child(std::path::MAIN_SEPARATOR_STR)
}
/// A crumb in the tree. Its label goes there (the current one opens the