added favorites functionality

This commit is contained in:
Maxim Stewart 2020-11-18 20:43:06 -06:00
parent fad7b6a31d
commit e30ae34f97
2 changed files with 51 additions and 14 deletions

View File

@ -1,6 +1,5 @@
# Python imports
import subprocess
import os
import os, subprocess, json
from os.path import isdir, isfile, join
from os import listdir
@ -25,6 +24,7 @@ class Main(Context):
paths = ["/opt/", "/usr/share/applications/", HOME_APPS]
baseOptions = ["[ TO MAIN MENU ]", "Favorites"]
self.menuData = self.getDesktopFilesInfo(paths)
self.faves = self.loadFaves()
query = ""
while True:
@ -35,8 +35,15 @@ class Main(Context):
if "Search..." in group:
query = self.call_method("searchMenu")["query"]
if "Favorites" in group:
query = self.call_method("favoritesMenu")["faves"]
if "[ Set Favorites ]" in group:
progsList = self.getSubgroup("Search...", "")
fixedProgsList = []
for prog in progsList:
fixedProgsList.append({'name': prog})
self.faves = self.call_method("setFavoritesMenu", [fixedProgsList])["setFaves"]
self.saveFaves(self.faves)
continue
if "[ Exit ]" in group:
break
@ -59,6 +66,35 @@ class Main(Context):
return method(data) if data else method()
def loadFaves(self, data = None):
configFolder = os.getenv("HOME") + "/.config/shellmen"
configFile = configFolder + "/favorites.json"
self.logger.info("[Opening saved favorites file: {}".format(configFile))
faves = []
if os.path.isdir(configFolder) == False:
os.mkdir(configFolder)
if os.path.isfile(configFile) == False:
open(configFile, 'a').close()
with open(configFile) as infile:
try:
faves = json.load(infile)
except Exception as e:
pass
infile.close()
return faves
def saveFaves(self, data = None):
configFolder = os.getenv("HOME") + "/.config/shellmen"
configFile = configFolder + "/favorites.json"
with open(configFile, 'w') as outfile:
json.dump(data, outfile)
def getDesktopFilesInfo(self, paths):
menuObjs = {
"Accessories": [],
@ -143,7 +179,7 @@ class Main(Context):
if query.lower() in opt["title"].lower() or query.lower() in opt["fileName"].lower():
desktopObjs.append( opt["title"] + " || " + opt["fileName"].replace(".desktop", "") )
elif "Favorites" in group:
pass
desktopObjs = self.faves
else:
for opt in self.menuData[group]:
keys = opt.keys()

View File

@ -17,7 +17,7 @@ from .mixins import StylesMixin
GROUPS = [ "Search...", "Favorites", "Accessories", "Multimedia", "Graphics", "Office",
"Development", "Internet", "Settings", "System", "Game", "Wine",
"Other", "[ Exit ]"
"Other", "[ Set Favorites ]", "[ Exit ]"
]
@ -53,15 +53,16 @@ class Context(StylesMixin):
return prompt(menu, style=self.theme)
def favoritesMenu(self, _grouplist = None):
GROUPS = ["[ TO MAIN MENU ]", "This is a stub method for Favorites..."]
grouplist = GROUPS if not _grouplist else _grouplist
def setFavoritesMenu(self, _grouplist = None):
GROUPS = [{'name': '[ TO MAIN MENU ]'}, {'name': 'This is a stub method for Favorites...'}]
grouplist = GROUPS if not _grouplist[0] else _grouplist[0]
menu = {
'type': 'list',
'name': 'faves',
'message': '[ Favorites ]',
'choices': grouplist
}
'type': 'checkbox',
'qmark': '>',
'message': 'Select Favorites',
'name': 'setFaves',
'choices': grouplist
}
return prompt(menu, style=self.theme)