hare-xdg/cmd/desktop_entry/main.ha

105 lines
2.9 KiB
Hare
Raw Normal View History

use fmt;
use format::desktop_entry;
use getopt;
use io;
use locale;
use os;
export fn main() void = {
const name = os::args[0];
const cmd = getopt::parse(
os::args,
"parse and display desktop entries",
2024-10-22 13:52:07 -06:00
('l', "locale", "the name of the locale to use"), // FIXME not working
('a', "action", "display a specific action"),
"file");
defer getopt::finish(&cmd);
let local = "";
let action = "";
for (let opt .. cmd.opts) switch (opt.0) {
case 'l' => local = opt.1;
case 'a' => action = opt.1;
case => abort();
};
let local = if (local == "") {
yield locale::get_messages();
} else {
yield locale::parse(local)!;
};
let file_name = if (len(cmd.args) == 1) {
yield cmd.args[0];
} else {
fmt::fprintf(os::stderr, "{}: expected 1 argument\n", name)!;
getopt::printusage(os::stderr, name, cmd.help)!;
os::exit(os::status::FAILURE);
};
2024-10-22 13:52:07 -06:00
// TODO put in function and handle errors here
let file = os::open(file_name)!;
defer io::close(file)!;
let file = desktop_entry::parse(file)!;
let result = if (action == "") {
yield print_file(file, local);
} else {
yield print_action(file, local, action);
};
match(result) {
case let err: desktop_entry::error =>
fmt::fprintf(os::stderr, "{}: {}\n", name, desktop_entry::strerror(err))!;
case void => void;
};
};
fn print_file (file: desktop_entry::file, local: locale::locale) (void | desktop_entry::error) = {
fmt::printf("Type={}\n", file.typ)?;
fmt::printf("Version={}\n", file.version)?;
match (locale::string_resolve(file.name, local)) {
case let name: str => fmt::printf("Name={}\n", name)?;
case void => void;
};
match (locale::string_resolve(file.generic_name, local)) {
case let generic_name: str => if (generic_name != "") fmt::printf("GenericName={}\n", generic_name)?;
case void => void;
};
if(file.no_display) fmt::println("NoDisplay=true")?;
match (locale::string_resolve(file.comment, local)) {
case let comment: str => if (comment != "") fmt::printf("Comment={}\n", comment)?;
case void => void;
};
match (locale::string_resolve(file.icon, local)) {
case let icon: str => if (icon != "") fmt::printf("Icon={}\n", icon)?;
case void => void;
};
2024-10-22 13:52:07 -06:00
// TODO all other keys
// https://specifications.freedesktop.org/desktop-entry-spec/latest/recognized-keys.html
};
fn print_action (file: desktop_entry::file, local: locale::locale, key: str) (void | desktop_entry::error) = {
let action: (desktop_entry::action | void) = void;
for (let actio .. file.actions) {
if (actio.key == key) {
action = actio;
break;
};
};
let action = match (action) {
case let action: desktop_entry::action => yield action;
case void => return;
};
match (locale::string_resolve(action.name, local)) {
case let name: str => fmt::printf("Name={}\n", name)?;
case void => void;
};
match (locale::string_resolve(action.icon, local)) {
case let icon: str => if (icon != "") fmt::printf("Icon={}\n", icon)?;
case void => void;
};
if (action.exec != "") fmt::printf("Exec={}\n", action.exec)?;
};