KeizaDesktop/jwmappbuilder.v

175 lines
5.9 KiB
Coq
Raw Normal View History

2023-12-30 23:37:04 +04:00
import os
fn main() {
mut args := []bool{len: 3}
mut complete := false
2023-12-30 23:37:04 +04:00
for mut arg in args {
arg = false
}
2024-01-07 01:12:39 +04:00
for arg in os.args {
/*
2024-01-07 01:12:39 +04:00
match arg {
2023-12-30 23:37:04 +04:00
"-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.1")
2023-12-30 23:37:04 +04:00
exit(0)
2024-01-07 01:12:39 +04:00
} else {
if arg == os.args[0] {} else {
println("Unknown argument: ${}")
2023-12-30 23:37:04 +04:00
exit(1)
2024-01-07 01:12:39 +04:00
}
2023-12-30 23:37:04 +04:00
}
}*/
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)
}
2023-12-30 23:37:04 +04:00
}
2023-12-30 23:37:04 +04:00
}
2023-12-30 23:37:04 +04:00
if !args[2] {
categorymode(args)
} else {
allmode(args)
}
}
fn allmode(args []bool){
linkslist := os.ls('/usr/share/applications/') or { panic(err) }
println('<?xml version="1.0"?>\n<JWM>')
for i:=0; i < linkslist.len-1; i++ {
if linkslist[i].contains('.desktop') && !(linkslist[i].contains('krita_')){
println(buildprogram('/usr/share/applications/${linkslist[i]}', args))
}
}
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, catname) {
println(buildprogram('${folder}${elem}', args))
elem = '/IM/'
}
}
println('</Menu>')
}
2023-12-30 23:37:04 +04:00
fn categorymode(args []bool) {
mut linkslist := os.ls('/usr/share/applications/') or { panic(err) }
println('<?xml version="1.0"?>\n<JWM>')
2023-12-30 23:37:04 +04:00
//Clean up trash
for mut item in linkslist {
if !(item.contains('.desktop')) || item.contains('krita_') { item = '/IM/' }
}
buildcategory(args, '/usr/share/applications/', "Accessories", "Utility", mut linkslist)
buildcategory(args, '/usr/share/applications/', "Office", "Office", mut linkslist)
buildcategory(args, '/usr/share/applications/', "Games", "Game", mut linkslist)
buildcategory(args, '/usr/share/applications/', "Graphics", "Graphics", mut linkslist)
buildcategory(args, '/usr/share/applications/', "Internet", "Network", mut linkslist)
buildcategory(args, '/usr/share/applications/', "System Tools", "System", mut linkslist)
buildcategory(args, '/usr/share/applications/', "Other", "NaN", mut linkslist)
println('</JWM>')
2023-12-30 23:37:04 +04:00
}
fn buildprogram(filename string, args []bool) string {
//println("Opening")
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>'
}
fn strfinder(data []string, mode int) string {
for i:=0; i < data.len-1; i++ {
match mode {
1 {
// Jump to the next line if GenericName= found, we don't need it
if data[i].contains('GenericName=') { i++ }
if data[i].contains('Name=') { return filter(data[i].substr(5, data[i].len), 1) }
} 2 {
// Same as GenericName=
if data[i].contains('TryExec=') { i++ }
if data[i].contains('Exec=') { return filter(data[i].substr(5, data[i].len), 2) }
} 3 {
if data[i].contains('Icon=') { return data[i].substr(5, data[i].len) }
} 4 {
if data[i].contains('Comment=') { return filter(data[i].substr(8, data[i].len), 2) }
}
else { panic('strfinder: This should NOT happen. Something went REALLY wrong.') }
}
}
return('')
}
fn filter(strorig string, mode int) string {
mut str := strorig.bytes()
match mode {
1 {
if strorig.contains('&') { str = strorig.replace('&', 'and').bytes()}
return str.bytestr()
}
2 {
if strorig.contains('<') { str = strorig.replace('<', '').bytes() }
if str.bytestr().contains('>') { str = str.bytestr().replace('>', '').bytes() }
if str.bytestr().contains(' %u') { str = str.bytestr().replace(' %u', '').bytes() }
if str.bytestr().contains(' %U') { str = str.bytestr().replace(' %U', '').bytes() }
if str.bytestr().contains(' %F') { str = str.bytestr().replace(' %f', '').bytes() }
if str.bytestr().contains(' %F') { str = str.bytestr().replace(' %F', '').bytes() }
return str.bytestr()
} else { panic('filter: This should NOT happen. Something went REALLY wrong.') }
}
return('')
}
fn checktargetcategory(filename string, check string) bool {
//println("checking")
if check == "NaN" {
return true
}
2023-12-30 23:37:04 +04:00
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 }
}
return false
}
fn optbuildicon(data []string, icons bool) string {
if !icons { return '' }
return ('icon="${strfinder(data, 3)}"')
}
fn optbuildtooltip(data []string, tooltip bool) string {
if !tooltip { return '' }
return ('tooltip="${strfinder(data, 4)}"')
}
2024-01-07 01:12:39 +04:00
/*fn detectlang() string {
2024-01-07 01:12:39 +04:00
for item in (os.read_file('/etc/locale.conf') or { panic(err) }).split_into_lines() {
if item.contains('LANG=') { return item.substr(5, 10) }
}
return 'en_US'
}*/