Skip to content

Instantly share code, notes, and snippets.

@gacallea
Last active January 18, 2024 19:36
Show Gist options
  • Save gacallea/254cb01872a034222c6034826e4bdc61 to your computer and use it in GitHub Desktop.
Save gacallea/254cb01872a034222c6034826e4bdc61 to your computer and use it in GitHub Desktop.
chuck-nvim shred idea
[chuck]: (VM) sporking incoming shred: 1 (test1.ck)...
[chuck]: (VM) sporking incoming shred: 2 (test2.ck)...
[chuck]: (VM) replacing shred 1 (test1.ck) with 1 (chant1.ck)...
mi0 97.998859
mi0 97.998859
mi0 97.998859
ut1 77.781746
ra1 82.406889
ut1 77.781746
ra1 82.406889
ra1 82.406889
ut1 77.781746
ra1 82.406889
ra1 82.406889
ra1 82.406889
[chuck]: (VM) replacing shred: no shred with id 3...
[chuck]: (VM) cannot remove: no shred with id 8...
ut1 77.781746
ra1 82.406889
ra1 82.406889
ra1 82.406889
ut1 77.781746
ra1 82.406889
mi0 97.998859
mi0 97.998859
ra1 82.406889
mi0 97.998859
ra0 82.406889
ut0 77.781746
[chuck]: (VM) removing shred: 1 (chant1.ck)...
[chuck]: (VM) sporking incoming shred: 3 (test3.ck)...
[chuck]: (VM) sporking incoming shred: 4 (test4.ck)...
[chuck]: (VM) sporking incoming shred: 5 (test5.ck)...
[chuck]: (VM) sporking incoming shred: 2 (test2.ck)...
[chuck]: (VM) replacing shred 2 (test2.ck) with 2 (chant2.ck)...
###### remove this line to see the effect of the above lines ######
[chuck]: (VM) removing all (4) shreds...
###################################################################
[chuck]: (VM) EXIT received....
-- shreds table to build the Shred UI with NuiTable
local shreds_table = {}
-- split line into columns and return each word as line
local function column(line, n)
local p = string.rep("%s+.-", n - 1) .. "(%S+)"
return line:match(p)
end
-- if the log line matches a certain word/pattern we set an action
local function set_action(line)
local action
if string.match(line, "sporking incoming shred: %d") then
action = "add"
end
if string.match(line, "replacing shred %d") then
action = "replace"
end
if string.match(line, "removing shred: %d") then
action = "remove"
end
-- if clearing or exiting, empty the table and set action to nil
if string.match(line, "removing all shreds") then
for k in pairs(shreds_table) do
shreds_table[k] = nil
end
action = nil
end
return action
end
-- set the shreds table from raw ChucK data ["id"] = "value"
local function set_shreds_tbl(line)
local action = set_action(line)
-- if the log line matches a certain word/pattern
if action ~= nil then
local shred_id
local shred_name
-- get string length
local _, count = string.gsub(line, "% ", "")
-- iterate over the string
for n = 1, count + 1 do
-- split line to columns and match the last id and last filename
local id = column(line, n):match("%d+")
local name = column(line, n):match("%((.+)%).+")
-- if id is valid initialize shred_id
if id ~= nil then
shred_id = tostring(id)
end
-- if name is valid initialize shred_name
if name ~= nil then
shred_name = name
end
-- if shred_id and shred_name are valid
if shred_id ~= nil and shred_name ~= nil and shred_name:match(".ck") then
-- if adding/replacing set shred as: id is key -> shred_name is value
if action == "add" or "replace" then
shreds_table[shred_id] = shred_name
end
-- if removing set shred as nil: id is key -> nil
if action == "remove" then
shreds_table[shred_id] = nil
end
end
end
end
end
local function peaceout()
for k, v in pairs(shreds_table) do
print(string.format("%d %s", k, v))
end
end
local function shred_lines(logfile)
local cmd = string.format("tail -f %s", logfile)
local pipe = assert(io.popen(cmd))
repeat
local line = pipe:read(1)
if line then
set_shreds_tbl(line)
io.write(line)
io.flush()
end
until not line
pipe:close()
peaceout()
end
shred_lines("chuck.log")
@gacallea
Copy link
Author

notes from discord:

Andrea C From The App β€” Today at 6:15 PM I guess that the first thing I'd ask
help about is: passing a log file to the script as argument "to an exposed
function" and parsing the lines instead of hard coded variables. That would be
progress πŸ™‚ ps: right now I'm mocking everything in a script. I hope that it
will be nvim-compatible :S

Cloud β€” Today at 6:22 PM If you want to pass a file, you have a couple options
Pass a /path/to/the/file.ext as an argument, and use io (nvim might have extra
filesystem interaction libraries that i don't know of) to open and use it

Open the file directly, and pass the handle around (might get messy if multiple
things try to touch it?)

Or open it, read it, and use an internal buffer passed around to "write" before
actually committing it to the disk 

Andrea C From The App β€” Today at 6:24 PM Hi @Cloud  πŸ™‚ the latter is not
feasible for me. At some point the script will have to be in its own split
parsing the log live to view the list of shreds. adding, replacing, removing
should be live and the table and UI updated accordingly. regarding multiple
things touching it: I'm not sure how to do this but: the log should be parsed
on new lines only to avoid dupes. each new line should trigger the action to
add/remove/replace based on matching. I don't even know this is possibe. Life
would be much easier if ChucK VM would just dump do console somehere besides
its own VM.

Cloud β€” Today at 6:28 PM So this should be pretty easy, the only major issue is
checking when a file has been written to, otherwise you'll just be on a
time-based loop for reading from the file realistically You can read a file
continuously in lua, and when it gets appended to, your next file:read("*a")
will read from your last point in the file (position 0 if no reads/seeks have
been performed) to the now last position in the file

Andrea C From The App β€” Today at 6:30 PM yay πŸ™‚ Cloud β€” Today at 6:31 PM Then
at that point you could just use a split function (custom logic, nvim might
have one builtin) to split on newlines. I don't know how file:lines() performs
when used from the middle of a file, for example, but it might work for exactly
what you need (and someone else might be able to clarify) for splitting on \n
(or carriage returns as well, depending) 

Andrea C From The App β€” Today at 6:32 PM thanks @Cloud I appreciate your input.
I will need to process it once my brain is back πŸ˜› 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment