55 "strings"
66 "time"
77
8+ "github.com/jesseduffield/gocui"
89 "github.com/jesseduffield/lazygit/pkg/gui/types"
910 "github.com/samber/lo"
1011)
@@ -16,10 +17,11 @@ type ICommitsHelper interface {
1617type CommitsHelper struct {
1718 c * HelperCommon
1819
19- getCommitSummary func () string
20- setCommitSummary func (string )
21- getCommitDescription func () string
22- setCommitDescription func (string )
20+ getCommitSummary func () string
21+ setCommitSummary func (string )
22+ getCommitDescription func () string
23+ getUnwrappedCommitDescription func () string
24+ setCommitDescription func (string )
2325}
2426
2527var _ ICommitsHelper = & CommitsHelper {}
@@ -29,14 +31,16 @@ func NewCommitsHelper(
2931 getCommitSummary func () string ,
3032 setCommitSummary func (string ),
3133 getCommitDescription func () string ,
34+ getUnwrappedCommitDescription func () string ,
3235 setCommitDescription func (string ),
3336) * CommitsHelper {
3437 return & CommitsHelper {
35- c : c ,
36- getCommitSummary : getCommitSummary ,
37- setCommitSummary : setCommitSummary ,
38- getCommitDescription : getCommitDescription ,
39- setCommitDescription : setCommitDescription ,
38+ c : c ,
39+ getCommitSummary : getCommitSummary ,
40+ setCommitSummary : setCommitSummary ,
41+ getCommitDescription : getCommitDescription ,
42+ getUnwrappedCommitDescription : getUnwrappedCommitDescription ,
43+ setCommitDescription : setCommitDescription ,
4044 }
4145}
4246
@@ -53,11 +57,36 @@ func (self *CommitsHelper) SetMessageAndDescriptionInView(message string) {
5357 self .c .Contexts ().CommitMessage .RenderCommitLength ()
5458}
5559
56- func (self * CommitsHelper ) JoinCommitMessageAndDescription () string {
57- if len (self .getCommitDescription ()) == 0 {
60+ func (self * CommitsHelper ) JoinCommitMessageAndUnwrappedDescription () string {
61+ if len (self .getUnwrappedCommitDescription ()) == 0 {
5862 return self .getCommitSummary ()
5963 }
60- return self .getCommitSummary () + "\n " + self .getCommitDescription ()
64+ return self .getCommitSummary () + "\n " + self .getUnwrappedCommitDescription ()
65+ }
66+
67+ func TryRemoveHardLineBreaks (message string , autoWrapWidth int ) string {
68+ messageRunes := []rune (message )
69+ lastHardLineStart := 0
70+ for i , r := range messageRunes {
71+ if r == '\n' {
72+ // Try to make this a soft linebreak by turning it into a space, and
73+ // checking whether it still wraps to the same result then.
74+ messageRunes [i ] = ' '
75+
76+ _ , cursorMapping := gocui .AutoWrapContent (messageRunes [lastHardLineStart :], autoWrapWidth )
77+
78+ // Look at the cursorMapping to check whether auto-wrapping inserted
79+ // a line break. If it did, there will be a cursorMapping entry with
80+ // Orig pointing to the position after the inserted line break.
81+ if len (cursorMapping ) == 0 || cursorMapping [0 ].Orig != i - lastHardLineStart + 1 {
82+ // It didn't, so change it back to a newline
83+ messageRunes [i ] = '\n'
84+ }
85+ lastHardLineStart = i + 1
86+ }
87+ }
88+
89+ return string (messageRunes )
6190}
6291
6392func (self * CommitsHelper ) SwitchToEditor () error {
@@ -154,7 +183,7 @@ func (self *CommitsHelper) HandleCommitConfirm() error {
154183
155184func (self * CommitsHelper ) CloseCommitMessagePanel () error {
156185 if self .c .Contexts ().CommitMessage .GetPreserveMessage () {
157- message := self .JoinCommitMessageAndDescription ()
186+ message := self .JoinCommitMessageAndUnwrappedDescription ()
158187
159188 self .c .Contexts ().CommitMessage .SetPreservedMessage (message )
160189 } else {
0 commit comments