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

Replace to tcell from termbox #32

Merged
merged 26 commits into from
Dec 25, 2020
Merged

Conversation

tjmtmmnk
Copy link
Contributor

@tjmtmmnk tjmtmmnk commented Dec 1, 2020

Related Issue: #15

Thanks great library!
This PR is intended to replace completely from termbox to tcell.

I think this migration has two merits.

  1. It is stated that termbox will not be maintained anymore (ref). So, migrate to maintained tcell, make this product more stable.

  2. tcell has a lot of useful features. For example, Ansi color in preview mode #29 can solve easily by using code used in tview etc (ref).


The problem reported in #17, can solve by using workaround(ref).

I could confirm the code(reproduce the problem) work well in local.

@codecov
Copy link

codecov bot commented Dec 1, 2020

Codecov Report

Merging #32 (159aa65) into master (6cc406b) will decrease coverage by 13.15%.
The diff coverage is 84.30%.

@@             Coverage Diff             @@
##           master      #32       +/-   ##
===========================================
- Coverage   89.61%   76.45%   -13.16%     
===========================================
  Files           6        6               
  Lines         597      756      +159     
===========================================
+ Hits          535      578       +43     
- Misses         44      159      +115     
- Partials       18       19        +1     

@ktr0731 ktr0731 self-requested a review December 2, 2020 04:48
@ktr0731 ktr0731 added the enhancement New feature or request label Dec 2, 2020
@ktr0731
Copy link
Owner

ktr0731 commented Dec 2, 2020

Hi, @tjmtmmnk. Thanks a lot for your great work!
I'll review this Pull Request later, so please take a little...

Copy link
Owner

@ktr0731 ktr0731 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! I commented on some points.

@@ -30,7 +30,7 @@ jobs:
run: go build

- name: Test
run: go test -v -coverpkg ./... -covermode atomic -coverprofile coverage.txt -tags fuzz -numCases 3000 -numEvents 300 ./...
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not a part of the changes PR's title describes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change related to #32 (comment)


// SetSize changes the pseudo-size of the window.
// Note that SetSize resets added cells.
func (m *TerminalMock) SetSize(w, h int) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change breaks TerminalMock's backward-compatibility. Could you define TerminalMockV2 instead of modifying TerminalMock?
Or, we may be able to put a new method SetTcellEvents into TerminalMock without breaking the current interface (maybe this is better if possible).

Copy link
Contributor Author

@tjmtmmnk tjmtmmnk Dec 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. I will define TerminalMockV2.
It wasn't such a simple problem, so I'll think about it for a moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me TerminalMock seemed to be used only for testing purposes.
I didn't understand why backwards compatibility was needed so please let me know...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is used only for testing. However, according to the import compatibility rule of Go modules, public interfaces should be compatible, regardless of whether they are for testing or not.

Please let me know if there seems to be no way to keep backward compatibility; I will consider making a v2 release...

Copy link
Contributor Author

@tjmtmmnk tjmtmmnk Dec 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for introducing useful article!
I tried to make the public interface backwards compatible.

mock.go Show resolved Hide resolved
tcell.go Outdated
Comment on lines 11 to 13
type termImpl struct {
screen tcell.Screen
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's clearer to simply use embedding.

Suggested change
type termImpl struct {
screen tcell.Screen
}
type termImpl struct {
tcell.Screen
}

@@ -553,7 +618,6 @@ func (f *finder) find(slice interface{}, itemFunc func(i int) string, opts []Opt
if err := f.initFinder(items, matched, opt); err != nil {
return nil, errors.Wrap(err, "failed to initialize the fuzzy finder")
}
defer f.term.close()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call f.term.Screen().Fini() at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I call Fini() here and here

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I misunderstood...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, is there any reason to call f.term.Fini() in Find and FindMulti instead of find? I thought it would work fine if we left it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was misunderstanding, as you say, I can call Fini() in find

fuzzyfinder.go Outdated
func sendExtraEventFix() {
kb, err := keybd_event.NewKeyBonding()
if err != nil {
panic(err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use panic, return the error instead.

fuzzyfinder.go Outdated
@@ -604,6 +672,20 @@ func (f *finder) find(slice interface{}, itemFunc func(i int) string, opts []Opt
}
}

// This method avoid tcell bug https://github.com/gdamore/tcell/issues/194
// Aditional EOL event is sent to ensure, consequent events, are correctly handled
func sendExtraEventFix() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great hack 👍

fuzzyfinder.go Outdated Show resolved Hide resolved
fuzzing_test.go Outdated Show resolved Hide resolved
fuzzing_test.go Outdated Show resolved Hide resolved
@tjmtmmnk
Copy link
Contributor Author

  • tried to make the public interface backwards compatible by creating v2
  • embedded struct implementing Screen or SimulationScreen interface
  • call Fini in find

I fixed 3 points.
Please re-review when you have a time🙏

Copy link
Owner

@ktr0731 ktr0731 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tjmtmmnk
I'm really sorry I'm always late...

It's almost perfect, but I'd like you to fix a few minor things before we merge it 🙏

helper_test.go Outdated
@@ -11,3 +11,11 @@ func NewWithMockedTerminal() (*finder, *TerminalMock) {
m.SetSize(w, h)
return f, m
}

func NewWithMockedTerminalV2() (*finder, *TerminalMock) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to define a v2 function because helper_test.go is only for testing and it is referenced only from fuzzyfinder_test.go. So you can rename it to NewWithMockedTerminal and remove the old one.

Suggested change
func NewWithMockedTerminalV2() (*finder, *TerminalMock) {
func NewWithMockedTerminal() (*finder, *TerminalMock) {

mock.go Outdated
func (m *TerminalMock) close() {}

// UseMockedTerminal switches the terminal, which is used from
// this package to a mocked one.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment for deprecation? (ref. https://github.com/golang/go/wiki/Deprecated)

Suggested change
// this package to a mocked one.
// this package to a mocked one.
//
// Deprecated: Use UseMockedTerminalV2.

mock.go Show resolved Hide resolved
mock.go Outdated
return buf.String()
}

// parseAttrV2 parses color and attribute for test
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[super nits]

Suggested change
// parseAttrV2 parses color and attribute for test
// parseAttrV2 parses color and attribute for testing.

mock.go Outdated
@@ -45,12 +49,30 @@ func (m *TerminalMock) SetSize(w, h int) {
m.cells = make([]*cell, w*h)
}

func (m *TerminalMock) SetSizeV2(w, h int) {
Copy link
Owner

@ktr0731 ktr0731 Dec 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you switch the implementation of SetSize based on a bool field such as m.v2 instead of defining SetSizeV2? (It looks like the variable v2 can be set inside of UseTerminalMockV2)

For example:

// SetSize changes the pseudo-size of the window.
// Note that SetSize resets added cells.
func (m *TerminalMock) SetSize(w, h int) {
	if m.v2 {
		m.simScreen.SetSize(w, h)
		return
	}

	m.sizeMu.Lock()
	defer m.sizeMu.Unlock()
	m.cellsMu.Lock()
	defer m.cellsMu.Unlock()
	m.width = w
	m.height = h
	m.cells = make([]*cell, w*h)
}

// SetEvents sets all events, which are fetched by pollEvent.
// A user of this must set the EscKey event at the end.
func (m *TerminalMock) SetEvents(e ...termbox.Event) {
func (m *TerminalMock) SetEvents(events ...termbox.Event) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

mock.go Show resolved Hide resolved
mock.go Show resolved Hide resolved
mock.go Outdated
@@ -61,6 +83,63 @@ func (m *TerminalMock) GetResult() string {
return m.result
}

func (m *TerminalMock) GetResultV2() string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the review comment on SetSizeV2.

@tjmtmmnk
Copy link
Contributor Author

Sorry for the delay, and thank you for review!
I fixed problems you indicated, please check when you have a time🙏

Copy link
Owner

@ktr0731 ktr0731 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
Thanks for your contribution. I'm sure that this change will be very helpful for go-fuzzyfinder's future.
Have a happy new year holiday 🍺

@ktr0731 ktr0731 merged commit 821eba4 into ktr0731:master Dec 25, 2020
@tjmtmmnk
Copy link
Contributor Author

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants