Skip to content

Instantly share code, notes, and snippets.

@sdondley
Created June 21, 2022 23:38
Show Gist options
  • Save sdondley/3a91deec89bb193c26419b0954966e46 to your computer and use it in GitHub Desktop.
Save sdondley/3a91deec89bb193c26419b0954966e46 to your computer and use it in GitHub Desktop.
use Injector;
use Data::Dump;
use lib '../Menu-Simple/lib';
use Menu::Simple;
unit class Distribution::Resources::Menu;
class ResourceMenu is export {
has CompUnit::Repository::Distribution $.dist is injected<dist>;
has Menu $.menu is rw;
has Hash $.menu-hash is rw;
has Str @.paths;
has Bool $.built is rw = False;
method action(Str $path) {
say $path;
my $file = %?RESOURCES{$path};
say $file;
}
sub hash-from-path(@list, $last-item, %parent = {} ) {
return if !@list;
%parent{$last-item} = {} if !%parent{$last-item};
my $this-item = @list.shift;
if !@list {
%parent{$last-item}{ $this-item } = 1;
}
hash-from-path(@list, $this-item, %parent{$last-item});
return %parent;
}
sub build-hash(@paths) {
my %hash;
for @paths {
my @array = $_.Array;
my $root = @array.shift;
%hash = hash-from-path(@array, $root, %hash);
}
return %hash;
}
method add-submenu($menu, %hash) {
my $submenu = Menu.new();
say %hash;
say %hash.keys;
for %hash.sort {
my $key = .key;
say %hash{$key};
if %hash{$key} eq 1 {
say $key;
$submenu.add-option($key);
} else {
my $sub-submenu = Menu.new();
say $sub-submenu.menuID;
$sub-submenu.add-option($key);
say $submenu;
say $sub-submenu;
$submenu.add-submenu($sub-submenu);
}
}
$menu.add-submenu($submenu);
}
method build-menu(%hash) {
my $menu = Menu.new();
self.menu = $menu;
for %hash.sort {
my $key = .key;
$menu.add-options: %hash{$key}.keys.sort;
for %hash{$key}.keys.sort {
my %subhash = %hash{$key}{$_};
self.add-submenu($menu, %subhash);
}
}
return $menu;
}
method menu-populate() {
my @paths;
push @paths, .split: '/' for self.paths;
my %hash = build-hash(@paths);
self.build-menu(%hash);
}
method init() {
self.paths = self.dist.meta<resources>.sort;
self.menu-populate;
self.built = True;
}
method execute() {
self.init if !self.built;
self.menu.execute;
}
}
=begin pod
=head1 NAME
Distribution::Resources::Menu - blah blah blah
=head1 SYNOPSIS
=begin code :lang<raku>
use Distribution::Resources::Menu;
=end code
=head1 DESCRIPTION
Distribution::Resources::Menu allows users to navigate and select a file from a
distribution's C<resources> directory.
=head1 AUTHOR
Steve Dondley <[email protected]>
=head1 COPYRIGHT AND LICENSE
Copyright 2022 Steve Dondley
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
=end pod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment