Skip to content

Commit

Permalink
Split recents by fileset
Browse files Browse the repository at this point in the history
So far, the recently chosen characters were shared among all instances
of `rofimoji`, no matter which files were actually shown.

Now, we show and save them per fileset, which is an improvement.
However, if one file is used in multiple filesets, their usage is not
shared, but separate. A solution for this seems to complicated for what
we try to achieve here.

The old `recents` file will be migrated on startup, if necessary.

Many thanks to @will-lynas for reporting this and working with me to
find a good solution.

Issue: #173
  • Loading branch information
fdw committed Mar 24, 2024
1 parent a6fe835 commit 5674e03
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# [NEXT]
## Added
- Support for [tofi](https://github.com/philj56/tofi/), [bemenu](https://github.com/Cloudef/bemenu) and [wmenu](https://git.sr.ht/~adnano/wmenu) as selectors

## Changed
- Only show recent characters from the same fileset. (#173)
- Updates to Gitmoji and FontAwesome.

## Added
- Support for [tofi](https://github.com/philj56/tofi/), [bemenu](https://github.com/Cloudef/bemenu) and [wmenu](https://git.sr.ht/~adnano/wmenu) as selectors

# [6.2.0]
## Added
- Support dmenu as selector.
Expand Down
8 changes: 6 additions & 2 deletions src/picker/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ def __parse_args(self) -> None:
self.clipboarder = Clipboarder.best_option(self.args.clipboarder)

def show_characters(self, state: State) -> None:
recent_characters = self.__format_recent_characters(load_recent_characters(self.args.max_recent))
recent_characters = self.__format_recent_characters(
load_recent_characters(self.args.max_recent, self.args.files)
)

state.output = "\x00markup-rows\x1ftrue\n"
state.output += "\x00use-hot-keys\x1ftrue\n"
Expand Down Expand Up @@ -149,7 +151,9 @@ def __format_characters(self, characters: Dict[str, str]) -> List[str]:

def handle_shortcuts(self, state: State) -> None:
if 10 <= state.return_code <= 19:
state.processed_characters = load_recent_characters(self.args.max_recent)[state.return_code - 10]
state.processed_characters = load_recent_characters(self.args.max_recent, self.args.files)[
state.return_code - 10
]
state.reset_current_input()
state.step += 2
return
Expand Down
19 changes: 14 additions & 5 deletions src/picker/recent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
from .paths import *


def load_recent_characters(max_recent: int) -> List[str]:
def load_recent_characters(max_recent: int, files: List[str]) -> List[str]:
try:
return [char.strip("\n") for char in recents_file_location.read_text().strip("\n").split("\n")][:max_recent]
return [char.strip("\n") for char in __filename_for(files).read_text().strip("\n").split("\n")][:max_recent]
except FileNotFoundError:
return []
except NotADirectoryError:
recents = [char.strip("\n") for char in recents_file_location.read_text().strip("\n").split("\n")][:max_recent]
recents_file_location.unlink()
save_recent_characters("".join(recents), max_recent, files)
return recents


def save_recent_characters(new_characters: str, max_recent: int) -> None:
def save_recent_characters(new_characters: str, max_recent: int, files: List[str]) -> None:
if max_recent == 0:
return
max_recent = min(max_recent, 10)

old_file_name = recents_file_location
new_file_name = old_file_name.with_name("recent.tmp")
old_file_name = __filename_for(files)
new_file_name = old_file_name.with_suffix(".tmp")

new_file_name.parent.mkdir(parents=True, exist_ok=True)
with new_file_name.open("w+") as new_file:
Expand All @@ -37,3 +42,7 @@ def save_recent_characters(new_characters: str, max_recent: int) -> None:
pass

new_file_name.rename(old_file_name)


def __filename_for(files: List[str]) -> Path:
return recents_file_location / "-".join(files)
8 changes: 4 additions & 4 deletions src/picker/standalone.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from typing import Dict, List, Tuple, Union
from typing import List, Tuple, Union

from . import emoji_data
from .action import execute_action
Expand Down Expand Up @@ -30,22 +30,22 @@ def standalone(self) -> None:
self.args.actions = [action]

if isinstance(value, Shortcut):
characters = load_recent_characters(self.args.max_recent)[value.index]
characters = load_recent_characters(self.args.max_recent, self.args.files)[value.index]
else:
characters = self.__process_chosen_characters(value)

if Action.MENU in self.args.actions:
self.args.actions = self.selector.show_action_menu(self.args.selector_args)

save_recent_characters(characters, self.args.max_recent)
save_recent_characters(characters, self.args.max_recent, self.args.files)
execute_action(characters, self.args.actions, self.active_window, self.args.typer, self.args.clipboarder)

def __open_main_selector_window(self) -> Tuple[Union[Action, DEFAULT, CANCEL], Union[List[str], Shortcut]]:
return self.selector.show_character_selection(
read_characters_from_files(
self.args.files, load_frecent_characters() if self.args.frecency else [], self.args.use_additional
),
load_recent_characters(self.args.max_recent),
load_recent_characters(self.args.max_recent, self.args.files),
self.args.prompt,
self.args.show_description,
self.args.use_icons,
Expand Down

0 comments on commit 5674e03

Please sign in to comment.