|
| 1 | +local M = {} |
| 2 | + |
| 3 | +local function ellipsize(s, max_len) |
| 4 | + if vim.fn.strdisplaywidth(s) <= max_len then |
| 5 | + return s |
| 6 | + end |
| 7 | + local truncated = vim.fn.strcharpart(s, 0, max_len - 3) |
| 8 | + truncated = truncated:gsub("%s+%S*$", "") |
| 9 | + |
| 10 | + return truncated .. "..." |
| 11 | +end |
| 12 | + |
| 13 | +function M.select_session() |
| 14 | + require("opencode.cli.server") |
| 15 | + .get_port() |
| 16 | + :next(function(port) |
| 17 | + return require("opencode.promise").new(function(resolve) |
| 18 | + require("opencode.cli.client").get_sessions(port, function(sessions) |
| 19 | + resolve({ sessions = sessions, port = port }) |
| 20 | + end) |
| 21 | + end) |
| 22 | + end) |
| 23 | + :next(function(session_data) |
| 24 | + local sessions = session_data.sessions |
| 25 | + table.sort(sessions, function(a, b) |
| 26 | + return a.time.updated > b.time.updated |
| 27 | + end) |
| 28 | + |
| 29 | + vim.ui.select(sessions, { |
| 30 | + prompt = "Select session (recently updated first):", |
| 31 | + format_item = function(item) |
| 32 | + local title_length = 60 |
| 33 | + local updated = os.date("%b %d, %Y %H:%M:%S", item.time.updated / 1000) |
| 34 | + local title = ellipsize(item.title, title_length) |
| 35 | + return ("%s%s%s"):format(title, string.rep(" ", title_length - #title), updated) |
| 36 | + end, |
| 37 | + }, function(choice) |
| 38 | + if choice then |
| 39 | + require("opencode.cli.client").select_session(session_data.port, choice.id) |
| 40 | + end |
| 41 | + end) |
| 42 | + end) |
| 43 | + :catch(function(err) |
| 44 | + vim.notify(err, vim.log.levels.ERROR) |
| 45 | + end) |
| 46 | +end |
| 47 | + |
| 48 | +return M |
0 commit comments