-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,70 @@ | ||
local sc_debug = require('smoothcursor.debug') | ||
|
||
return { | ||
buffer = (function() | ||
local obj = { | ||
buffer = {}, | ||
length = 1, | ||
bufnr = 1, -- chach bufnr | ||
push_front = function(self, data) | ||
table.insert(self, 1, data) | ||
table.remove(self, self.length + 1) | ||
end, | ||
--- Resize the buffer size | ||
--- @param size number | ||
resize_buffer = function(self, size) | ||
if self.length == size then | ||
return | ||
end | ||
local diff = size - self.length | ||
if diff < 0 then | ||
for _ = 1, -diff, 1 do | ||
table.remove(self, self.length + 1) | ||
end | ||
else | ||
for _ = 1, diff, 1 do | ||
table.insert(self, 1, 0) | ||
end | ||
end | ||
self.length = size | ||
end, | ||
switch_buf = function(self) | ||
self.bufnr = vim.fn.bufnr() | ||
sc_debug.buf_switch_counter = sc_debug.buf_switch_counter + 1 | ||
end, | ||
---@param value number | ||
all = function(self, value) | ||
for i = 1, self.length, 1 do | ||
self[i] = value | ||
end | ||
end, | ||
--- check if | ||
is_stay_still = function(self) | ||
local first_val = self[1] | ||
for i = 2, self.length do | ||
if first_val ~= self[i] then | ||
return false | ||
end | ||
local function create_buffer() | ||
return { | ||
buffer = {}, | ||
length = 1, | ||
bufnr = 1, -- cache bufnr | ||
|
||
push_front = function(self, data) | ||
table.insert(self, 1, data) | ||
table.remove(self, self.length + 1) | ||
end, | ||
|
||
resize_buffer = function(self, size) | ||
local diff = size - self.length | ||
if diff < 0 then | ||
for _ = 1, -diff, 1 do | ||
table.remove(self, self.length + 1) | ||
end | ||
return true | ||
end, | ||
} | ||
return setmetatable(obj, { | ||
__index = function(t, k) | ||
if t.buffer[t.bufnr] == nil then | ||
t.buffer[t.bufnr] = {} | ||
else | ||
for _ = 1, diff, 1 do | ||
table.insert(self, 1, 0) | ||
end | ||
return t.buffer[t.bufnr][k] | ||
end, | ||
__newindex = function(t, k, v) | ||
if t.buffer[t.bufnr] == nil then | ||
t.buffer[t.bufnr] = {} | ||
end | ||
self.length = size | ||
end, | ||
|
||
switch_buf = function(self) | ||
self.bufnr = vim.fn.bufnr() | ||
sc_debug.buf_switch_counter = sc_debug.buf_switch_counter + 1 | ||
end, | ||
|
||
all = function(self, value) | ||
for i = 1, self.length, 1 do | ||
self[i] = value | ||
end | ||
end, | ||
|
||
is_stay_still = function(self) | ||
local first_val = self[1] | ||
for i = 2, self.length do | ||
if first_val ~= self[i] then | ||
return false | ||
end | ||
t.buffer[t.bufnr][k] = v | ||
end, | ||
}) | ||
end)(), | ||
end | ||
return true | ||
end, | ||
} | ||
end | ||
|
||
local function buffer_metatable() | ||
return { | ||
__index = function(t, k) | ||
if t.buffer[t.bufnr] == nil then | ||
t.buffer[t.bufnr] = {} | ||
end | ||
return t.buffer[t.bufnr][k] | ||
end, | ||
__newindex = function(t, k, v) | ||
if t.buffer[t.bufnr] == nil then | ||
t.buffer[t.bufnr] = {} | ||
end | ||
t.buffer[t.bufnr][k] = v | ||
end, | ||
} | ||
end | ||
|
||
return { | ||
buffer = setmetatable(create_buffer(), buffer_metatable()), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters