diff --git a/lua/shelltime/utils/system.lua b/lua/shelltime/utils/system.lua index 06ce6b2..3019ffe 100644 --- a/lua/shelltime/utils/system.lua +++ b/lua/shelltime/utils/system.lua @@ -80,11 +80,16 @@ function M.get_project_root(file_path) return vim.fn.fnamemodify(file_path, ':p:h') end ---- Get project name from root path +--- Get project name from root path (last 2 folder layers) ---@param project_root string Project root path ---@return string Project name function M.get_project_name(project_root) - return vim.fn.fnamemodify(project_root, ':t') + local tail = vim.fn.fnamemodify(project_root, ':t') + local parent = vim.fn.fnamemodify(project_root, ':h:t') + if parent and parent ~= '' and parent ~= '/' then + return parent .. '/' .. tail + end + return tail end --- Generate UUID v4 diff --git a/tests/system_spec.lua b/tests/system_spec.lua index 1a599eb..ce5d1e0 100644 --- a/tests/system_spec.lua +++ b/tests/system_spec.lua @@ -181,12 +181,12 @@ describe('shelltime.utils.system', function() end) describe('get_project_name', function() - it('should extract directory name from path', function() + it('should extract last 2 folder layers from path', function() local name = system.get_project_name('/home/user/projects/myapp') - assert.equals('myapp', name) + assert.equals('projects/myapp', name) end) - it('should handle simple path', function() + it('should handle simple path with only 1 layer', function() local name = system.get_project_name('/myapp') assert.equals('myapp', name) end) @@ -196,6 +196,8 @@ describe('shelltime.utils.system', function() local name = system.get_project_name(project_dir) assert.is_string(name) assert.is_true(#name > 0) + -- Should contain a slash for 2 layers + assert.is_true(name:find('/') ~= nil) end) end) end)