add "session manager" (telescope picker)
authorLukas Hägele <lukas.haegele93@web.de>
Tue, 6 Jan 2026 14:42:34 +0000 (15:42 +0100)
committerLukas Hägele <lukas.haegele93@web.de>
Tue, 6 Jan 2026 14:42:34 +0000 (15:42 +0100)
plugin/sessionmanager.lua [new file with mode: 0644]

diff --git a/plugin/sessionmanager.lua b/plugin/sessionmanager.lua
new file mode 100644 (file)
index 0000000..d4ac078
--- /dev/null
@@ -0,0 +1,96 @@
+local pickers = require "telescope.pickers"
+local finders = require "telescope.finders"
+local conf = require("telescope.config").values
+local actions = require "telescope.actions"
+local action_state = require "telescope.actions.state"
+
+local history = {}
+
+--- show session history
+local function session_picker(opts)
+  opts = opts or require("telescope.themes").get_dropdown{}
+
+  -- rearrange history
+  local results = {}
+  for entry_path, _ in pairs(history) do
+    table.insert(results, entry_path)
+  end
+
+  pickers.new(opts, {
+    prompt_title = "session history",
+    finder = finders.new_table {
+      results = results,
+    },
+
+    sorter = conf.generic_sorter(opts),
+
+    attach_mappings = function(prompt_bufnr, map)
+      actions.select_default:replace(function()
+        actions.close(prompt_bufnr)
+
+        -- close all splits (by closing all other windows)
+        vim.cmd("only")
+
+        -- close all buffers
+        vim.cmd("bufdo bdelete")
+
+        local selection = action_state.get_selected_entry()
+        local dir = selection[1]
+        -- change the current directory
+        vim.cmd("cd" ..dir)
+        -- open file browser
+        vim.cmd("Explore" ..dir)
+      end)
+      return true
+    end
+  }):find()
+end
+
+-- read and update history file on disk
+do
+  local history_dir = vim.fn.stdpath("state") .."/sessionmanager/"
+  local history_file = history_dir .. "history.lua"
+
+  -- read history from disk
+  do
+    function project(entry)
+      history[entry.path] = entry
+    end
+    -- tolerate missing history file
+    local success, error = pcall(dofile, history_file)
+  end
+
+  -- add current directory to history
+  do
+    local function serialize()
+      local str = ""
+      for _, entry in pairs(history) do
+        str = str .. "project { path = \"" .. entry.path .. "\" }\n"
+      end
+
+      return str
+    end
+
+    local cwd = vim.fn.getcwd()
+    -- TODO: only save, when nvim was opened in a directory?
+    -- check if directory is opened for the first time
+    if history[cwd] == nil then
+      local entry = { path = cwd }
+      history[cwd] = entry
+
+      -- serialize history
+      local history_str = serialize()
+
+      -- write to disk
+      vim.fn.mkdir(history_dir, "p")
+      local f = assert(io.open(history_file, "w"))
+      f:write(history_str)
+      f:close()
+    end
+  end
+end
+
+-- register with vim
+vim.api.nvim_create_user_command("Session Manager", session_picker, {})
+vim.keymap.set("n", "<space>fs", session_picker, { desc = "show session picker" })
+