From bf71c232ce79e74b026c47c46c1ff4c448b67b3a Mon Sep 17 00:00:00 2001 From: Chris Antos Date: Fri, 29 Apr 2022 08:11:48 -0700 Subject: [PATCH] {cwd} can optionally shorten only repo parent dir. --- flexprompt_modules.lua | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/flexprompt_modules.lua b/flexprompt_modules.lua index fde9bcb..e20acd8 100644 --- a/flexprompt_modules.lua +++ b/flexprompt_modules.lua @@ -158,6 +158,9 @@ end -- - "rootsmart" is the full path, with parent of git repo not colored. -- -- The 'shorten' option abbreviates parent directories to only the first letter. +-- The 'shorten' option may optionally be followed by "=rootsmart" to abbreviate +-- only the repo's parent directories when in a git repo (otherwise abbreviate +-- all the parent directories). -- -- The default type is "rootsmart" if not specified. @@ -175,11 +178,16 @@ local function get_folder_name(dir) return dir end -local function abbreviate_parents(dir, ref) - local tmp, suffix = path.toparent(dir) +local function abbreviate_parents(dir, all) + local tmp, suffix + if all then + tmp = dir + else + tmp, suffix = path.toparent(dir) + end tmp = tmp:gsub("^([!-.0-%][-~])[^:/\\]*", "%1") tmp = tmp:gsub("([/\\][!-.0-%][-~])[^/\\]*", "%1") - if suffix ~= "" then + if suffix and suffix ~= "" then tmp = path.join(tmp, suffix) end return tmp @@ -197,7 +205,11 @@ local function render_cwd(args) color = flexprompt.use_best_color("blue", "38;5;33") end color, altcolor = flexprompt.parse_colors(colors, color, altcolor) - local shorten = flexprompt.parse_arg_keyword(args, "s", "shorten") + + local shorten = flexprompt.parse_arg_keyword(args, "s", "shorten") and "all" + if not shorten then + shorten = flexprompt.parse_arg_token(args, "s", "shorten") + end local wizard = flexprompt.get_wizard_state() local cwd = wizard and wizard.cwd or os.getcwd() @@ -240,12 +252,14 @@ local function render_cwd(args) local smart_dir = get_folder_name(git_wks_parent) .. appended_dir if type == "rootsmart" then local rootcolor = flexprompt.parse_arg_token(args, "rc", "rootcolor") + local parent = cwd:sub(1, #cwd - #smart_dir) if shorten then - cwd = abbreviate_parents(cwd) - smart_dir = abbreviate_parents(smart_dir) + parent = abbreviate_parents(parent, true--[[all]]) + if shorten ~= "smartroot" then + smart_dir = abbreviate_parents(smart_dir) + end shorten = nil end - local parent = cwd:sub(1, #cwd - #smart_dir) cwd = flexprompt.make_fluent_text(parent, rootcolor or true) .. smart_dir else cwd = smart_dir