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() {
|
fn main() {
|
||||||
mut args := []bool{len: 3}
|
mut args := []bool{len: 3}
|
||||||
|
mut complete := false
|
||||||
for mut arg in args {
|
for mut arg in args {
|
||||||
arg = false
|
arg = false
|
||||||
}
|
}
|
||||||
for arg in os.args {
|
for arg in os.args {
|
||||||
|
/*
|
||||||
match arg {
|
match arg {
|
||||||
|
|
||||||
"-i", "--icons" { args[0] = true }
|
"-i", "--icons" { args[0] = true }
|
||||||
"-t", "--tooltip" { args[1] = true }
|
"-t", "--tooltip" { args[1] = true }
|
||||||
"-a", "--all" { args[2] = true }
|
"-a", "--all" { args[2] = true }
|
||||||
"-h", "--help" {
|
"-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)
|
exit(0)
|
||||||
} else {
|
} else {
|
||||||
if arg == os.args[0] {} else {
|
if arg == os.args[0] {} else {
|
||||||
@ -19,8 +22,27 @@ fn main() {
|
|||||||
exit(1)
|
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] {
|
if !args[2] {
|
||||||
categorymode(args)
|
categorymode(args)
|
||||||
} else {
|
} else {
|
||||||
@ -41,6 +63,17 @@ fn allmode(args []bool){
|
|||||||
println('</JWM>')
|
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) {
|
fn categorymode(args []bool) {
|
||||||
mut linkslist := os.ls('/usr/share/applications/') or { panic(err) }
|
mut linkslist := os.ls('/usr/share/applications/') or { panic(err) }
|
||||||
println('<?xml version="1.0"?>\n<JWM>\n<Menu label="Accessories">')
|
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/' }
|
if !(item.contains('.desktop')) || item.contains('krita_') { item = '/IM/' }
|
||||||
}
|
}
|
||||||
|
|
||||||
for mut item in linkslist {
|
buildcategory(args, '/usr/share/applications/', "Utility", "Accessories", mut linkslist)
|
||||||
if !(item.contains('/IM/')) && checktargetcategory(item, "Utility") {
|
buildcategory(args, '/usr/share/applications/', "Office", "Office", mut linkslist)
|
||||||
println(buildprogram('/usr/share/applications/${item}', args))
|
buildcategory(args, '/usr/share/applications/', "Game", "Games", mut linkslist)
|
||||||
item = '/IM/'
|
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)
|
||||||
println('</Menu>\n<Menu label="Office">')
|
buildcategory(args, '/usr/share/applications/', "Other", "NaN", mut linkslist)
|
||||||
for mut item in linkslist {
|
|
||||||
if !(item.contains('/IM/')) && checktargetcategory(item, "Office") {
|
println('</JWM>')
|
||||||
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>')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn buildprogram(filename string, args []bool) string {
|
fn buildprogram(filename string, args []bool) string {
|
||||||
@ -104,7 +98,9 @@ fn buildprogram(filename string, args []bool) string {
|
|||||||
file := os.read_file(filename) or {
|
file := os.read_file(filename) or {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
content := file.split_into_lines()
|
content := file.split_into_lines()
|
||||||
|
|
||||||
return '<Program ${optbuildicon(content, args[0])} label="${strfinder(content, 1)}" ${optbuildtooltip(content, args[1])}>${strfinder(content, 2)}</Program>'
|
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 {
|
fn checktargetcategory(filename string, check string) bool {
|
||||||
//println("checking")
|
//println("checking")
|
||||||
|
if filename == "NaN" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
for line in (os.read_file("/usr/share/applications/${filename}") or { panic(err) }).split_into_lines() {
|
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 }
|
if line.contains(check) && line.contains("Categories=") { return true }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user