Add appbuilder and config
This commit is contained in:
171
jwmappbuilder.v
Normal file
171
jwmappbuilder.v
Normal file
@@ -0,0 +1,171 @@
|
||||
import os
|
||||
|
||||
fn main() {
|
||||
mut args := []bool{len: 3}
|
||||
for mut arg in args {
|
||||
arg = false
|
||||
}
|
||||
for i := 0; i < os.args.len; i++ {
|
||||
if i == 0 {i++}
|
||||
match os.args[i] {
|
||||
"-i", "--icons" { args[0] = true }
|
||||
"-t", "--tooltip" { args[1] = true }
|
||||
"-a", "--all" { args[2] = true }
|
||||
"-h", "--help" {
|
||||
println("\nUsage:\n ${os.args[0]} [-htia]\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")
|
||||
exit(0)
|
||||
}
|
||||
else {
|
||||
println("Unknown argument: ${os.args[i]}")
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
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 categorymode(args []bool) {
|
||||
mut linkslist := os.ls('/usr/share/applications/') or { panic(err) }
|
||||
println('<?xml version="1.0"?>\n<JWM>\n<Menu label="Accessories">')
|
||||
//Clean up trash
|
||||
for mut item in linkslist {
|
||||
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>')
|
||||
}
|
||||
|
||||
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")
|
||||
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)}"')
|
||||
}
|
Reference in New Issue
Block a user