diff --git a/handlers.go b/handlers.go index fe3eedf..6c26254 100755 --- a/handlers.go +++ b/handlers.go @@ -140,6 +140,13 @@ func handlePageRequest(c *gin.Context) { command[0:2] != "/l" && command[0:2] != "/h") + var FileNames, FileLastEdited []string + var FileSizes, FileNumChanges []int + if page == "ls" { + command = "/view" + FileNames, FileSizes, FileNumChanges, FileLastEdited = DirectoryList() + } + c.HTML(http.StatusOK, "index.tmpl", gin.H{ "EditPage": command[0:2] == "/e", // /edit "ViewPage": command[0:2] == "/v", // /view @@ -149,6 +156,11 @@ func handlePageRequest(c *gin.Context) { command[0:2] != "/v" && command[0:2] != "/l" && command[0:2] != "/h", + "DirectoryPage": page == "ls", + "FileNames": FileNames, + "FileSizes": FileSizes, + "FileNumChanges": FileNumChanges, + "FileLastEdited": FileLastEdited, "Page": page, "RenderedPage": template.HTML([]byte(rawHTML)), "RawPage": rawText, diff --git a/page.go b/page.go index ca53b0f..6b33ac6 100755 --- a/page.go +++ b/page.go @@ -7,6 +7,7 @@ import ( "path" "regexp" "strings" + "time" "github.com/schollz/versionedtext" ) @@ -38,6 +39,27 @@ func Open(name string) (p *Page) { return p } +func DirectoryList() (names []string, lengths []int, numchanges []int, lastEdited []string) { + files, _ := ioutil.ReadDir(pathToData) + names = make([]string, len(files)) + lengths = make([]int, len(files)) + numchanges = make([]int, len(files)) + lastEdited = make([]string, 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") + } + return +} + +func DecodeFileName(s string) string { + s2, _ := decodeFromBase32(strings.Split(s, ".")[0]) + return s2 +} + func (p *Page) Update(newText string) error { p.Text.Update(newText) p.Render() diff --git a/page_test.go b/page_test.go index 8b05832..7064466 100755 --- a/page_test.go +++ b/page_test.go @@ -7,6 +7,22 @@ import ( "testing" ) +func TestListFiles(t *testing.T) { + pathToData = "testdata" + os.MkdirAll(pathToData, 0755) + defer os.RemoveAll(pathToData) + p := Open("testpage") + p.Update("Some data") + p = Open("testpage2") + 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) + } +} + func TestGeneral(t *testing.T) { pathToData = "testdata" os.MkdirAll(pathToData, 0755) diff --git a/templates/index.tmpl b/templates/index.tmpl index acbd62e..876c14f 100755 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -453,6 +453,25 @@ {{ end }} {{ end }} + {{ if .DirectoryPage }} + + + + + + + + {{range $i, $e := .FileNames}} + + + + + + + {{ end }} +
DocumentCurrent sizeNum EditsLast Edited
{{ $e }}{{index $.FileSizes $i}}{{index $.FileNumChanges $i}}{{index $.FileLastEdited $i}}
+ {{end}} +