Skip to content

Commit

Permalink
First pass at adding 'favorites'. (#1207)
Browse files Browse the repository at this point in the history
* First pass at adding 'favorites'.
* Add the favorites to the beginning of the menu list.
* Use tabutil instead of janky shell outs to bash.
* Don't use the "selected script" var.
* Prevent "favoriting" the "-" separator.
* Add favorites to paths table.
* Include removing paths.favorites when calling reset.

Co-authored-by: rayne <rayne>
  • Loading branch information
zzsnzmn authored Oct 20, 2020
1 parent 915c86c commit da9565c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
1 change: 1 addition & 0 deletions lua/core/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ _path.code = _path.dust..'code/'
_path.audio = _path.dust..'audio/'
_path.tape = _path.audio..'tape/'
_path.data = _path.dust..'data/'
_path.favorites = _path.data..'system.favorites'

1 change: 1 addition & 0 deletions lua/core/menu/reset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ m.key = function(n,z)
_menu.redraw()
os.execute("rm ~/dust/data/system.pset")
os.execute("rm ~/dust/data/system.state")
os.execute("rm "..paths.favorites)
_norns.reset()
end
end
Expand Down
83 changes: 71 additions & 12 deletions lua/core/menu/select.lua
Original file line number Diff line number Diff line change
@@ -1,37 +1,64 @@
tabutil = require "tabutil"

local m = {
pos = 0,
list = {},
len = "scan"
favorites = {},
len = "scan"
}

local function menu_table_entry(file)
local p = string.match(file,".*/")
local n = string.gsub(file,'.lua','/')
n = string.gsub(n,paths.code,'')
n = string.sub(n,0,-2)
local a,b = string.match(n,"(.+)/(.+)$") -- strip similar dir/script
if a==b and a then n = a end
return {name=n,file=file,path=p}
end

local function sort_select_tree(results)
if tab.count(m.favorites) > 0 then
for _, entry in pairs(m.favorites) do
table.insert(m.list,entry)
end
table.insert(m.list, {name="-", file=nil, path=nil})
end

local t = {}
for filename in results:gmatch("[^\r\n]+") do
if string.match(filename,"/data/")==nil and
if string.match(filename,"/data/")==nil and
string.match(filename,"/lib/")==nil then
table.insert(t,filename)
end
end

for _,file in pairs(t) do
local p = string.match(file,".*/")
local n = string.gsub(file,'.lua','/')
n = string.gsub(n,_path.code,'')
n = string.sub(n,0,-2)
local a,b = string.match(n,"(.+)/(.+)$") -- strip similar dir/script
if a==b and a then n = a end
--print(file,n,p)
table.insert(m.list,{name=n,file=file,path=p})
table.insert(m.list,menu_table_entry(file))
end

m.len = tab.count(m.list)
_menu.redraw()
end

local function contains(list, menu_item)
for _, v in pairs(list) do
if v.file == menu_item.file then
return true
end
end
return false
end

m.init = function()
m.len = "scan"
m.len = "scan"
m.list = {}
m.favorites = {}
m.favorites = tabutil.load(paths.favorites)
if m.favorites == nil then
m.favorites = {}
tabutil.save(m.favorites, paths.favorites)
end
-- weird command, but it is fast, recursive, skips hidden dirs, and sorts
norns.system_cmd('find ~/dust/code/ -name "*.lua" | sort', sort_select_tree)
end
Expand All @@ -44,6 +71,8 @@ m.key = function(n,z)
_menu.set_page("HOME")
-- select
elseif n==3 and z==1 then
-- return if the current "file" is the split between favorites and all scripts
if m.list[m.pos+1].file == nil then return end
_menu.previewfile = m.list[m.pos+1].file
_menu.set_page("PREVIEW")
end
Expand All @@ -53,6 +82,13 @@ m.enc = function(n,delta)
if n==2 then
m.pos = util.clamp(m.pos + delta, 0, m.len - 1)
_menu.redraw()
elseif n==3 then
if delta > 0 then
m.add_favorite()
else
m.remove_favorite()
end
_menu.redraw()
end
end

Expand All @@ -75,11 +111,34 @@ m.redraw = function()
else
screen.level(4)
end
screen.text(string.upper(line))
local is_fave = " "
if contains(m.favorites, m.list[i+m.pos-2]) then is_fave = "* " else is_fave = " " end
screen.text(is_fave .. string.upper(line))
end
end
end
screen.update()
end

m.add_favorite = function()
-- don't add the '-' split as a favorite.
if m.list[m.pos+1].name == '-' then
return
end
if not contains(m.favorites, m.list[m.pos+1]) then
table.insert(m.favorites, m.list[m.pos+1])
tabutil.save(m.favorites, paths.favorites)
end
end

m.remove_favorite = function()
for i, v in pairs(m.favorites) do
if v.file == m.list[m.pos+1].file then
table.remove(m.favorites, i)
tabutil.save(m.favorites, paths.favorites)
return
end
end
end

return m

0 comments on commit da9565c

Please sign in to comment.