Skip to content

Commit 10e604e

Browse files
committed
feat(commit): add editor code
add editor code #19 Signed-off-by: mritd <mritd@linux.com>
1 parent 4160975 commit 10e604e

File tree

5 files changed

+121
-47
lines changed

5 files changed

+121
-47
lines changed

consts.go

+2
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ const commitMessageCheckFailedMsg = `
3737
│ │
3838
╰──────────────────────────────────────────────────╯`
3939

40+
const editorKey = "//edit"
41+
4042
const luckyCommitEnv = "MMH_LUCKY_COMMIT"
4143
const strictHostKey = "MMH_STRICT_HOST_KEY"

go.mod

+12-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.17
44

55
require (
66
github.com/charmbracelet/bubbles v0.9.0
7-
github.com/charmbracelet/bubbletea v0.17.0
7+
github.com/charmbracelet/bubbletea v0.19.3
88
github.com/charmbracelet/lipgloss v0.4.0
99
github.com/mattn/go-runewidth v0.0.13
1010
github.com/mitchellh/go-homedir v1.1.0
@@ -13,17 +13,20 @@ require (
1313

1414
require (
1515
github.com/atotto/clipboard v0.1.2 // indirect
16-
github.com/containerd/console v1.0.2 // indirect
17-
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
16+
github.com/containerd/console v1.0.3 // indirect
17+
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
1818
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
19-
github.com/mattn/go-isatty v0.0.13 // indirect
19+
github.com/mattn/go-isatty v0.0.14 // indirect
20+
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect
2021
github.com/muesli/reflow v0.3.0 // indirect
2122
github.com/muesli/termenv v0.9.0 // indirect
22-
github.com/pkg/errors v0.9.1 // indirect
2323
github.com/rivo/uniseg v0.2.0 // indirect
24-
github.com/russross/blackfriday/v2 v2.0.1 // indirect
24+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
2525
github.com/sahilm/fuzzy v0.1.0 // indirect
26-
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
27-
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
28-
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed // indirect
26+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
27+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
28+
)
29+
30+
replace (
31+
github.com/charmbracelet/bubbletea v0.19.3 => /Users/bleem/gopath/src/github.com/charmbracelet/bubbletea
2932
)

go.sum

+19-11
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,66 @@ github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn
44
github.com/charmbracelet/bubbles v0.9.0 h1:lqJ8FXwoLceQF2J0A+dWo1Cuu1dNyjbW4Opgdi2vkhw=
55
github.com/charmbracelet/bubbles v0.9.0/go.mod h1:NWT/c+0rYEnYChz5qCyX4Lj6fDw9gGToh9EFJPajghU=
66
github.com/charmbracelet/bubbletea v0.14.1/go.mod h1:b5lOf5mLjMg1tRn1HVla54guZB+jvsyV0yYAQja95zE=
7-
github.com/charmbracelet/bubbletea v0.17.0 h1:ABmd01l6K7Q8lSQxF7yG8yWayz5gXWSt3JfB/X1eYP4=
8-
github.com/charmbracelet/bubbletea v0.17.0/go.mod h1:YTZSs2p3odhwYZdhqJheYHVUjU37c9OLgS85kw6NGQY=
7+
github.com/charmbracelet/bubbletea v0.19.3 h1:OKeO/Y13rQQqt4snX+lePB0QrnW80UdrMNolnCcmoAw=
8+
github.com/charmbracelet/bubbletea v0.19.3/go.mod h1:VuXF2pToRxDUHcBUcPmCRUHRvFATM4Ckb/ql1rBl3KA=
99
github.com/charmbracelet/harmonica v0.1.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
1010
github.com/charmbracelet/lipgloss v0.3.0/go.mod h1:VkhdBS2eNAmRkTwRKLJCFhCOVkjntMusBDxv7TXahuk=
1111
github.com/charmbracelet/lipgloss v0.4.0 h1:768h64EFkGUr8V5yAKV7/Ta0NiVceiPaV+PphaW1K9g=
1212
github.com/charmbracelet/lipgloss v0.4.0/go.mod h1:vmdkHvce7UzX6xkyf4cca8WlwdQ5RQr8fzta+xl7BOM=
1313
github.com/containerd/console v1.0.1/go.mod h1:XUsP6YE/mKtz6bxc+I8UiKKTP04qjQL4qcS3XoQ5xkw=
14-
github.com/containerd/console v1.0.2 h1:Pi6D+aZXM+oUw1czuKgH5IJ+y0jhYcwBJfx5/Ghn9dE=
1514
github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ=
16-
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
15+
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
16+
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
1717
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
18+
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
19+
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
1820
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
1921
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
2022
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
2123
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
2224
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
23-
github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA=
2425
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
26+
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
27+
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
2528
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
2629
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
2730
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
2831
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
2932
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
3033
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
34+
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
35+
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 h1:kMlmsLSbjkikxQJ1IPwaM+7LJ9ltFu/fi8CRzvSnQmA=
36+
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
3137
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
3238
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
3339
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
3440
github.com/muesli/termenv v0.8.1/go.mod h1:kzt/D/4a88RoheZmwfqorY3A+tnsSMA9HJC/fQSFKo0=
3541
github.com/muesli/termenv v0.9.0 h1:wnbOaGz+LUR3jNT0zOzinPnyDaCZUQRZj9GxK8eRVl8=
3642
github.com/muesli/termenv v0.9.0/go.mod h1:R/LzAKf+suGs4IsO95y7+7DpFHO0KABgnZqtlyx2mBw=
37-
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
3843
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
39-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4044
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4145
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
4246
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
4347
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
44-
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
4548
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
49+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
50+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
4651
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
4752
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
48-
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
4953
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
5054
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
5155
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
5256
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5357
golang.org/x/sys v0.0.0-20200916030750-2334cc1a136f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5458
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5559
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
56-
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
5760
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
58-
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed h1:Ei4bQjjpYUsS4efOUz+5Nz++IVkHk87n2zBA0NxBWc0=
61+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
62+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
63+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
64+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5965
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
66+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
67+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
6068
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6169
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

ui_commit.go

+13
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ func (m commitModel) commit() tea.Msg {
100100
SOB: sob,
101101
}
102102

103+
//if msg.Scope == editorKey {
104+
// msg.Scope = m.views[INPUTS].(inputsModel).editorInputs[0]
105+
//}
106+
//if msg.Subject == editorKey {
107+
// msg.Subject = m.views[INPUTS].(inputsModel).editorInputs[1]
108+
//}
109+
//if msg.Body == editorKey {
110+
// msg.Body = m.views[INPUTS].(inputsModel).editorInputs[2]
111+
//}
112+
//if msg.Footer == editorKey {
113+
// msg.Footer = m.views[INPUTS].(inputsModel).editorInputs[3]
114+
//}
115+
103116
if msg.Body == "" {
104117
msg.Body = msg.Subject
105118
}

ui_commit_inputs.go

+75-27
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,46 @@ var (
7272
spinnerMetaFrame3 = lipgloss.NewStyle().Foreground(lipgloss.Color("2")).Render("❯")
7373
)
7474

75+
//
76+
//func openEditor() string {
77+
// f, err := ioutil.TempFile("", "gitflow-toolkit")
78+
// if err != nil {
79+
// panic(err)
80+
// }
81+
// defer func() {
82+
// _ = f.Close()
83+
// _ = os.Remove(f.Name())
84+
// }()
85+
//
86+
// // write utf8 bom
87+
// _, err = f.Write([]byte{0xEF, 0xBB, 0xBF})
88+
// if err != nil {
89+
// panic(err)
90+
// }
91+
//
92+
// editor := "vim"
93+
// if runtime.GOOS == "windows" {
94+
// editor = "notepad"
95+
// }
96+
// if v := os.Getenv("VISUAL"); v != "" {
97+
// editor = v
98+
// } else if e := os.Getenv("EDITOR"); e != "" {
99+
// editor = e
100+
// }
101+
//
102+
// cmd := exec.Command(editor, f.Name())
103+
// cmd.Stdin = os.Stdin
104+
// cmd.Stdout = os.Stdout
105+
// cmd.Stderr = os.Stderr
106+
// _ = cmd.Run()
107+
// raw, err := ioutil.ReadFile(f.Name())
108+
// if err != nil {
109+
// panic(err)
110+
// }
111+
//
112+
// return strings.TrimSpace(string(bytes.TrimPrefix(raw, []byte{0xEF, 0xBB, 0xBF})))
113+
//}
114+
75115
type inputWithCheck struct {
76116
input textinput.Model
77117
checker func(s string) error
@@ -81,53 +121,61 @@ type inputsModel struct {
81121
focusIndex int
82122
title string
83123
inputs []inputWithCheck
124+
//editorInputs []string
84125
err error
85126
errSpinner spinner.Model
127+
editMode bool
86128
}
87129

88130
func (m inputsModel) Init() tea.Cmd {
89131
return tea.Batch(textinput.Blink, spinner.Tick)
90132
}
91133

92134
func (m inputsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
135+
if m.editMode {
136+
return m, nil
137+
}
138+
93139
switch msg := msg.(type) {
94140
case tea.KeyMsg:
141+
var renderCursor bool
95142
switch msg.String() {
96143
case "ctrl+c", "esc":
97144
return m, tea.Quit
98-
99-
// Set focus to next input
100-
case "tab", "shift+tab", "enter", "up", "down":
101-
s := msg.String()
102-
103-
if s == "enter" {
104-
if m.focusIndex == len(m.inputs) {
105-
for _, iwc := range m.inputs {
106-
if iwc.checker != nil {
107-
m.err = iwc.checker(iwc.input.Value())
108-
if m.err != nil {
109-
return m, spinner.Tick
110-
}
145+
case "enter":
146+
if m.focusIndex == len(m.inputs) {
147+
for _, iwc := range m.inputs {
148+
if iwc.checker != nil {
149+
m.err = iwc.checker(iwc.input.Value())
150+
if m.err != nil {
151+
return m, spinner.Tick
111152
}
112153
}
113-
return m, func() tea.Msg { return done{nextView: COMMIT} }
114154
}
115-
155+
return m, func() tea.Msg { return done{nextView: COMMIT} }
116156
}
117-
118-
// Cycle indexes
119-
if s == "up" || s == "shift+tab" {
120-
m.focusIndex--
121-
} else {
122-
m.focusIndex++
123-
}
124-
157+
//if m.inputs[m.focusIndex].input.Value() == editorKey {
158+
// m.editMode = true
159+
// m.editorInputs[m.focusIndex] = openEditor()
160+
// m.editMode = false
161+
// return m, tea.HideCursor
162+
//}
163+
fallthrough
164+
case "tab", "down":
165+
m.focusIndex++
125166
if m.focusIndex > len(m.inputs) {
126167
m.focusIndex = 0
127-
} else if m.focusIndex < 0 {
168+
}
169+
renderCursor = true
170+
case "shift+tab", "up":
171+
m.focusIndex--
172+
if m.focusIndex < 0 {
128173
m.focusIndex = len(m.inputs)
129174
}
175+
renderCursor = true
176+
}
130177

178+
if renderCursor {
131179
cmds := make([]tea.Cmd, len(m.inputs))
132180
for i := 0; i <= len(m.inputs)-1; i++ {
133181
if i == m.focusIndex {
@@ -145,6 +193,7 @@ func (m inputsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
145193

146194
return m, tea.Batch(cmds...)
147195
}
196+
148197
case string:
149198
m.title = "✔ Commit Type: " + msg
150199
return m, nil
@@ -155,9 +204,7 @@ func (m inputsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
155204
}
156205

157206
// Handle character input and blinking
158-
cmd := m.updateInputs(msg)
159-
160-
return m, cmd
207+
return m, m.updateInputs(msg)
161208
}
162209

163210
func (m *inputsModel) updateInputs(msg tea.Msg) tea.Cmd {
@@ -209,6 +256,7 @@ func (m inputsModel) View() string {
209256
func newInputsModel() inputsModel {
210257
m := inputsModel{
211258
inputs: make([]inputWithCheck, 4),
259+
//editorInputs: make([]string, 4),
212260
}
213261

214262
for i := range m.inputs {

0 commit comments

Comments
 (0)