You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package gopher
|
|
|
|
import (
|
|
"gophi/core"
|
|
"os"
|
|
)
|
|
|
|
// generatedFileContents is a simple core.FileContent implementation for holding onto a generated (virtual) file contents
|
|
type generatedFileContent struct {
|
|
content []byte
|
|
}
|
|
|
|
// ReadAllFrom does nothing for generated content
|
|
func (fc *generatedFileContent) Load(p *core.Path, file *os.File) error { return nil }
|
|
|
|
// WriteAllTo writes the generated FileContent to client
|
|
func (fc *generatedFileContent) WriteToClient(client *core.Client, p *core.Path) error {
|
|
return client.Conn().Write(fc.content)
|
|
}
|
|
|
|
// Clear does nothing
|
|
func (fc *generatedFileContent) Clear() {}
|
|
|
|
// gophermapContents is an implementation of core.FileContent that holds individually renderable sections of a gophermap
|
|
type gophermapContent struct {
|
|
sections []gophermapSection
|
|
}
|
|
|
|
// Load takes an open FD and loads the gophermap contents into memory as different renderable sections
|
|
func (gc *gophermapContent) Load(path *core.Path, file *os.File) error {
|
|
var err error
|
|
gc.sections, err = readGophermap(file, path)
|
|
return err
|
|
}
|
|
|
|
// WriteToClient renders each cached section of the gophermap, and writes them to the client
|
|
func (gc *gophermapContent) WriteToClient(client *core.Client, path *core.Path) error {
|
|
// Render + write the sections!
|
|
for _, section := range gc.sections {
|
|
err := section.RenderAndWrite(client)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Finally, write the footer (including last-line)
|
|
return client.Conn().Write(footer)
|
|
}
|
|
|
|
// Clear empties currently cached GophermapContents memory
|
|
func (gc *gophermapContent) Clear() {
|
|
gc.sections = nil
|
|
}
|