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

Faster card creation #130

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft

Conversation

stexus
Copy link

@stexus stexus commented Nov 25, 2024

Hi, I was using my fork of mpvacious for the past year without updates, but when I finally got around to updating it the conflict fixing was a bit of a headache, so I thought I'd see if you would be interested in merging this upstream. It is pretty hacky at the moment but it works. There would have to be some additions for config reading to change keybinds I think. I wanted to see what you thought before making the effort for that as I don't need it for my personal setup haha

I basically wanted a faster way to make basic cards where I don't manually set the start/end times. Normally the default Ctrl+M binding does the job; however, there are cases I want to either use multiple lines (usually 2) and/or update multiple notes at once, say when I encounter two unknown words within the same context or sentence. Mpvacious at the moment has incredible flexibility but it comes at the cost of a lot more keystrokes than I'm used to.

What I added was a "quick card creation menu" where card creation for most subtitled works can be done in two or less keystrokes. It opens with 'g' (by default) and asks for a number. I can enter [0-9] to overwrite the last note with the number of subs chosen. alt+g would let me change the number of cards to update, followed by the number of subs.

This relies on the fact that mpv has actually seen the next subtitle, but that will usually already be the case as the user wouldn't be trying to add lines they haven't seen yet to the card.

I've successfully created plenty of cards with this now and I love having both the quick card creation as well as the advanced menu flexibility when I actually need it, often in non subtitled works or YouTube videos with very strange subtitle timings.

@tatsumoto-ren
Copy link
Member

Hi! Before making a large PR, It would be awesome if you reached out through our community. Thanks for the changes, I'll review them.

helpers.lua Outdated
Comment on lines 36 to 39
this.max_n = function(note_ids, n)
table.sort(note_ids)
return {table.unpack(note_ids, math.max(#note_ids - n + 1, 1), #note_ids)}
end
Copy link
Member

Choose a reason for hiding this comment

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

This name is hard to understand. Apparently, this function should be named something like take_last_n_added_notes.

platform/win.lua Outdated
Comment on lines 46 to 48
table.concat { '@', curl_tmpfile_path }
--table.concat { '@', curl_tmpfile_path }
}
table.insert(args, request_json)
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this change was made by mistake.

Copy link
Author

Choose a reason for hiding this comment

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

Apologies, I don't quite remember the intention of this change either. I'll remove it and verify.

Copy link
Author

Choose a reason for hiding this comment

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

I actually believe this was actually to prevent overwriting due to asynchronous calls? When updating multiple cards, only the last updateNoteFields goes through. I'm not 100% sure on this as I never got to the bottom of it, but I suspect it has something to do with using a temp txt file + mp.command_native_async. When I changed it to include the request_json at runtime (as the nix.lua also does), multiple card updates works again. Is there a reason for using a temporary text file for the curl commands?

Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason for using a temporary text file for the curl commands?

Curl on windows doesn't work reliably when json is passed as a command line argument. Using a temporary file is a widely used solution. We also ran into the issue in the past and had to adopt the fix.

When updating multiple cards, only the last updateNoteFields goes through.

Then we need to use a unique filename every time.

@stexus
Copy link
Author

stexus commented Nov 26, 2024

Hi! Before making a large PR, It would be awesome if you reached out through our community. Thanks for the changes, I'll review them.

Good to know, thanks for the heads up. I didn't see any contact in the mpvacious repo yesterday but I found the telegram on the main site today. I'll try to make an account tonight.

@tatsumoto-ren
Copy link
Member

tatsumoto-ren commented Nov 26, 2024

I can't update notes anymore using the 'm' key. After I select the subtitle lines I want, the 'm' button just doesn't do anything.

screenshot-2024-11-26-03-44-53

In the console it says helpers.lua:38: attempt to call field 'unpack' (a nil value)

helpers.lua Outdated
@@ -33,6 +33,10 @@ this.max_num = function(table)
end
return max
end
this.max_n = function(note_ids, n)
table.sort(note_ids)
return { table.unpack(note_ids, math.max(#note_ids - n + 1, 1), #note_ids) }
Copy link
Member

Choose a reason for hiding this comment

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

Callingtable.unpack causes an error: attempt to call field 'unpack' (a nil value). To fix the error, change table.unpack to this.unpack.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you

@stexus stexus marked this pull request as draft November 26, 2024 04:04
use this.unpack in place of table.unpack

remove extraneous observer

revert win.lua changes
@stexus
Copy link
Author

stexus commented Nov 26, 2024

Unfortunately the multiple card update looks to be broken now. I made a few cards tonight with the regular quick card creation (with just g[0-9]), so that seems to work fine at least. Would you prefer me to close the PR and work on integration or leave it open as draft?

@tatsumoto-ren
Copy link
Member

Would you prefer me to close the PR and work on integration or leave it open as draft?

I think it's better to leave it open for now.

Additionally, the users need to understand how to use the new functionality. So we need to add the appropriate documentation to README.md (or to a new file in howto).

@stexus
Copy link
Author

stexus commented Nov 26, 2024

Got it, I'll add that over this week too. Currently I plan to:

  1. Fix multi card update functionality
  2. Config support to change keybinds
  3. README documentation

@stexus stexus force-pushed the quick-card-creation branch 7 times, most recently from 435009b to b13ae4f Compare November 29, 2024 05:53
@stexus stexus force-pushed the quick-card-creation branch from b13ae4f to 0c31bd1 Compare November 29, 2024 05:55
@stexus stexus marked this pull request as ready for review November 29, 2024 06:07
@stexus
Copy link
Author

stexus commented Nov 29, 2024

@tatsumoto-ren Hi Tatsumoto, can you review the short tutorial I wrote in the README?
Otherwise this is working well on my machine -- I made just shy of 50 cards so it passes the sniff test at least. Please give it a shot too! I don't use the advanced menu that much nowadays so I may have missed some corner case bugs in my testing.

@@ -194,9 +198,23 @@ self.get_timing = function(position)
return -1
end

self.collect = function()
self.collect = function(n_lines)
Copy link
Member

@tatsumoto-ren tatsumoto-ren Nov 29, 2024

Choose a reason for hiding this comment

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

This function now does two different things depending on the argument passed. In such cases it's better to have two functions instead of one.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, good point.

subs2srs.lua Outdated
Comment on lines 52 to 53
local n_lines
local n_cards = 1
Copy link
Member

Choose a reason for hiding this comment

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

These two values are logically tied together, so it would be appropriate to encapsulate them in one object.

@tatsumoto-ren
Copy link
Member

Thanks for making the README.

Otherwise this is working well on my machine -- I made just shy of 50 cards so it passes the sniff test at least.

It doesn't work with secondary subtitles, right?

Please give it a shot too!

I'll test it.

@stexus
Copy link
Author

stexus commented Nov 29, 2024

It doesn't work with secondary subtitles, right?

Yes, I didn't add support for secondary subtitles. Let me know if this is a problem.

@stexus
Copy link
Author

stexus commented Nov 30, 2024

Also, I saw you reverted the request_json change. Were you able to take a look at the earlier comment regarding it?

@tatsumoto-ren
Copy link
Member

Yes, I didn't add support for secondary subtitles. Let me know if this is a problem.

Users will likely find this confusing.

@tatsumoto-ren
Copy link
Member

I wrote a workaround for the async curl calls issue. But I don't use Windows so can't test it.

@stexus
Copy link
Author

stexus commented Nov 30, 2024

Thanks, I'll give it a shot. What's the benefit of using these files for the curl command instead of passing in the request directly in the lua table?

I'll add secondary subtitle support too.

@stexus stexus marked this pull request as draft November 30, 2024 19:52
@tatsumoto-ren
Copy link
Member

What's the benefit of using these files for the curl command instead of passing in the request directly in the lua table?

It's to avoid the issue with failing curl requests on windows.

subs2srs.lua Outdated
Comment on lines 404 to 418
local last_note_ids = ankiconnect.get_last_note_ids(n_cards or 1)
local last_note_ids = ankiconnect.get_last_note_ids(n_cards)
Copy link
Member

Choose a reason for hiding this comment

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

What's the purpose of the or 1 expression?

Copy link
Author

Choose a reason for hiding this comment

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

I don't quite remember why I had that to be honest lol. Likely some sort of safeguard in case n_cards is nil or 0, but using the quick_creation_opts guarantees a valid value now.

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

Successfully merging this pull request may close these issues.

2 participants