Skip to content

Commit

Permalink
Merge pull request #11 from shanehull/fix/nvim-init
Browse files Browse the repository at this point in the history
fix: nvim init
  • Loading branch information
shanehull authored Feb 14, 2024
2 parents 03c1555 + e7fb10a commit 1d3594c
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions cmd/shed/shed_zet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ import (

var (
secondBrain string
filename string
fileName string
subDir string
)

var vimCmd = "vim"
var vimCmd = "nvim"
var vimInit = "~/.config/nvim/init.lua"

var defaultSubDir = "0-inbox"

var fileTemplate = `---
date: %s
id: %s
aliases: []
tags:
-
- change-me
date: "%s"
---
# %s
Expand Down Expand Up @@ -62,7 +65,7 @@ var zetCommand = &cli.Command{
Aliases: []string{"f"},
Value: "",
Usage: "the name of the file (note) to create",
Destination: &filename,
Destination: &fileName,
},
},
Action: func(cCtx *cli.Context) error {
Expand All @@ -80,12 +83,12 @@ var zetCommand = &cli.Command{
subDir = defaultSubDir
}

if filename == "" {
if fileName == "" {
var err error

path := fmt.Sprintf("%s/%s", secondBrain, subDir)

filename, err = promptUniqueDashedFileName(path)
fileName, err = promptUniqueDashedFileName(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand All @@ -100,20 +103,30 @@ var zetCommand = &cli.Command{
}
}

fullFilePath := fmt.Sprintf("%s/%s/%s.md", secondBrain, subDir, filename)
fullFilePath := fmt.Sprintf("%s/%s/%s.md", secondBrain, subDir, fileName)

fmt.Println("Creating note:", fileName)

initialFileContents := fmt.Sprintf(
fileTemplate,
fileName,
time.Now().Format("2006-01-02"),
titleCase(strings.ReplaceAll(filename, "-", " ")),
titleCase(strings.ReplaceAll(fileName, "-", " ")),
)

if err := os.WriteFile(fullFilePath, []byte(initialFileContents), 0o644); err != nil {
fmt.Println(err)
os.Exit(1)
}

cmd := exec.Command(vimCmd, "+normal G", "+startinsert!", fullFilePath)
cmd := exec.Command(
vimCmd,
"-u",
vimInit,
"+normal G",
"+startinsert!",
fullFilePath,
)

cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
Expand Down Expand Up @@ -177,7 +190,7 @@ func promptUniqueDashedFileName(path string) (string, error) {

reg := regexp.MustCompile(`^[A-Za-z0-9-]+$`)
if !reg.MatchString(input) {
return errors.New("invalid filename")
return errors.New("invalid file name")
}

val = input
Expand All @@ -186,7 +199,7 @@ func promptUniqueDashedFileName(path string) (string, error) {
}

s := promptui.Prompt{
Label: "Filename",
Label: "File name",
Validate: validate,
AllowEdit: true,
}
Expand Down

0 comments on commit 1d3594c

Please sign in to comment.