next, last : glob and regexp
This commit is contained in:
parent
c19b11e115
commit
af2f2032f1
2 changed files with 72 additions and 3 deletions
33
next/last.go
33
next/last.go
|
|
@ -1,14 +1,39 @@
|
||||||
package next
|
package next
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Last() {
|
const (
|
||||||
ls, err := exec.Command("ls").Output()
|
LS_CMD = `ls -r [0-9]*.md`
|
||||||
|
LS_GREP_PIPE = `ls -r | grep -E '^[[:digit:]].*\.md'`
|
||||||
|
FILE_REGEXP = `(?m)^([0-9]+)\.([[:alpha:]]*)\.*md$`
|
||||||
|
)
|
||||||
|
|
||||||
|
var fileRegexp *regexp.Regexp = regexp.MustCompile(FILE_REGEXP)
|
||||||
|
|
||||||
|
func last() (string, error) {
|
||||||
|
// NOTE: globbing requires invoking the shell as cmd:
|
||||||
|
cmd := exec.Command("sh", "-c", LS_CMD)
|
||||||
|
ls, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Print(err)
|
return "", err
|
||||||
}
|
}
|
||||||
fmt.Printf("ls\n%s\n", string(ls))
|
lines := strings.Split(string(ls), "\n")
|
||||||
|
if len(lines) == 0 {
|
||||||
|
return "", errors.New("no um files in current dir")
|
||||||
|
}
|
||||||
|
return lines[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Last() {
|
||||||
|
s, err := last()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("err: %v\n", err)
|
||||||
|
}
|
||||||
|
fmt.Println(s)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
44
next/next.go
44
next/next.go
|
|
@ -1,7 +1,9 @@
|
||||||
package next
|
package next
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/brtholomy/um/go/flags"
|
"github.com/brtholomy/um/go/flags"
|
||||||
)
|
)
|
||||||
|
|
@ -38,7 +40,49 @@ func parseArgs(args []string) options {
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseFile(last string) (string, string, error) {
|
||||||
|
res := fileRegexp.FindStringSubmatch(last)
|
||||||
|
num := ""
|
||||||
|
desc := ""
|
||||||
|
if len(res) < 2 {
|
||||||
|
return num, desc, errors.New("no um files in current dir")
|
||||||
|
}
|
||||||
|
num = res[1]
|
||||||
|
if len(res) > 2 {
|
||||||
|
desc = res[2]
|
||||||
|
}
|
||||||
|
return num, desc, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func next(last string) (string, error) {
|
||||||
|
num, desc, err := parseFile(last)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
i, err := strconv.Atoi(num)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if desc != "" {
|
||||||
|
desc = desc + "."
|
||||||
|
}
|
||||||
|
width := len(num)
|
||||||
|
fmtstr := fmt.Sprintf("%%0%dd.%%smd", width)
|
||||||
|
n := fmt.Sprintf(fmtstr, i+1, desc)
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
func Next(args []string) {
|
func Next(args []string) {
|
||||||
opts := parseArgs(args)
|
opts := parseArgs(args)
|
||||||
fmt.Println(opts)
|
fmt.Println(opts)
|
||||||
|
|
||||||
|
l, err := last()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("err: %v\n", err)
|
||||||
|
}
|
||||||
|
n, err := next(l)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("err: %v\n", err)
|
||||||
|
}
|
||||||
|
fmt.Println(n)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue