Skip to content

Commit

Permalink
Open picker when :ObsidianWorkspace called without arg
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh committed Feb 11, 2024
1 parent 665c609 commit 14889af
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Major internal refactoring / improvements for how we integrate with pickers.
- Configuration option `finder` and `finder_mappings` have been consolidated into `picker = { name: string, mappings: { ... } }`.
- When `:ObsidianWorkspace` is called without any arguments, obsidian.nvim will open your picker to select a workspace to switch to.

### Fixed

Expand Down
26 changes: 24 additions & 2 deletions lua/obsidian/commands/workspace.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
local log = require "obsidian.log"
local Workspace = require "obsidian.workspace"

---@param client obsidian.Client
return function(client, data)
if not data.args or string.len(data.args) == 0 then
log.info("Current workspace: '%s' @ '%s'", client.current_workspace.name, client.current_workspace.path)
return
local picker = client:picker()
if not picker then
log.info("Current workspace: '%s' @ '%s'", client.current_workspace.name, client.current_workspace.path)
return
end

local options = {}
for i, spec in ipairs(client.opts.workspaces) do
local workspace = Workspace.new_from_spec(spec)
if workspace == client.current_workspace then
options[#options + 1] = string.format("*[%d] %s @ '%s'", i, workspace.name, workspace.path)
else
options[#options + 1] = string.format("[%d] %s @ '%s'", i, workspace.name, workspace.path)
end
end

picker:pick(options, {
prompt_title = "Workspaces",
callback = function(workspace_str)
local idx = tonumber(string.match(workspace_str, "%*?%[(%d+)]"))
client:switch_workspace(client.opts.workspaces[idx].name, { lock = true })
end,
})
else
client:switch_workspace(data.args, { lock = true })
end
Expand Down
7 changes: 7 additions & 0 deletions lua/obsidian/workspace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ local Workspace = abc.new_class {
__tostring = function(self)
return string.format("Workspace(name='%s', path='%s', root='%s')", self.name, self.path, self.root)
end,
__eq = function(a, b)
local a_fields = a:as_tbl()
a_fields.locked = nil
local b_fields = b:as_tbl()
b_fields.locked = nil
return vim.deep_equal(a_fields, b_fields)
end,
}

--- Find the vault root from a given directory.
Expand Down

0 comments on commit 14889af

Please sign in to comment.