Skip to content

Commit 6df68b4

Browse files
committed
(WIP) Old UI return
1 parent 9b529ff commit 6df68b4

4 files changed

Lines changed: 422 additions & 19 deletions

File tree

README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ vim.keymap.set('n', '<leader>sp', '<cmd>lua require("spectre").open_file_search(
4949
})
5050
```
5151

52+
You can also toggle between the legacy UI and the modern nui-based UI:
53+
54+
```lua
55+
-- Toggle between legacy and modern UI
56+
vim.keymap.set('n', '<leader>sU', '<cmd>lua require("spectre").toggle_ui()<CR>', {
57+
desc = "Toggle between legacy and modern UI"
58+
})
59+
60+
-- Switch to a specific UI
61+
vim.keymap.set('n', '<leader>su', '<cmd>lua require("spectre").set_ui_type(false)<CR>', {
62+
desc = "Use modern UI"
63+
})
64+
vim.keymap.set('n', '<leader>sL', '<cmd>lua require("spectre").set_ui_type(true)<CR>', {
65+
desc = "Use legacy UI"
66+
})
67+
```
68+
5269
Use command: `:Spectre`
5370

5471
## Warnings
@@ -286,12 +303,9 @@ require('spectre').setup({
286303
use_trouble_qf = false, -- use trouble.nvim as quickfix list
287304
is_open_target_win = true, --open file on opener window
288305
is_insert_mode = false, -- start open panel on is_insert_mode
289-
is_block_ui_break = false -- mapping backspace and enter key to avoid ui break
290-
open_template = {
291-
-- an template to use on open function
292-
-- see the 'custom function' section below to learn how to configure the template
293-
-- { search_text = 'text1', replace_text = '', path = "" }
294-
}
306+
is_block_ui_break = false, -- mapping backspace and enter key to avoid ui break
307+
open_template = {}, -- an template to use on open function
308+
use_legacy_ui = false, -- set to true to use the legacy UI instead of nui-based modern UI
295309
})
296310

297311
```
@@ -398,6 +412,4 @@ require('spectre').setup({ is_block_ui_break = true })
398412
399413
- Why is it called Spectre?
400414

401-
I wanted to call it `Search Panel` but this name is not cool.
402-
I got the name of a hero on a game.
403-
Spectre has a skill to find enemy on global map so I use it:)
415+
I wanted to call it `

lua/spectre/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ local config = {
220220
is_insert_mode = false,
221221
is_block_ui_break = false,
222222
open_template = {},
223+
use_legacy_ui = false, -- set to true to use the legacy UI
223224
}
224225

225226
return config

lua/spectre/init.lua

Lines changed: 124 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ local config = require('spectre.config')
2424
local state = require('spectre.state')
2525
local state_utils = require('spectre.state_utils')
2626
local utils = require('spectre.utils')
27-
local ui = require('spectre.ui.nui_components')
27+
-- Dynamically choose UI based on configuration
28+
local ui = nil
2829
local log = require('spectre._log')
2930
local async = require('plenary.async')
3031

@@ -40,6 +41,18 @@ M.setup = function(opts)
4041
end
4142
require('spectre.highlight').set_hl()
4243
M.check_replace_cmd_bins()
44+
45+
-- Initialize UI based on configuration
46+
M.init_ui()
47+
end
48+
49+
-- Initialize UI based on user config
50+
M.init_ui = function()
51+
if state.user_config.use_legacy_ui then
52+
ui = require('spectre.ui.legacy')
53+
else
54+
ui = require('spectre.ui.nui_components')
55+
end
4356
end
4457

4558
M.check_replace_cmd_bins = function()
@@ -80,11 +93,20 @@ M.open = function(opts)
8093
}
8194
state.regex = nil
8295

96+
-- Ensure UI is initialized
97+
if ui == nil then
98+
M.init_ui()
99+
end
100+
83101
ui.open()
84102
end
85103

86104
M.close = function()
87105
state.is_open = false
106+
-- Ensure UI is initialized
107+
if ui == nil then
108+
M.init_ui()
109+
end
88110
ui.close()
89111
end
90112

@@ -182,14 +204,26 @@ M.change_options = function(key)
182204
end
183205
state.regex.change_options(state_utils.get_replace_engine_config().options_value)
184206
if state.query.search_query ~= nil then
185-
ui.render_search_ui()
207+
-- Ensure UI is initialized
208+
if ui == nil then
209+
M.init_ui()
210+
end
211+
212+
if ui and ui.render_search_ui then
213+
ui.render_search_ui()
214+
end
186215
M.search()
187216
end
188217
end
189218

190219
M.show_options = function()
191-
if not ui then return end
192-
ui.show_options()
220+
if not ui then
221+
M.init_ui()
222+
end
223+
224+
if ui and ui.show_options then
225+
ui.show_options()
226+
end
193227
end
194228

195229
M.get_fold = function(lnum)
@@ -215,18 +249,98 @@ M.get_fold = function(lnum)
215249
end
216250

217251
M.tab = function()
218-
if not ui then return end
219-
ui.tab()
252+
if not ui then
253+
M.init_ui()
254+
end
255+
256+
if ui and ui.tab then
257+
ui.tab()
258+
end
220259
end
221260

222261
M.tab_shift = function()
223-
if not ui then return end
224-
ui.tab_shift()
262+
if not ui then
263+
M.init_ui()
264+
end
265+
266+
if ui and ui.tab_shift then
267+
ui.tab_shift()
268+
end
225269
end
226270

227271
M.toggle_preview = function()
228-
if not ui then return end
229-
ui.toggle_preview()
272+
if not ui then
273+
M.init_ui()
274+
end
275+
276+
if ui and ui.toggle_preview then
277+
ui.toggle_preview()
278+
end
279+
end
280+
281+
-- Function to toggle between different view modes
282+
M.change_view = function()
283+
if not ui then
284+
M.init_ui()
285+
end
286+
287+
-- Toggle view mode
288+
if state.view.mode == "both" then
289+
state.view.mode = "replace"
290+
state.view.show_search = false
291+
state.view.show_replace = true
292+
elseif state.view.mode == "replace" then
293+
state.view.mode = "search"
294+
state.view.show_search = true
295+
state.view.show_replace = false
296+
else
297+
state.view.mode = "both"
298+
state.view.show_search = true
299+
state.view.show_replace = true
300+
end
301+
302+
-- Trigger UI update if available
303+
if ui and ui.render_search_ui then
304+
ui.render_search_ui()
305+
end
306+
end
307+
308+
-- Function to toggle between UI types
309+
M.toggle_ui = function()
310+
state.user_config.use_legacy_ui = not state.user_config.use_legacy_ui
311+
312+
-- Re-initialize the UI
313+
M.init_ui()
314+
315+
-- Notify the user
316+
local ui_type = state.user_config.use_legacy_ui and "legacy" or "modern"
317+
vim.notify("Switched to " .. ui_type .. " UI. Reopen spectre panel to apply changes.", vim.log.levels.INFO)
318+
319+
-- If spectre is open, close and reopen it to apply changes
320+
if state.is_open then
321+
local query_backup = vim.deepcopy(state.query)
322+
M.close()
323+
M.open(query_backup)
324+
end
325+
end
326+
327+
-- Function to set the UI type
328+
M.set_ui_type = function(use_legacy)
329+
state.user_config.use_legacy_ui = use_legacy
330+
331+
-- Re-initialize the UI
332+
M.init_ui()
333+
334+
-- Notify the user
335+
local ui_type = state.user_config.use_legacy_ui and "legacy" or "modern"
336+
vim.notify("Set to " .. ui_type .. " UI. Reopen spectre panel to apply changes.", vim.log.levels.INFO)
337+
338+
-- If spectre is open, close and reopen it to apply changes
339+
if state.is_open then
340+
local query_backup = vim.deepcopy(state.query)
341+
M.close()
342+
M.open(query_backup)
343+
end
230344
end
231345

232346
return M

0 commit comments

Comments
 (0)