Skip to content

Commit

Permalink
fix(lib): add convenience param to get last part of session name
Browse files Browse the repository at this point in the history
Mainly useful for Lualine
  • Loading branch information
cameronr committed Jul 29, 2024
1 parent 0e46cf4 commit d44b2fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,13 @@ require('lualine').setup{
options = {
theme = 'tokyonight',
},
sections = {lualine_c = {require('auto-session.lib').current_session_name}}
sections = {
lualine_c = {
function()
return require('auto-session.lib').current_session_name(true)
end
}
}
}
```

Expand Down
12 changes: 10 additions & 2 deletions lua/auto-session/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ end
---Returns the current session name. For an automatically generated session name, it
---will just be the same as vim.fn.getcwd(). For a named session, it will be the name
---without .vim
---@param tail_only? boolean Only return the last part of a path based session name
---@return string The current session name
function Lib.current_session_name()
function Lib.current_session_name(tail_only)
tail_only = tail_only or false
-- get the filename without the extension
local file_name = vim.fn.fnamemodify(vim.v.this_session, ":t:r")
return Lib.get_session_display_name(file_name)
local session_name = Lib.get_session_display_name(file_name)

if not tail_only then
return session_name
end

return vim.fn.fnamemodify(session_name, ":t")
end

function Lib.is_empty_table(t)
Expand Down
2 changes: 2 additions & 0 deletions tests/cmds_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe("The default config", function()
assert.equals(1, vim.fn.bufexists(TL.test_file))

assert.equals(TL.named_session_name, require("auto-session").Lib.current_session_name())
assert.equals(TL.named_session_name, require("auto-session").Lib.current_session_name(true))
end)

it("can complete session names", function()
Expand Down Expand Up @@ -156,6 +157,7 @@ describe("The default config", function()
assert.equals(1, vim.fn.bufexists(TL.test_file))

assert.equals(vim.fn.getcwd(), require("auto-session.lib").current_session_name())
assert.equals(vim.fn.fnamemodify(vim.fn.getcwd(), ":t"), require("auto-session.lib").current_session_name(true))
end)

it("can purge old sessions", function()
Expand Down

0 comments on commit d44b2fb

Please sign in to comment.