From ba330e9e0dae07393a959b6390c9f6829d7fffae Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 22 Oct 2024 15:50:52 -0400 Subject: [PATCH] cmd::desktop_entry Add beginnings of desktop entry example --- cmd/desktop_entry/main.ha | 102 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 cmd/desktop_entry/main.ha diff --git a/cmd/desktop_entry/main.ha b/cmd/desktop_entry/main.ha new file mode 100644 index 0000000..8ac619a --- /dev/null +++ b/cmd/desktop_entry/main.ha @@ -0,0 +1,102 @@ +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", + ('l', "locale", "the name of the locale to use"), + ('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); + }; + + 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; + }; + // TODO +}; + +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)?; +};