Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nvim init #11

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading