-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
92 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,35 +1,45 @@ | ||
package entryui | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/bashbunni/project-management/entry" | ||
"os" | ||
"os/exec" | ||
"github.com/pkg/errors" | ||
"github.com/bashbunni/project-management/utils" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
func (m Model) createEntryCmd(activeProject uint, er *entry.GormRepository) tea.Cmd { | ||
return func() tea.Msg { | ||
err := m.p.ReleaseTerminal() | ||
if err != nil { | ||
log.Print(err) | ||
return errMsg{err} | ||
} | ||
input, err := utils.CaptureInputFromFile() | ||
if err != nil { | ||
log.Print(err) | ||
return errMsg{err} | ||
} | ||
err = er.CreateEntry(input, activeProject) | ||
if err != nil { | ||
log.Print(err) | ||
return errMsg{err} | ||
func openEditorCmd() tea.Cmd { | ||
file, err := os.CreateTemp(os.TempDir(), "") | ||
if err != nil { | ||
return func() tea.Msg { | ||
return errMsg{errors.Wrap(err, "cannot create temp file")} | ||
} | ||
err = m.p.RestoreTerminal() | ||
if err != nil { | ||
log.Print(err) | ||
return errMsg{err} | ||
} | ||
return updateEntryListMsg{} | ||
} | ||
filename := file.Name() | ||
c := exec.Command(os.Getenv("EDITOR"), filename) | ||
return tea.Exec(tea.WrapExecCommand(c), func(err error) tea.Msg { | ||
return editorFinishedMsg{err, file} | ||
}) | ||
} | ||
|
||
func (m Model) updateEntriesCmd() tea.Msg { | ||
m.setViewportContent() | ||
return updatedMsg{} | ||
} | ||
|
||
func (m Model) createEntryCmd(file *os.File) tea.Cmd { | ||
return func() tea.Msg { | ||
defer file.Close() | ||
input, err := utils.ReadFile(file.Name()) | ||
if err != nil { | ||
return errMsg{errors.Wrap(err, "cannot read file in createEntryCmd")} | ||
} | ||
if m.er.CreateEntry(input, m.activeProjectID); err != nil { | ||
return errMsg{errors.Wrap(err, "cannot create entry")} | ||
} | ||
if err := os.Remove(file.Name()); err != nil { | ||
return errMsg{errors.Wrap(err, "cannot remove file")} | ||
} | ||
return updateEntryListMsg{input} | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,12 @@ | ||
package entryui | ||
|
||
import "os" | ||
|
||
type errMsg struct{ error } // TODO: have this implement Error() | ||
type updateEntryListMsg struct{} | ||
type updateEntryListMsg struct{input []byte} | ||
type updatedMsg struct{} | ||
|
||
type editorFinishedMsg struct { | ||
err error | ||
file *os.File | ||
} |
This file contains 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 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