zntg/ui/pathresources.go
Sander Schobers 67e73a8671 Resources only exposes OpenResource (and Destroy).
PhysicalResources derives from Resources and exposes FetchResource.
Made dependency specific resource addons.
Extended the available resource options (fallback, path, refactored copy).
NewRenderer provides DefaultResources to the created renderer.
2020-05-25 22:24:06 +02:00

63 lines
2.1 KiB
Go

package ui
import (
"io"
"path/filepath"
)
var _ Resources = &PathResources{}
// PathResources implements Resources by adding a prefix to the requested source name before proxying it to its source.
type PathResources struct {
Source Resources
Prefix string
}
// NewPathResources creates a new Resources by adding a prefix to the requested source name before proxying it to its source. If source is nil it will use the OS file system for its resources.
func NewPathResources(source Resources, prefix string) *PathResources {
if source == nil {
source = &OSResources{}
}
return &PathResources{source, prefix}
}
// Destroy destroys the source.
func (r *PathResources) Destroy() error { return r.Source.Destroy() }
// OpenResource opens the resource with the prefixed name.
func (r *PathResources) OpenResource(name string) (io.ReadCloser, error) {
path := filepath.Join(r.Prefix, name)
return r.Source.OpenResource(path)
}
var _ PhysicalResources = &PathPhysicalResources{}
// PathPhysicalResources implements PhysicalResources by adding a prefix to the requested source name before proxying it to its source.
type PathPhysicalResources struct {
Source PhysicalResources
Prefix string
}
// NewPathPhysicalResources creates a new PhysicalResources by adding a prefix to the requested source name before proxying it to its source. If source is nil it will use the OS file system for its resources.
func NewPathPhysicalResources(source PhysicalResources, prefix string) *PathPhysicalResources {
if source == nil {
source = &OSResources{}
}
return &PathPhysicalResources{source, prefix}
}
// Destroy destroys the source.
func (r *PathPhysicalResources) Destroy() error { return r.Source.Destroy() }
// FetchResource fetches the resource with the prefixed name.
func (r *PathPhysicalResources) FetchResource(name string) (string, error) {
path := filepath.Join(r.Prefix, name)
return r.Source.FetchResource(path)
}
// OpenResource opens the resource with the prefixed name.
func (r *PathPhysicalResources) OpenResource(name string) (io.ReadCloser, error) {
path := filepath.Join(r.Prefix, name)
return r.Source.OpenResource(path)
}