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 return
} }
var FileNames, FileLastEdited []string var DirectoryEntries []DirectoryEntry
var FileSizes, FileNumChanges []int
if page == "ls" { if page == "ls" {
command = "/view" command = "/view"
FileNames, FileSizes, FileNumChanges, FileLastEdited = DirectoryList() DirectoryEntries = DirectoryList()
} }
// swap out /view for /read if it is published // 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] != "/l" &&
command[0:2] != "/h", command[0:2] != "/h",
"DirectoryPage": page == "ls", "DirectoryPage": page == "ls",
"FileNames": FileNames, "DirectoryEntries": DirectoryEntries,
"FileSizes": FileSizes,
"FileNumChanges": FileNumChanges,
"FileLastEdited": FileLastEdited,
"Page": page, "Page": page,
"RenderedPage": template.HTML([]byte(rawHTML)), "RenderedPage": template.HTML([]byte(rawHTML)),
"RawPage": rawText, "RawPage": rawText,

33
page.go
View File

@ -41,20 +41,31 @@ func Open(name string) (p *Page) {
return p 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) files, _ := ioutil.ReadDir(pathToData)
names = make([]string, len(files)) entries := make([]DirectoryEntry, len(files))
lengths = make([]int, len(files))
numchanges = make([]int, len(files))
lastEdited = make([]string, len(files))
for i, f := range files { for i, f := range files {
names[i] = DecodeFileName(f.Name()) name := DecodeFileName(f.Name())
p := Open(names[i]) p := Open(name)
lengths[i] = len(p.Text.GetCurrent()) entries[i] = DirectoryEntry{
numchanges[i] = p.Text.NumEdits() Name: name,
lastEdited[i] = time.Unix(p.Text.LastEditTime()/1000000000, 0).Format("Mon Jan 2 15:04:05 MST 2006") 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 { func DecodeFileName(s string) string {

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
// "fmt"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -17,9 +16,19 @@ func TestListFiles(t *testing.T) {
p.Update("A different bunch of data") p.Update("A different bunch of data")
p = Open("testpage3") p = Open("testpage3")
p.Update("Not much else") p.Update("Not much else")
n, l, _, _ := DirectoryList() n := DirectoryList()
if strings.Join(n, " ") != "testpage testpage2 testpage3" { if len(n) != 3 {
t.Errorf("Names: %s, Lengths: %d", n, l) 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>Num Edits</th>
<th>Last Edited</th> <th>Last Edited</th>
</tr> </tr>
{{range $i, $e := .FileNames}} {{range .DirectoryEntries}}
<tr> <tr>
<td><a href="/{{ $e }}/view">{{ $e }}</a></td> <td><a href="/{{ .Name }}/view">{{ .Name }}</a></td>
<td>{{index $.FileSizes $i}}</td> <td>{{.Length}}</td>
<td>{{index $.FileNumChanges $i}}</td> <td>{{.Numchanges}}</td>
<td>{{index $.FileLastEdited $i}}</td> <td>{{.LastEditTime}}</td>
</tr> </tr>
{{ end }} {{ end }}
</table> </table>