Add support for limit and offset parameters for GET / endpoint
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Related #3372
authored by Junegunn Choi · committed by Junegunn Choi · parent edfdcc8 · Browse files · 2 files·+46 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+46 added−12 removed
- other:+46 added−12 removed
Provenance
- Commit
901939b- Tree
25211e6- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
edfdcc8
2 files changed+46 added−12 removed
modified:src/server.go+37 added-3 removed
6 unmodified lines7 "errors"8 "fmt"9 "net"10 "os"11+ "regexp"12 "strconv"13 "strings"14 "time"15 )16 17+var getRegex *regexp.Regexp18+19+func init() {20+ getRegex = regexp.MustCompile(`^GET /(?:\?([a-z0-9=&]+))? HTTP`)21+}22+23+type getParams struct {24+ limit int25+ offset int26+}27+28 const (29 crlf = "\r\n"30 httpOk = "HTTP/1.1 200 OK" + crlf31 httpBadRequest = "HTTP/1.1 400 Bad Request" + crlf96 unmodified lines128 for scanner.Scan() {129 text := scanner.Text()130 switch section {131 case 0:120- // TODO: Parameter support e.g. "GET /?limit=100 HTTP"121- if strings.HasPrefix(text, "GET / HTTP") {122- server.actionChannel <- []*action{{t: actResponse}}132+ getMatch := getRegex.FindStringSubmatch(text)133+ if len(getMatch) > 0 {134+ server.actionChannel <- []*action{{t: actResponse, a: getMatch[1]}}135 response := <-server.responseChannel136 return good(response)137 } else if !strings.HasPrefix(text, "POST / HTTP") {138 return bad("invalid request method")47 unmodified lines186 187 server.actionChannel <- actions188 return httpOk189 }190+191+func parseGetParams(query string) getParams {192+ params := getParams{limit: 100, offset: 0}193+ for _, pair := range strings.Split(query, "&") {194+ parts := strings.SplitN(pair, "=", 2)195+ if len(parts) == 2 {196+ switch parts[0] {197+ case "limit":198+ val, err := strconv.Atoi(parts[1])199+ if err == nil {200+ params.limit = val201+ }202+ case "offset":203+ val, err := strconv.Atoi(parts[1])204+ if err == nil {205+ params.offset = val206+ }207+ }208+ }209+ }210+ return params211+}modified:src/terminal.go+9 added-9 removed
150 unmodified lines151 }152 153 type Status struct {154 Reading bool `json:"reading"`155+ Progress int `json:"progress"`156 Query string `json:"query"`157 Position int `json:"position"`158 Sort bool `json:"sort"`159 TotalCount int `json:"totalCount"`2862 unmodified lines3022 doAction = func(a *action) bool {3023 switch a.t {3024 case actIgnore:3025 case actResponse:3025- t.serverOutputChan <- t.dumpStatus()3026+ t.serverOutputChan <- t.dumpStatus(parseGetParams(a.a))3027 case actBecome:3028 valid, list := t.buildPlusList(a.a, false)3029 if valid {3030 command := t.replacePlaceholder(a.a, false, string(t.input), list)771 unmodified lines3802 Text: i.AsString(t.ansi),3803 }3804 }3805 3805-const dumpItemLimit = 100 // TODO: Make configurable via GET parameter3806-3807-func (t *Terminal) dumpStatus() string {3806+func (t *Terminal) dumpStatus(params getParams) string {3807 selectedItems := t.sortSelected()3809- selected := make([]StatusItem, util.Min(dumpItemLimit, len(selectedItems)))3810- for i, selectedItem := range selectedItems[:len(selected)] {3811- selected[i] = t.dumpItem(selectedItem.item)3808+ selected := make([]StatusItem, util.Max(0, util.Min(params.limit, len(selectedItems)-params.offset)))3809+ for i := range selected {3810+ selected[i] = t.dumpItem(selectedItems[i+params.offset].item)3811 }3812 3814- matches := make([]StatusItem, util.Min(dumpItemLimit, t.merger.Length()))3813+ matches := make([]StatusItem, util.Max(0, util.Min(params.limit, t.merger.Length()-params.offset)))3814 for i := range matches {3816- matches[i] = t.dumpItem(t.merger.Get(i).item)3815+ matches[i] = t.dumpItem(t.merger.Get(i + params.offset).item)3816 }3817 3818 var current *StatusItem3819 currentItem := t.currentItem()3 unmodified lines3823 }3824 3825 dump := Status{3826 Reading: t.reading,3827+ Progress: t.progress,3828 Query: string(t.input),3829 Position: t.cy,3830 Sort: t.sort,3831 TotalCount: t.count,8 unmodified lines