Skip to content

Commit

Permalink
Add -cats for category filtering #34
Browse files Browse the repository at this point in the history
  • Loading branch information
nwg-piotr committed Nov 29, 2021
1 parent 94c94b8 commit ee0ac23
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
Binary file modified bin/nwg-drawer
Binary file not shown.
45 changes: 33 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
id2entry map[string]desktopEntry
preferredApps map[string]interface{}
exclusions []string
catsFilter []string
)

var categoryNames = [...]string{
Expand Down Expand Up @@ -129,9 +130,10 @@ var fileManager = flag.String("fm", "thunar", "File Manager")
var term = flag.String("term", defaultStringIfBlank(os.Getenv("TERM"), "alacritty"), "Terminal emulator")
var nameLimit = flag.Int("fslen", 80, "File Search name LENgth Limit")
var noCats = flag.Bool("nocats", false, "Disable filtering by category")
var noFS = flag.Bool("nofs", false, "Disable file search")
var resident = flag.Bool("r", false, "Leave the program resident in memory")
var debug = flag.Bool("d", false, "Turn on Debug messages")
var cats = flag.String("cats", "", "case sensitive, comma-separated list of CATegorieS to show, e.g. Utility,TerminalEmulator,System")
var noFS = flag.Bool("nofs", false, "disable file search")
var resident = flag.Bool("r", false, "leave the program Resident in memory")
var debug = flag.Bool("d", false, "turn on Debug messages")

func main() {
timeStart := time.Now()
Expand Down Expand Up @@ -260,6 +262,19 @@ func main() {

appDirs = getAppDirs()

// if categories filter given w/ -cats argument
if *cats != "" {
*noCats = true

cf := strings.Split(*cats, ",")
for _, c := range cf {
if c != "" {
catsFilter = append(catsFilter, c)
}
}
log.Info(fmt.Sprintf("Using category filter: %s", catsFilter))
}

setUpCategories()

desktopFiles := listDesktopFiles()
Expand Down Expand Up @@ -411,7 +426,11 @@ func main() {

pinnedFlowBoxWrapper, _ = gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
outerVBox.PackStart(pinnedFlowBoxWrapper, false, false, 0)
pinnedFlowBox = setUpPinnedFlowBox()

// Avoid missing items if category filter enabled with -cats argument
if *cats == "" {
pinnedFlowBox = setUpPinnedFlowBox()
}

resultWindow, _ = gtk.ScrolledWindowNew(nil, nil)
resultWindow.SetEvents(int(gdk.ALL_EVENTS_MASK))
Expand Down Expand Up @@ -439,14 +458,16 @@ func main() {
appFlowBox = setUpAppsFlowBox(nil, "")

// Focus 1st pinned item if any, otherwise focus 1st found app icon
var button gtk.IWidget
if pinnedFlowBox.GetChildren().Length() > 0 {
button, err = pinnedFlowBox.GetChildAtIndex(0).GetChild()
} else {
button, err = appFlowBox.GetChildAtIndex(0).GetChild()
}
if err == nil {
button.ToWidget().GrabFocus()
if pinnedFlowBox != nil {
var button gtk.IWidget
if pinnedFlowBox.GetChildren().Length() > 0 {
button, err = pinnedFlowBox.GetChildAtIndex(0).GetChild()
} else {
button, err = appFlowBox.GetChildAtIndex(0).GetChild()
}
if err == nil {
button.ToWidget().GrabFocus()
}
}

userDirsMap = mapXdgUserDirs()
Expand Down
24 changes: 20 additions & 4 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,30 @@ func parseDesktopFiles(desktopFiles []string) string {
// Fixes bug introduced in #19
}

id2entry[entry.DesktopID] = entry
desktopEntries = append(desktopEntries, entry)
assignToLists(entry.DesktopID, entry.Category)
// Add category filter requested in #34
inCategoryFilter := true

if *cats != "" && len(catsFilter) > 0 {
inCategoryFilter = false
entryCategories := strings.Split(entry.Category, ";")
for _, ec := range entryCategories {
if isIn(catsFilter, ec) {
inCategoryFilter = true
break
}
}
}

if inCategoryFilter {
id2entry[entry.DesktopID] = entry
desktopEntries = append(desktopEntries, entry)
assignToLists(entry.DesktopID, entry.Category)
}
}
sort.Slice(desktopEntries, func(i, j int) bool {
return desktopEntries[i].NameLoc < desktopEntries[j].NameLoc
})
summary := fmt.Sprintf("%v entries (+%v hidden)", len(desktopEntries)-hidden, hidden)
summary := fmt.Sprintf("%v entries (%v hidden)", len(desktopEntries), hidden)
log.Infof("Skipped %v duplicates; %v .desktop entries hidden by \"NoDisplay=true\"", skipped, hidden)
return summary
}
Expand Down

0 comments on commit ee0ac23

Please sign in to comment.