Skip to content

Commit

Permalink
feat: use vim.o.guicursor to hide real cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
sphamba committed Feb 2, 2025
1 parent 9b04545 commit fbba581
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ As an example of further configuration, you can tune the smear dynamics to be sn
stiffness = 0.8, -- 0.6 [0, 1]
trailing_stiffness = 0.5, -- 0.3 [0, 1]
distance_stop_animating = 0.5, -- 0.1 > 0
hide_target_hack = false, -- true boolean
},
```
Expand All @@ -117,6 +116,7 @@ As an example of further configuration, you can tune the smear dynamics to be sn
> stiffness = 0.3,
> trailing_stiffness = 0.1,
> trailing_exponent = 5,
> hide_target_hack = true,
> gamma = 1,
> }
> ```
Expand Down
62 changes: 41 additions & 21 deletions lua/smear_cursor/animation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ local current_top_row = -1
local previous_line = -1
local current_line = -1

local original_guicursor = nil
local cursor_hidden = false

local function cursor_is_vertical_bar()
if vim.api.nvim_get_mode().mode == "i" then
return config.vertical_bar_cursor_insert_mode
Expand Down Expand Up @@ -166,6 +169,25 @@ local function stop_animation()
animating = false
end

local function hide_cursor()
if not config.hide_target_hack then
if cursor_hidden then return end
original_guicursor = vim.o.guicursor
cursor_hidden = true
vim.o.guicursor = "a:SmearCursorHidden"
elseif not cursor_is_vertical_bar() then
local character = ""
draw.draw_character(target_position[1], target_position[2], character, color.get_hl_group())
end
end

local function unhide_cursor()
if not cursor_hidden then return end
cursor_hidden = false

vim.o.guicursor = original_guicursor
end

local function animate()
animating = true
update()
Expand All @@ -191,6 +213,7 @@ local function animate()
then
set_corners(current_corners, target_position[1], target_position[2])
if vim.api.nvim_get_mode().mode == "c" then vim.cmd.redraw() end
unhide_cursor()
stop_animation()
return
end
Expand All @@ -202,31 +225,27 @@ local function animate()
or math.abs(target_center[2] - current_center[2]) < 1 / 8
local drawn_corners = straight_line and current_corners or shrink_volume(current_corners)

local vertical_bar = cursor_is_vertical_bar()

if config.hide_target_hack and not vertical_bar then
local target_reached = false
for i = 1, 4 do
-- stylua: ignore
local target_reached = (
math.floor(drawn_corners[1][1]) == target_position[1] and
math.floor(drawn_corners[1][2]) == target_position[2]
) or (
math.floor(drawn_corners[2][1]) == target_position[1] and
math.ceil(drawn_corners[2][2]) - 1 == target_position[2]
) or (
math.ceil(drawn_corners[3][1]) - 1 == target_position[1] and
math.ceil(drawn_corners[3][2]) - 1 == target_position[2]
) or (
math.ceil(drawn_corners[4][1]) - 1 == target_position[1] and
math.floor(drawn_corners[4][2]) == target_position[2]
)

if not target_reached then
local character = vertical_bar and "" or ""
draw.draw_character(target_position[1], target_position[2], character, color.get_hl_group())
if (
drawn_corners[i][1] >= target_corners[1][1] and
drawn_corners[i][1] <= target_corners[3][1] and
drawn_corners[i][2] >= target_corners[1][2] and
drawn_corners[i][2] <= target_corners[3][2]
) then
target_reached = true
break
end
end

draw.draw_quad(drawn_corners, target_position, vertical_bar)
if target_reached then
unhide_cursor()
else
hide_cursor()
end

draw.draw_quad(drawn_corners, target_position, cursor_is_vertical_bar())
if vim.api.nvim_get_mode().mode == "c" then vim.cmd.redraw() end
end

Expand Down Expand Up @@ -338,6 +357,7 @@ M.change_target_position = function(row, col)
set_corners(target_corners, row, col)
set_stiffnesses()

hide_cursor()
start_anination()
end

Expand Down
5 changes: 5 additions & 0 deletions lua/smear_cursor/color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,9 @@ setmetatable(M, {
end,
})

vim.api.nvim_set_hl(0, "SmearCursorHidden", {
fg = "white",
blend = 100,
})

return M
7 changes: 4 additions & 3 deletions lua/smear_cursor/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ M.smear_insert_mode = true
M.vertical_bar_cursor_insert_mode = true

-- Attempt to hide the real cursor by drawing a character below it.
M.hide_target_hack = true
-- Can be useful when not using `termguicolors`
M.hide_target_hack = false

-- Number of windows that stay open for rendering.
M.max_kept_windows = 50
Expand Down Expand Up @@ -82,8 +83,8 @@ M.slowdown_exponent = 0
M.distance_stop_animating = 0.1

-- Set of parameters for insert mode
M.stiffness_insert_mode = 0.4
M.trailing_stiffness_insert_mode = 0.4
M.stiffness_insert_mode = 0.3
M.trailing_stiffness_insert_mode = 0.3
M.trailing_exponent_insert_mode = 1
M.distance_stop_animating_vertical_bar = 0.875 -- Can be decreased (e.g. to 0.1) if using legacy computing symbols

Expand Down

0 comments on commit fbba581

Please sign in to comment.