Skip to main content

contract_tilde

Function contract_tilde 

Source
pub fn contract_tilde(raw: &Path) -> String
Expand description

expand_tilde’s inverse: put the ~ back for DISPLAY.

/Users/dhruva/src/lattice → ~/src/lattice. For showing a path in a prompt, a status line or any other one-line surface where the home prefix is the least informative part of it and the part that squeezes out the rest.

Display only. The result is not a path to hand to anything that opens files — that is what the expanding direction is for, and round-tripping through here would be a way to lose a path whose home resolution changed underneath it.

Home itself contracts to ~, not ~/. Anything outside home, a non-UTF-8 path, or an unresolvable home is returned verbatim, so the failure is a path that reads slightly long rather than one that reads wrong.