-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add $EDITOR functionality #76
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
Open
samuelstranges
wants to merge
2
commits into
DavidMiserak:main
Choose a base branch
from
samuelstranges:feat/editor-functionality
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // File: internal/data/editor.go | ||
|
|
||
| package data | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "time" | ||
|
|
||
| "github.com/DavidMiserak/GoCard/internal/model" | ||
| tea "github.com/charmbracelet/bubbletea" | ||
| ) | ||
|
|
||
| type EditorResponse struct { | ||
| FileName string | ||
| ExitCode error | ||
| IsEdit bool // true = editing, false = adding | ||
| CardID string // original card ID (for edits) | ||
| } | ||
|
|
||
| func getShellEditor() (string, error) { | ||
| editor := os.Getenv("EDITOR") | ||
| if editor == "" { | ||
| // Default to vi | ||
| editor = "vi" | ||
| } | ||
|
|
||
| return exec.LookPath(editor) | ||
| } | ||
|
|
||
| func LaunchEditor(file string, isEdit bool, cardID string) tea.Cmd { | ||
| editor, err := getShellEditor() | ||
|
|
||
| if err != nil { | ||
| return nil // can't launch | ||
| } | ||
|
|
||
| cmdToRun := exec.Command(editor, file) | ||
|
|
||
| return tea.ExecProcess(cmdToRun, func(result error) tea.Msg { | ||
| return EditorResponse{ | ||
| FileName: file, | ||
| ExitCode: result, | ||
| IsEdit: isEdit, | ||
| CardID: cardID, | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func getCardTemplate() string { | ||
| currentDate := time.Now().Format("2006-01-02") | ||
| template := fmt.Sprintf(`--- | ||
| tags: [] | ||
| created: %s | ||
| review_interval: 0 | ||
| --- | ||
|
|
||
| # Title | ||
|
|
||
| ## Question | ||
|
|
||
| ## Answer | ||
|
|
||
| `, currentDate) | ||
|
|
||
| return template | ||
|
|
||
| } | ||
|
|
||
| // Abstracted method to create temporary files with desired text | ||
| func createTmpFileWithText(text string) (string, error) { | ||
| tmpFile, err := os.CreateTemp("", "GoCard-tmp-*.md") | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| _, err = tmpFile.WriteString(text) | ||
| if err != nil { | ||
| tmpFile.Close() | ||
| return "", err | ||
| } | ||
|
|
||
| err = tmpFile.Close() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return tmpFile.Name(), nil | ||
| } | ||
|
|
||
| func CreateTmpFileWithCard(card model.Card) (string, error) { | ||
| originalFileContents, err := os.ReadFile(card.ID) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return createTmpFileWithText(string(originalFileContents)) | ||
| } | ||
|
|
||
| func CreateTmpFileWithTemplate() (string, error) { | ||
| return createTmpFileWithText(getCardTemplate()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| // File: internal/data/editor_test.go | ||
|
|
||
| package data | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/DavidMiserak/GoCard/internal/model" | ||
| ) | ||
|
|
||
| func TestGetShellEditor(t *testing.T) { | ||
| // Test assumes vi and vim exist in env where test is run | ||
| // and "nonexistentEditor123" doesn't exist | ||
|
|
||
| originalEditor := os.Getenv("EDITOR") | ||
| defer os.Setenv("EDITOR", originalEditor) | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| editorEnv string // to set | ||
| expectError bool | ||
| lookPathShouldContain string // e.g. LookPath has "vi" in path | ||
| }{ | ||
| { | ||
| name: "$EDITOR not set, fallback to vi", | ||
| editorEnv: "", | ||
| expectError: false, | ||
| lookPathShouldContain: "vi", | ||
| }, | ||
| { | ||
| name: "$EDITOR set to vim", | ||
| editorEnv: "vim", | ||
| expectError: false, | ||
| lookPathShouldContain: "vim", | ||
| }, | ||
| { | ||
| name: "$EDITOR set to nonexistent editor", | ||
| editorEnv: "nonexistentEditor123", | ||
| expectError: true, | ||
| lookPathShouldContain: "", | ||
| }, | ||
| } | ||
|
|
||
| // Run tests | ||
| for _, testCase := range testCases { | ||
| os.Setenv("EDITOR", testCase.editorEnv) | ||
| result, err := getShellEditor() | ||
|
|
||
| switch { | ||
| case testCase.expectError && err != nil: | ||
| // test passes | ||
| case testCase.expectError && err == nil: | ||
| t.Errorf("Expected error but got none") | ||
| case !testCase.expectError && err != nil: | ||
| t.Errorf("Unexpected error: %v", err) | ||
| case !testCase.expectError && err == nil: | ||
| pathIncludesEditor := strings.Contains(result, testCase.lookPathShouldContain) | ||
| if !pathIncludesEditor { | ||
| t.Errorf("Expected path to include %q, got %q", testCase.lookPathShouldContain, result) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestCreateTmpFileWithText(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| text string | ||
| }{ | ||
| {name: "Empty text", text: ""}, | ||
| {name: "Single line text", text: "Hello World!"}, | ||
| {name: "Multi line text", text: "Hello\nWorld\n!"}, | ||
| } | ||
|
|
||
| expectedFilenamePrefix := "GoCard-tmp-" | ||
| expectedFilenameSuffix := ".md" | ||
|
|
||
| for _, testCase := range testCases { | ||
| // Run function | ||
| filePath, err := createTmpFileWithText(testCase.text) | ||
| defer os.Remove(filePath) | ||
|
|
||
| if err != nil { | ||
| t.Errorf("%q returned unexpected %v", testCase.name, err) | ||
| } | ||
|
|
||
| // Verify existence of file | ||
| if _, err := os.Stat(filePath); os.IsNotExist(err) { | ||
| t.Errorf("createTmpFileWithText didnt create file at %s", filePath) | ||
| } | ||
|
|
||
| // Filename checks | ||
| fileName := filepath.Base(filePath) | ||
| fileNameHasExpectedPrefix := strings.HasPrefix(fileName, expectedFilenamePrefix) | ||
| fileNameHasExpectedSuffix := strings.HasSuffix(fileName, expectedFilenameSuffix) | ||
|
|
||
| if !fileNameHasExpectedPrefix { | ||
| t.Errorf("Expected filename prefix %q in file %q", expectedFilenamePrefix, fileName) | ||
| } | ||
| if !fileNameHasExpectedSuffix { | ||
| t.Errorf("Expected filename suffix %q in file %q", expectedFilenameSuffix, fileName) | ||
| } | ||
|
|
||
| // Check correct content exists | ||
| content, err := os.ReadFile(filePath) | ||
| if err != nil { | ||
| t.Errorf("Test %q failed to read file: %v", testCase.name, err) | ||
| continue | ||
| } | ||
| if string(content) != testCase.text { | ||
| t.Errorf("Test %q expected content %q but got %q", testCase.name, testCase.text, string(content)) | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| func TestCreateTmpFileWithTemplate(t *testing.T) { | ||
| expectedFilenamePrefix := "GoCard-tmp-" | ||
| expectedFilenameSuffix := ".md" | ||
|
|
||
| // Run function | ||
| filePath, err := CreateTmpFileWithTemplate() | ||
| defer os.Remove(filePath) | ||
| if err != nil { | ||
| t.Errorf("CreateTmpFileWithTemplate returned unexpected %v", err) | ||
| } | ||
|
|
||
| // Verify existence of file | ||
| if _, err := os.Stat(filePath); os.IsNotExist(err) { | ||
| t.Errorf("CreateTmpFileWithTemplate didnt create file at %s", filePath) | ||
| } | ||
|
|
||
| // Filename checks | ||
| fileName := filepath.Base(filePath) | ||
| fileNameHasExpectedPrefix := strings.HasPrefix(fileName, expectedFilenamePrefix) | ||
| fileNameHasExpectedSuffix := strings.HasSuffix(fileName, expectedFilenameSuffix) | ||
| if !fileNameHasExpectedPrefix { | ||
| t.Errorf("Expected filename prefix %q in file %q", expectedFilenamePrefix, fileName) | ||
| } | ||
| if !fileNameHasExpectedSuffix { | ||
| t.Errorf("Expected filename suffix %q in file %q", expectedFilenameSuffix, fileName) | ||
| } | ||
|
|
||
| // Check file contents exist | ||
| content, err := os.ReadFile(filePath) | ||
| if err != nil { | ||
| t.Errorf("CreateTmpFileWithTemplate failed to read file: %v", err) | ||
| } | ||
| actualLines := strings.Split(string(content), "\n") | ||
|
|
||
| // Check parts w/ ordering | ||
| expectedLines := []string{ | ||
| "---", | ||
| "tags: []", | ||
| "created: " + time.Now().Format("2006-01-02"), | ||
| "review_interval: 0", | ||
| "---", | ||
| "", | ||
| "# Title", | ||
| "", | ||
| "## Question", | ||
| "", | ||
| "## Answer", | ||
| "", | ||
| } | ||
| for index, writtenLine := range expectedLines { | ||
| actualLine := strings.TrimSpace(actualLines[index]) | ||
| writtenLine = strings.TrimSpace(writtenLine) | ||
|
|
||
| if actualLine != writtenLine { | ||
| t.Errorf("Line %d: expected %q, got %q", index, writtenLine, actualLine) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestCreateTmpFileWithCard(t *testing.T) { | ||
| // Create Fake File | ||
| tempDir, err := os.MkdirTemp("", "card-test") | ||
| if err != nil { | ||
| t.Fatalf("Cant cretae temp dir: %v", err) | ||
|
||
| } | ||
| defer os.RemoveAll(tempDir) | ||
| fakeCardPath := filepath.Join(tempDir, "test-card.md") | ||
| fakeCard := `--- | ||
| tags: [test,go] | ||
| created: 2025-01-01 | ||
| review_interval: 5 | ||
| difficulty: 2.3 | ||
| --- | ||
|
|
||
| # Fake Title | ||
|
|
||
| ## Question | ||
|
|
||
| Fake question | ||
|
|
||
| ## Answer | ||
|
|
||
| Fake answer | ||
| ` | ||
|
|
||
| testCard := model.Card{ | ||
| ID: fakeCardPath, | ||
| } | ||
|
|
||
| err = os.WriteFile(fakeCardPath, []byte(fakeCard), 0644) | ||
| if err != nil { | ||
| t.Fatalf("Failed to write fake card %v", err) | ||
| } | ||
|
|
||
| expectedFilenamePrefix := "GoCard-tmp-" | ||
| expectedFilenameSuffix := ".md" | ||
|
|
||
| // Run function | ||
| filePath, err := CreateTmpFileWithCard(testCard) | ||
| defer os.Remove(filePath) | ||
| if err != nil { | ||
| t.Errorf("CreateTmpFileWithCard returned unexpected %v", err) | ||
| } | ||
|
|
||
| // Verify existence of file | ||
| if _, err := os.Stat(filePath); os.IsNotExist(err) { | ||
| t.Errorf("CreateTmpFileWithCard didnt create file at %s", filePath) | ||
| } | ||
|
|
||
| // Filename checks | ||
| fileName := filepath.Base(filePath) | ||
| fileNameHasExpectedPrefix := strings.HasPrefix(fileName, expectedFilenamePrefix) | ||
| fileNameHasExpectedSuffix := strings.HasSuffix(fileName, expectedFilenameSuffix) | ||
| if !fileNameHasExpectedPrefix { | ||
| t.Errorf("Expected filename prefix %q in file %q", expectedFilenamePrefix, fileName) | ||
| } | ||
| if !fileNameHasExpectedSuffix { | ||
| t.Errorf("Expected filename suffix %q in file %q", expectedFilenameSuffix, fileName) | ||
| } | ||
|
|
||
| // Check file contents exist | ||
| content, err := os.ReadFile(filePath) | ||
| if err != nil { | ||
| t.Errorf("CreateTmpFileWithCard failed to read file: %v", err) | ||
| } | ||
|
|
||
| if string(content) != fakeCard { | ||
| t.Errorf("Temp file doesnt match card content") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When getShellEditor fails, LaunchEditor returns nil without any error indication to the caller, making it difficult to distinguish between successful completion and failure.