mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-17 11:15:12 -04:00
Add Find (CTRL-F) to edit.
This commit is contained in:
parent
fa8b9600cc
commit
4f01087652
@ -7,6 +7,8 @@ local term = require("term")
|
|||||||
local text = require("text")
|
local text = require("text")
|
||||||
local unicode = require("unicode")
|
local unicode = require("unicode")
|
||||||
|
|
||||||
|
local helpStatusText = "Save: [Ctrl+S] Close: [Ctrl+W] Find: [Ctrl+F]"
|
||||||
|
|
||||||
if not term.isAvailable() then
|
if not term.isAvailable() then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -118,6 +120,35 @@ local function setCursor(nbx, nby)
|
|||||||
component.gpu.set(w - 9, h + 1, text.padLeft(string.format("%d,%d", nby, nbx), 10))
|
component.gpu.set(w - 9, h + 1, text.padLeft(string.format("%d,%d", nby, nbx), 10))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function highlight(x,y,n,yes)
|
||||||
|
local w, h = getSize()
|
||||||
|
|
||||||
|
local cy = y - scrollY
|
||||||
|
if cy > h or cy < 1 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local cx = x - scrollX
|
||||||
|
if cx > w or cx < 1 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local fg = component.gpu.getForeground()
|
||||||
|
local bg = component.gpu.getBackground()
|
||||||
|
if yes then
|
||||||
|
component.gpu.setForeground(bg)
|
||||||
|
component.gpu.setBackground(fg)
|
||||||
|
end
|
||||||
|
for c = cx,cx+n do
|
||||||
|
local ch,_ = component.gpu.get(c,cy)
|
||||||
|
component.gpu.set(c,cy,ch)
|
||||||
|
end
|
||||||
|
if yes then
|
||||||
|
component.gpu.setForeground(fg)
|
||||||
|
component.gpu.setBackground(bg)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function home()
|
local function home()
|
||||||
local cbx, cby = getCursor()
|
local cbx, cby = getCursor()
|
||||||
setCursor(1, cby)
|
setCursor(1, cby)
|
||||||
@ -205,7 +236,7 @@ local function delete()
|
|||||||
component.gpu.copy(1, cy + 2, w, h - (cy + 1), 0, -1)
|
component.gpu.copy(1, cy + 2, w, h - (cy + 1), 0, -1)
|
||||||
component.gpu.set(1, h, text.padRight(buffer[cby + (h - cy)], w))
|
component.gpu.set(1, h, text.padRight(buffer[cby + (h - cy)], w))
|
||||||
end
|
end
|
||||||
setStatus("Save: [Ctrl+S] Close: [Ctrl+W]")
|
setStatus(helpStatusText)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -227,7 +258,7 @@ local function insert(value)
|
|||||||
end
|
end
|
||||||
component.gpu.set(cx, cy, value)
|
component.gpu.set(cx, cy, value)
|
||||||
right(len)
|
right(len)
|
||||||
setStatus("Save: [Ctrl+S] Close: [Ctrl+W]")
|
setStatus(helpStatusText)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function enter()
|
local function enter()
|
||||||
@ -245,11 +276,70 @@ local function enter()
|
|||||||
component.gpu.set(1, cy + 1, text.padRight(buffer[cby + 1], w))
|
component.gpu.set(1, cy + 1, text.padRight(buffer[cby + 1], w))
|
||||||
end
|
end
|
||||||
setCursor(1, cby + 1)
|
setCursor(1, cby + 1)
|
||||||
setStatus("Save: [Ctrl+S] Close: [Ctrl+W]")
|
setStatus(helpStatusText)
|
||||||
|
end
|
||||||
|
|
||||||
|
local findText = ""
|
||||||
|
local searchHistory = {}
|
||||||
|
local function refind()
|
||||||
|
local w, h = getSize()
|
||||||
|
local cx, cy = term.getCursor()
|
||||||
|
local cbx, cby = getCursor()
|
||||||
|
local ibx, iby = cbx, cby
|
||||||
|
local phl = -1
|
||||||
|
repeat
|
||||||
|
term.setCursor(7+unicode.len(findText), h+1)
|
||||||
|
setStatus("Find: "..findText)
|
||||||
|
local _,_,char,code = event.pull("key_up")
|
||||||
|
if code == keyboard.keys.enter then
|
||||||
|
break
|
||||||
|
elseif code == keyboard.keys.back then
|
||||||
|
local nlen = unicode.len(findText)-1
|
||||||
|
if nlen < 0 then
|
||||||
|
break
|
||||||
|
else
|
||||||
|
findText = unicode.sub(findText,1,nlen)
|
||||||
|
end
|
||||||
|
elseif keyboard.isControlDown() then
|
||||||
|
if code == keyboard.keys.f or code == keyboard.keys.g then
|
||||||
|
-- find next
|
||||||
|
ibx = cbx + 1
|
||||||
|
iby = cby
|
||||||
|
end
|
||||||
|
elseif keyboard.isControl(char) then
|
||||||
|
-- ignore
|
||||||
|
else
|
||||||
|
findText = findText..unicode.char(char)
|
||||||
|
end
|
||||||
|
if unicode.len(findText) > 0 then
|
||||||
|
for syo = 1,#buffer do
|
||||||
|
local sy = (iby + syo - 1 + #buffer - 1) % #buffer + 1
|
||||||
|
local line = buffer[sy]
|
||||||
|
local found,e = string.find(line,findText,syo == 1 and ibx or 1)
|
||||||
|
if found and (found >= ibx or syo > 1) then
|
||||||
|
highlight(cbx,cby,phl)
|
||||||
|
cbx = found
|
||||||
|
cby = sy
|
||||||
|
setCursor(cbx,cby)
|
||||||
|
phl = e-found
|
||||||
|
highlight(cbx,cby,phl,1)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
until false
|
||||||
|
highlight(cbx,cby,phl)
|
||||||
|
setStatus(helpStatusText)
|
||||||
|
setCursor(cbx,cby)
|
||||||
|
end
|
||||||
|
local function find()
|
||||||
|
findText = ""
|
||||||
|
refind()
|
||||||
end
|
end
|
||||||
|
|
||||||
local controlKeyCombos = {[keyboard.keys.s]=true,[keyboard.keys.w]=true,
|
local controlKeyCombos = {[keyboard.keys.s]=true,[keyboard.keys.w]=true,
|
||||||
[keyboard.keys.c]=true,[keyboard.keys.x]=true}
|
[keyboard.keys.c]=true,[keyboard.keys.x]=true,
|
||||||
|
[keyboard.keys.f]=true,[keyboard.keys.g]=true}
|
||||||
local function onKeyDown(char, code)
|
local function onKeyDown(char, code)
|
||||||
if code == keyboard.keys.back and not readonly then
|
if code == keyboard.keys.back and not readonly then
|
||||||
if left() then
|
if left() then
|
||||||
@ -303,6 +393,11 @@ local function onKeyDown(char, code)
|
|||||||
else
|
else
|
||||||
setStatus(reason)
|
setStatus(reason)
|
||||||
end
|
end
|
||||||
|
elseif code == keyboard.keys.f then
|
||||||
|
event.pull("key_up") -- Ctrl-F up
|
||||||
|
find()
|
||||||
|
elseif code == keyboard.keys.g then
|
||||||
|
refind() -- will immediate CTRL-G
|
||||||
elseif code == keyboard.keys.w or
|
elseif code == keyboard.keys.w or
|
||||||
code == keyboard.keys.c or
|
code == keyboard.keys.c or
|
||||||
code == keyboard.keys.x
|
code == keyboard.keys.x
|
||||||
|
Loading…
x
Reference in New Issue
Block a user