Skip to content

Commit

Permalink
If there's only a single search result, copy it
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 2, 2022
1 parent 358edcf commit dbe6b65
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ $ em -s yellow

<!-- [[[end]]] -->

If there's only a single search result, it's copied:

<!-- [[[cog run("em -s ukraine") ]]] -->

```console
$ em -s ukraine
Copied! 🇺🇦 flag_ukraine
```

<!-- [[[end]]] -->

## Installation

At this time, **em** requires Python and pip:
Expand Down
8 changes: 7 additions & 1 deletion em/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ def cli():
for (name, emoji) in found:
# Some registered emoji have no value.
try:
print(f"{emoji} {name}")
# Copy the results (and say so!) to the clipboard.
if copier and not no_copy and len(found) == 1:
copier.copy(emoji)
print(f"Copied! {emoji} {name}")
else:
print(f"{emoji} {name}")

# Sometimes, an emoji will have no value.
except TypeError:
pass
Expand Down
21 changes: 21 additions & 0 deletions tests/test_em.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ def test_search_star(mock_print, mock_exit, mock_argparse):
mock_exit.assert_called_with(0)


@patch("em.argparse.ArgumentParser.parse_args")
@patch("builtins.print")
def test_search_single_result_is_copied(mock_print, mock_argparse):
# Arrange
mock_argparse.return_value = argparse.Namespace(
name=["ukraine"], no_copy=None, search=True
)

# Act
with pytest.raises(SystemExit) as e:
cli()

# Assert
if copier:
mock_print.assert_called_once_with("Copied! 🇺🇦 flag_ukraine")
else:
mock_print.assert_called_once_with("🇺🇦 flag_ukraine")
assert e.type == SystemExit
assert e.value.code == 0


@patch("em.argparse.ArgumentParser.parse_args")
@patch("em.sys.exit")
@patch("builtins.print")
Expand Down
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ envlist =
extras =
tests
commands =
{envpython} -m pytest --cov em --cov tests --cov-report term {posargs}
{envpython} -m coverage xml
{envpython} -m pytest --cov em --cov tests --cov-report html --cov-report term --cov-report xml {posargs}

[testenv:cli]
commands =
Expand Down

0 comments on commit dbe6b65

Please sign in to comment.