1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Use a struct for related data instead of four separate arrays

This commit is contained in:
Daniel Heath 2018-01-18 17:05:07 +11:00
parent c802fa06be
commit a36f8e318e
5 changed files with 87 additions and 71 deletions

File diff suppressed because one or more lines are too long

View File

@ -293,11 +293,10 @@ func handlePageRequest(c *gin.Context) {
return
}
var FileNames, FileLastEdited []string
var FileSizes, FileNumChanges []int
var DirectoryEntries []DirectoryEntry
if page == "ls" {
command = "/view"
FileNames, FileSizes, FileNumChanges, FileLastEdited = DirectoryList()
DirectoryEntries = DirectoryList()
}
// swap out /view for /read if it is published
@ -316,10 +315,7 @@ func handlePageRequest(c *gin.Context) {
command[0:2] != "/l" &&
command[0:2] != "/h",
"DirectoryPage": page == "ls",
"FileNames": FileNames,
"FileSizes": FileSizes,
"FileNumChanges": FileNumChanges,
"FileLastEdited": FileLastEdited,
"DirectoryEntries": DirectoryEntries,
"Page": page,
"RenderedPage": template.HTML([]byte(rawHTML)),
"RawPage": rawText,

33
page.go
View File

@ -41,20 +41,31 @@ func Open(name string) (p *Page) {
return p
}
func DirectoryList() (names []string, lengths []int, numchanges []int, lastEdited []string) {
type DirectoryEntry struct {
Name string
Length int
Numchanges int
LastEdited time.Time
}
func (d DirectoryEntry) LastEditTime() string {
return d.LastEdited.Format("Mon Jan 2 15:04:05 MST 2006")
}
func DirectoryList() []DirectoryEntry {
files, _ := ioutil.ReadDir(pathToData)
names = make([]string, len(files))
lengths = make([]int, len(files))
numchanges = make([]int, len(files))
lastEdited = make([]string, len(files))
entries := make([]DirectoryEntry, len(files))
for i, f := range files {
names[i] = DecodeFileName(f.Name())
p := Open(names[i])
lengths[i] = len(p.Text.GetCurrent())
numchanges[i] = p.Text.NumEdits()
lastEdited[i] = time.Unix(p.Text.LastEditTime()/1000000000, 0).Format("Mon Jan 2 15:04:05 MST 2006")
name := DecodeFileName(f.Name())
p := Open(name)
entries[i] = DirectoryEntry{
Name: name,
Length: len(p.Text.GetCurrent()),
Numchanges: p.Text.NumEdits(),
LastEdited: time.Unix(p.Text.LastEditTime()/1000000000, 0),
}
}
return
return entries
}
func DecodeFileName(s string) string {

View File

@ -1,7 +1,6 @@
package main
import (
// "fmt"
"os"
"strings"
"testing"
@ -17,9 +16,19 @@ func TestListFiles(t *testing.T) {
p.Update("A different bunch of data")
p = Open("testpage3")
p.Update("Not much else")
n, l, _, _ := DirectoryList()
if strings.Join(n, " ") != "testpage testpage2 testpage3" {
t.Errorf("Names: %s, Lengths: %d", n, l)
n := DirectoryList()
if len(n) != 3 {
t.Error("Expected three directory entries")
t.FailNow()
}
if n[0].Name != "testpage" {
t.Error("Expected testpage to be first")
}
if n[1].Name != "testpage2" {
t.Error("Expected testpage2 to be second")
}
if n[2].Name != "testpage3" {
t.Error("Expected testpage3 to be last")
}
}

View File

@ -602,12 +602,12 @@ body#pad textarea {
<th>Num Edits</th>
<th>Last Edited</th>
</tr>
{{range $i, $e := .FileNames}}
{{range .DirectoryEntries}}
<tr>
<td><a href="/{{ $e }}/view">{{ $e }}</a></td>
<td>{{index $.FileSizes $i}}</td>
<td>{{index $.FileNumChanges $i}}</td>
<td>{{index $.FileLastEdited $i}}</td>
<td><a href="/{{ .Name }}/view">{{ .Name }}</a></td>
<td>{{.Length}}</td>
<td>{{.Numchanges}}</td>
<td>{{.LastEditTime}}</td>
</tr>
{{ end }}
</table>