Skip to content

Commit

Permalink
{cwd} can optionally shorten only repo parent dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisant996 committed Apr 29, 2022
1 parent 7b42238 commit bf71c23
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions flexprompt_modules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bf71c23

Please sign in to comment.