Skip to content

Commit e00de0a

Browse files
authored
Merge pull request #8 from shelltime/feat/project-name-two-layers
feat(system): use last 2 folder layers for project name
2 parents eaa6746 + c1e1815 commit e00de0a

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

‎lua/shelltime/utils/system.lua‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,16 @@ function M.get_project_root(file_path)
8080
return vim.fn.fnamemodify(file_path, ':p:h')
8181
end
8282

83-
--- Get project name from root path
83+
--- Get project name from root path (last 2 folder layers)
8484
---@param project_root string Project root path
8585
---@return string Project name
8686
function M.get_project_name(project_root)
87-
return vim.fn.fnamemodify(project_root, ':t')
87+
local tail = vim.fn.fnamemodify(project_root, ':t')
88+
local parent = vim.fn.fnamemodify(project_root, ':h:t')
89+
if parent and parent ~= '' and parent ~= '/' then
90+
return parent .. '/' .. tail
91+
end
92+
return tail
8893
end
8994

9095
--- Generate UUID v4

‎tests/system_spec.lua‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ describe('shelltime.utils.system', function()
181181
end)
182182

183183
describe('get_project_name', function()
184-
it('should extract directory name from path', function()
184+
it('should extract last 2 folder layers from path', function()
185185
local name = system.get_project_name('/home/user/projects/myapp')
186-
assert.equals('myapp', name)
186+
assert.equals('projects/myapp', name)
187187
end)
188188

189-
it('should handle simple path', function()
189+
it('should handle simple path with only 1 layer', function()
190190
local name = system.get_project_name('/myapp')
191191
assert.equals('myapp', name)
192192
end)
@@ -196,6 +196,8 @@ describe('shelltime.utils.system', function()
196196
local name = system.get_project_name(project_dir)
197197
assert.is_string(name)
198198
assert.is_true(#name > 0)
199+
-- Should contain a slash for 2 layers
200+
assert.is_true(name:find('/') ~= nil)
199201
end)
200202
end)
201203
end)

0 commit comments

Comments
 (0)