~fmac/androsace

1c41beddfddd5a88dd1b51b2bc2c42c08285d08f — Francesco Camuffo 3 months ago d7a81b0
add seg cwd
2 files changed, 35 insertions(+), 0 deletions(-)

M cmd/androsace/main.ha
A segs/cwd.ha
M cmd/androsace/main.ha => cmd/androsace/main.ha +2 -0
@@ 8,6 8,8 @@ use strings;
export fn main() void = {
	for (let i = 1z; i < len(os::args); i += 1) {
		let segfn: nullable *fn() str = switch (os::args[i]) {
		case "cwd" =>
			yield &segs::seg_cwd;
		case "hostname" =>
			yield &segs::seg_hostname;
		case "tail" =>

A segs/cwd.ha => segs/cwd.ha +33 -0
@@ 0,0 1,33 @@
use os;
use strings;

// CWD with contracted path
export fn seg_cwd() str = {
	let cwd = os::getcwd();
	const home = os::tryenv("HOME", "");

	if (strings::hasprefix(cwd, home) == true) {
		cwd = strings::concat("~", strings::cut(cwd, home).1);
	};

	const ss = strings::split(cwd, "/");
	cwd = "";
	for (let i = 0z; i < len(ss); i += 1) {
		const s = ss[i];
		if (i == len(ss) - 1) {
			cwd = strings::concat(cwd, s);
		} else {
			if (len(s) == 0) {
				cwd = strings::concat(cwd, "/");
				continue;
			};
			if (strings::hasprefix(s, ".")) {
				cwd = strings::concat(cwd, strings::sub(s, 0, 2), "/");
			} else {
				cwd = strings::concat(cwd, strings::sub(s, 0, 1), "/");
			};
		};
	};

	return cwd;
};