Allow multiple options to be passed in one arg, clean code.
This commit is contained in:
parent
341c4d91f0
commit
aee9be799b
@ -2,16 +2,19 @@ import os
|
||||
|
||||
fn main() {
|
||||
mut args := []bool{len: 3}
|
||||
mut complete := false
|
||||
for mut arg in args {
|
||||
arg = false
|
||||
}
|
||||
for arg in os.args {
|
||||
/*
|
||||
match arg {
|
||||
|
||||
"-i", "--icons" { args[0] = true }
|
||||
"-t", "--tooltip" { args[1] = true }
|
||||
"-a", "--all" { args[2] = true }
|
||||
"-h", "--help" {
|
||||
println("\nUsage:\n ${os.args[0]} [-h] [-t] [-i] [-a]\n\nGenerates Applications menu for Joe's Window Manager.\n\nUsage in .jwmrc:\n<Dynamic label=\"Applications\" icon=\"view-list\">\n\texec: ${os.args[0]} \n</Dynamic>\nOptions:\n -h, --help\tShow this text.\n -i, --icons\tAlso generate icons.\n -t, --tooltip\tAlso generate tooltips.\n -a, --all\tDo not generate categories: list all applications.\n\nv1.0")
|
||||
println("\nUsage:\n ${os.args[0]} [-h] [-t] [-i] [-a]\n\nGenerates Applications menu for Joe's Window Manager.\n\nUsage in .jwmrc:\n<Dynamic label=\"Applications\" icon=\"view-list\">\n\texec: ${os.args[0]} \n</Dynamic>\nOptions:\n -h, --help\tShow this text.\n -i, --icons\tAlso generate icons.\n -t, --tooltip\tAlso generate tooltips.\n -a, --all\tDo not generate categories: list all applications.\n\nv1.1")
|
||||
exit(0)
|
||||
} else {
|
||||
if arg == os.args[0] {} else {
|
||||
@ -19,8 +22,27 @@ fn main() {
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}*/
|
||||
if !(arg == os.args[0]) {
|
||||
if arg.contains('h') && arg.contains('-') {
|
||||
println("\nUsage:\n ${os.args[0]} [-h] [-tia]\n\nGenerates Applications menu for Joe's Window Manager.\n\nUsage in .jwmrc:\n<Dynamic label=\"Applications\" icon=\"view-list\">\n\texec: ${os.args[0]} \n</Dynamic>\nOptions:\n -h, --help\tShow this text.\n -i, --icons\tAlso generate icons.\n -t, --tooltip\tAlso generate tooltips.\n -a, --all\tDo not generate categories: list all applications.\n\nv1.1")
|
||||
exit(0)
|
||||
}
|
||||
complete = false
|
||||
if arg.contains('i') && arg.contains('-') { args[0] = true; complete = true }
|
||||
if arg.contains('t') && arg.contains('-') { args[1] = true; complete = true }
|
||||
if arg.contains('a') && arg.contains('-') { args[2] = true; complete = true }
|
||||
if !complete {
|
||||
println("Unknown argument: ${arg}")
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if !args[2] {
|
||||
categorymode(args)
|
||||
} else {
|
||||
@ -41,6 +63,17 @@ fn allmode(args []bool){
|
||||
println('</JWM>')
|
||||
}
|
||||
|
||||
fn buildcategory(args []bool, folder string, item string, catname string, mut linkslist []string) {
|
||||
println('<Menu label="${item}">')
|
||||
for mut elem in linkslist {
|
||||
if !(elem.contains('/IM/')) && checktargetcategory(elem, item) {
|
||||
println(buildprogram('${folder}${elem}', args))
|
||||
elem = '/IM/'
|
||||
}
|
||||
}
|
||||
println('</Menu>')
|
||||
}
|
||||
|
||||
fn categorymode(args []bool) {
|
||||
mut linkslist := os.ls('/usr/share/applications/') or { panic(err) }
|
||||
println('<?xml version="1.0"?>\n<JWM>\n<Menu label="Accessories">')
|
||||
@ -49,54 +82,15 @@ fn categorymode(args []bool) {
|
||||
if !(item.contains('.desktop')) || item.contains('krita_') { item = '/IM/' }
|
||||
}
|
||||
|
||||
for mut item in linkslist {
|
||||
if !(item.contains('/IM/')) && checktargetcategory(item, "Utility") {
|
||||
println(buildprogram('/usr/share/applications/${item}', args))
|
||||
item = '/IM/'
|
||||
}
|
||||
}
|
||||
println('</Menu>\n<Menu label="Office">')
|
||||
for mut item in linkslist {
|
||||
if !(item.contains('/IM/')) && checktargetcategory(item, "Office") {
|
||||
println(buildprogram('/usr/share/applications/${item}', args))
|
||||
item = '/IM/'
|
||||
}
|
||||
}
|
||||
println('</Menu>\n<Menu label="Games">')
|
||||
for mut item in linkslist {
|
||||
if !(item.contains('/IM/')) && checktargetcategory(item, "Game") {
|
||||
println(buildprogram('/usr/share/applications/${item}', args))
|
||||
item = '/IM/'
|
||||
}
|
||||
}
|
||||
println('</Menu>\n<Menu label="Graphics">')
|
||||
for mut item in linkslist {
|
||||
if !(item.contains('/IM/')) && checktargetcategory(item, "Graphics") {
|
||||
println(buildprogram('/usr/share/applications/${item}', args))
|
||||
item = '/IM/'
|
||||
}
|
||||
}
|
||||
println('</Menu>\n<Menu label="Internet">')
|
||||
for mut item in linkslist {
|
||||
if !(item.contains('/IM/')) && checktargetcategory(item, "Network") {
|
||||
println(buildprogram('/usr/share/applications/${item}', args))
|
||||
item = '/IM/'
|
||||
}
|
||||
}
|
||||
println('</Menu>\n<Menu label="System Tools">')
|
||||
for mut item in linkslist {
|
||||
if !(item.contains('/IM/')) && checktargetcategory(item, "System") {
|
||||
println(buildprogram('/usr/share/applications/${item}', args))
|
||||
item = '/IM/'
|
||||
}
|
||||
}
|
||||
println('</Menu>\n<Menu label="Other">')
|
||||
for mut item in linkslist {
|
||||
if !(item.contains('/IM/')) {
|
||||
println(buildprogram('/usr/share/applications/${item}', args))
|
||||
}
|
||||
}
|
||||
println('</Menu>\n</JWM>')
|
||||
buildcategory(args, '/usr/share/applications/', "Utility", "Accessories", mut linkslist)
|
||||
buildcategory(args, '/usr/share/applications/', "Office", "Office", mut linkslist)
|
||||
buildcategory(args, '/usr/share/applications/', "Game", "Games", mut linkslist)
|
||||
buildcategory(args, '/usr/share/applications/', "Graphics", "Graphics", mut linkslist)
|
||||
buildcategory(args, '/usr/share/applications/', "Network", "Internet", mut linkslist)
|
||||
buildcategory(args, '/usr/share/applications/', "System", "System Tools", mut linkslist)
|
||||
buildcategory(args, '/usr/share/applications/', "Other", "NaN", mut linkslist)
|
||||
|
||||
println('</JWM>')
|
||||
}
|
||||
|
||||
fn buildprogram(filename string, args []bool) string {
|
||||
@ -104,7 +98,9 @@ fn buildprogram(filename string, args []bool) string {
|
||||
file := os.read_file(filename) or {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
content := file.split_into_lines()
|
||||
|
||||
return '<Program ${optbuildicon(content, args[0])} label="${strfinder(content, 1)}" ${optbuildtooltip(content, args[1])}>${strfinder(content, 2)}</Program>'
|
||||
}
|
||||
|
||||
@ -153,6 +149,9 @@ fn filter(strorig string, mode int) string {
|
||||
|
||||
fn checktargetcategory(filename string, check string) bool {
|
||||
//println("checking")
|
||||
if filename == "NaN" {
|
||||
return true
|
||||
}
|
||||
for line in (os.read_file("/usr/share/applications/${filename}") or { panic(err) }).split_into_lines() {
|
||||
if line.contains(check) && line.contains("Categories=") { return true }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user