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

feat: allow to open prompt in the editor #419

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ var help = map[string]string{
"show": "Show a saved conversation with the given title or ID.",
"theme": "Theme to use in the forms. Valid units are: 'charm', 'catppuccin', 'dracula', and 'base16'",
"show-last": "Show the last saved conversation.",
"editor": "Edit the prompt in your $EDITOR. Only taken into account if no other args and if STDIN is a TTY.",
}

// Model represents the LLM model used in the API call.
Expand Down Expand Up @@ -180,6 +181,7 @@ type Config struct {
DeleteOlderThan time.Duration
User string

openEditor bool
cacheReadFromID, cacheWriteToID, cacheWriteToTitle string
}

Expand Down
37 changes: 37 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ var (
config.Quiet = true
}

if isNoArgs() && isInputTTY() && config.openEditor {
prompt, err := prefixFromEditor()
if err != nil {
return err
}
config.Prefix = prompt
}

if (isNoArgs() || config.AskModel) && isInputTTY() {
if err := askInfo(); err != nil && err == huh.ErrUserAborted {
return modsError{
Expand Down Expand Up @@ -256,6 +264,7 @@ func initFlags() {
flags.StringVarP(&config.Role, "role", "R", config.Role, stdoutStyles().FlagDesc.Render(help["role"]))
flags.BoolVar(&config.ListRoles, "list-roles", config.ListRoles, stdoutStyles().FlagDesc.Render(help["list-roles"]))
flags.StringVar(&config.Theme, "theme", "charm", stdoutStyles().FlagDesc.Render(help["theme"]))
flags.BoolVarP(&config.openEditor, "editor", "e", false, stdoutStyles().FlagDesc.Render(help["editor"]))
flags.Lookup("prompt").NoOptDefVal = "-1"
flags.SortFlags = false

Expand Down Expand Up @@ -837,3 +846,31 @@ func themeFrom(theme string) *huh.Theme {
return huh.ThemeCharm()
}
}

// creates a temp file, opens it in user's editor, and then returns its contents.
func prefixFromEditor() (string, error) {
f, err := os.CreateTemp("", "prompt")
if err != nil {
return "", fmt.Errorf("could not create temporary file: %w", err)
}
_ = f.Close()
defer func() { _ = os.Remove(f.Name()) }()
cmd, err := editor.Cmd(
"mods",
f.Name(),
)
if err != nil {
return "", fmt.Errorf("could not open editor: %w", err)
}
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("could not open editor: %w", err)
}
prompt, err := os.ReadFile(f.Name())
if err != nil {
return "", fmt.Errorf("could not read file: %w", err)
}
return string(prompt), nil
}
Loading