ui.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. local M = {}
  2. --- Prompts the user to pick a single item from a collection of entries
  3. ---
  4. ---@param items table Arbitrary items
  5. ---@param opts table Additional options
  6. --- - prompt (string|nil)
  7. --- Text of the prompt. Defaults to `Select one of:`
  8. --- - format_item (function item -> text)
  9. --- Function to format an
  10. --- individual item from `items`. Defaults to `tostring`.
  11. --- - kind (string|nil)
  12. --- Arbitrary hint string indicating the item shape.
  13. --- Plugins reimplementing `vim.ui.select` may wish to
  14. --- use this to infer the structure or semantics of
  15. --- `items`, or the context in which select() was called.
  16. ---@param on_choice function ((item|nil, idx|nil) -> ())
  17. --- Called once the user made a choice.
  18. --- `idx` is the 1-based index of `item` within `items`.
  19. --- `nil` if the user aborted the dialog.
  20. ---
  21. ---
  22. --- Example:
  23. --- <pre>
  24. --- vim.ui.select({ 'tabs', 'spaces' }, {
  25. --- prompt = 'Select tabs or spaces:',
  26. --- format_item = function(item)
  27. --- return "I'd like to choose " .. item
  28. --- end,
  29. --- }, function(choice)
  30. --- if choice == 'spaces' then
  31. --- vim.o.expandtab = true
  32. --- else
  33. --- vim.o.expandtab = false
  34. --- end
  35. --- end)
  36. --- </pre>
  37. function M.select(items, opts, on_choice)
  38. vim.validate {
  39. items = { items, 'table', false },
  40. on_choice = { on_choice, 'function', false },
  41. }
  42. opts = opts or {}
  43. local choices = {opts.prompt or 'Select one of:'}
  44. local format_item = opts.format_item or tostring
  45. for i, item in pairs(items) do
  46. table.insert(choices, string.format('%d: %s', i, format_item(item)))
  47. end
  48. local choice = vim.fn.inputlist(choices)
  49. if choice < 1 or choice > #items then
  50. on_choice(nil, nil)
  51. else
  52. on_choice(items[choice], choice)
  53. end
  54. end
  55. --- Prompts the user for input
  56. ---
  57. ---@param opts table Additional options. See |input()|
  58. --- - prompt (string|nil)
  59. --- Text of the prompt. Defaults to `Input: `.
  60. --- - default (string|nil)
  61. --- Default reply to the input
  62. --- - completion (string|nil)
  63. --- Specifies type of completion supported
  64. --- for input. Supported types are the same
  65. --- that can be supplied to a user-defined
  66. --- command using the "-complete=" argument.
  67. --- See |:command-completion|
  68. --- - highlight (function)
  69. --- Function that will be used for highlighting
  70. --- user inputs.
  71. ---@param on_confirm function ((input|nil) -> ())
  72. --- Called once the user confirms or abort the input.
  73. --- `input` is what the user typed.
  74. --- `nil` if the user aborted the dialog.
  75. ---
  76. --- Example:
  77. --- <pre>
  78. --- vim.ui.input({ prompt = 'Enter value for shiftwidth: ' }, function(input)
  79. --- vim.o.shiftwidth = tonumber(input)
  80. --- end)
  81. --- </pre>
  82. function M.input(opts, on_confirm)
  83. vim.validate {
  84. on_confirm = { on_confirm, 'function', false },
  85. }
  86. opts = opts or {}
  87. local input = vim.fn.input(opts)
  88. if #input > 0 then
  89. on_confirm(input)
  90. else
  91. on_confirm(nil)
  92. end
  93. end
  94. return M