Skip to content

Commit

Permalink
Merge pull request #84 from scalabli/_quo
Browse files Browse the repository at this point in the history
under the hood optimizations
  • Loading branch information
quantum-quirks authored May 1, 2022
2 parents 8208c67 + ceb080e commit 8187788
Show file tree
Hide file tree
Showing 41 changed files with 217 additions and 169 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,15 @@ Press [Tab] to autocomplete
Auto suggestion is a way to propose some input completions to the user. Usually, the input is compared to the history and when there is another entry starting with the given text, the completion will be shown as gray text behind the current input. Pressing the right arrow → or ctrl-e will insert this suggestion, alt-f willinsert the first word of the suggestion.
```python

from quo.history import MemoryHistory
from quo.prompt import Prompt
from quo.completion import AutoSuggestFromHistory
from quo.history import InMemoryHistory

history = InMemoryHistory()
history.append("import os")
history.append('print("hello")')
history.append('print("world")')
history.append("import path")
MemoryHistory.append("import os")
MemoryHistory.append('print("hello")')
MemoryHistory.append('print("world")')
MemoryHistory.append("import path")

session = Prompt(auto_suggest=AutoSuggestFromHistory(), history=history)
session = Prompt(history=MemoryHistory, suggest="history")

while True:
session.prompt('> ')
Expand Down Expand Up @@ -240,7 +238,7 @@ A key binding is an association between a physical key on a keyboard and a param
def _(event):
echo("Hello, World!")

session.prompt(">>")
session.prompt(">> ")
```
Read more on [Key bindings](https://quo.readthedocs.io/en/latest/kb.html)

Expand Down
13 changes: 12 additions & 1 deletion docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ Changelog

.. image:: https://media.giphy.com/media/12oufCB0MyZ1Go/giphy.gif

``Version 2022.4.5``
``Version 2022.5``
--------------------

Released on 2022-05-01

**Added**
^^^^^^^^^^
- Added :meth:`quo.console.Console.spin`
- Added :param:`column_width` and :param:`headers` to :func:`quo.table.Table`
- Added :param:`suggest` to :class:`quo.prompt.Prompt`

`Version 2022.4.5``
---------------------

Released on 2022-04-23
Expand Down
17 changes: 17 additions & 0 deletions docs/console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,22 @@ The :meth:`~quo.console.Console.bar` method will draw a horizontal bar with an o
- ``color`` - controls if the pager supports ANSI colors or not.


``spin``
----------
This creates a context manager that is used to display a spinner on stdout as long as the context has not exited.
*Added on v2022.5*

.. code:: python
import time
from quo.console import Console
console = Console()
with console.spin():
time.sleep(3)
print("Hello, World")
» Check out more examples `here <https://github.com/scalabli/quo
/tree/master/examples/console/>`_
2 changes: 1 addition & 1 deletion docs/full_screen_apps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Print the layout to the output
**Parameters**
- ``container`` - AnyContaine
- ``bind`` *(bool)* - When True, initiate a :class:`~quo.keys.Bind` instance for the key bindings.
- ``full_screen`` - When True, run the application on the alternate screen buffer.
- ``full_screen`` *(bool)* - When True, run the application on the alternate screen buffer.
- ``focused_element`` - element to be focused initially. *(Can be anything the `focus` function accepts.)*
- ``mouse_support`` - :class:`~quo.filters.Filter` or boolean. When True, enable mouse support.
- ``style`` - A style string.
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added docs/images/oldpics/two_tasks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/two_tasks.png
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 9 additions & 10 deletions docs/prompt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ Auto suggestion is a way to propose some input completions to the user like the

Usually, the input is compared to the history and when there is another entry starting with the given text, the completion will be shown as gray text behind the current input. Pressing the right arrow :kbd:`` or :kbd:`ctrl-e` will insert this suggestion, :kbd:`alt-f` will insert the first word of the suggestion.

*Added :param:`suggest` on v2022.5*

.. note::

When suggestions are based on the history, don't forget to share one :class:`~quo.history.History` object between consecutive prompt calls. Using a :class:`~quo.prompt.Prompt`
Expand All @@ -472,23 +474,20 @@ Example:
.. code:: python
from quo.prompt import Prompt
from quo.completion import AutoSuggestFromHistory
from quo.history import InMemoryHistory
history = InMemoryHistory()
history.append("import os")
history.append('print("hello")')
history.append('print("world")')
history.append("import path")
from quo.history import MemoryHistory
session = Prompt(auto_suggest=AutoSuggestFromHistory(), history=history)
MemoryHistory.append("import os")
MemoryHistory.append('print("hello")')
MemoryHistory.append('print("world")')
MemoryHistory.append("import path")
session = Prompt(history=MemoryHistory, suggest="history")
while True:
text = session.prompt('> ')
print(f"You said: {text}")
.. image:: ./images/auto-suggestion.png

A suggestion does not have to come from the history. Any implementation of the :class:`~quo.completion.AutoSuggest` abstract base class can be passed as an argument.
A suggestion does not have to come from the history. Any implementation of the :class:`~quo.completion.AutoSuggest` abstract base class can be passed as a string i.e `history`, `dynamic` or `conditional`

Autocompletion
^^^^^^^^^^^^^^^
Expand Down
33 changes: 32 additions & 1 deletion docs/table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Table
**Parameters**

- ``data`` - The first required argument. Can be a list-of-lists *(or another iterable of iterables)*, a list of named tuples, a dictionary of iterables, an iterable of dictionaries, a two-dimensional NumPy array, NumPy record array, or a Pandas' dataframe.
- ``align`` - :class:`.WindowAlign` value or callable that return an :class:`.WindowAlign` value. alignment of content. i.e ``left``, ``centre`` or ``right``. ``centre`` is the default value.
- ``style`` - A style string.

- ``theme`` - **plain** - Separates columns with a double space.
- **simple** - like Pandoc simple_tables.
Expand All @@ -20,7 +22,6 @@ Table
- **mediawiki** - Produces a table markup used in Wikipedia and on other MediaWiki-based sites.
- **rst** - Like a simple table format from reStructuredText.

- ``style`` - A style string.

Changed on *v2022.4.3*

Expand All @@ -38,3 +39,33 @@ Changed on *v2022.4.3*
Table(data)
.. image:: https://raw.githubusercontent.com/scalabli/quo/master/docs/images/table.png

``Table headers``
------------------

To print nice column headers, supply the ``headers`` argument.

- `headers` can be an explicit list of column headers.
- if `headers="firstrow"`, then the first row of data is used
- if `headers="keys"`, then dictionary keys or column indices are used otherwise a headerless table is produced.

.. code:: python
data = [
["sex","age"]
["Alice","F",24]
["Bob","M",19]
]
Table(data, headers="firstrow")
``Column Widths and Line Wrapping``
--------------------------------------
:func:`Table` will, by default, set the width of each column to the length of the longest element in that column. However, in situations where fields are expected to reasonably be too long to look good as a single line, :param:`:param:`column_width` can help automate word wrapping long fields.

.. code:: python
data = [
[1, 'John Smith', 'This is a rather long description that might look better if it is wrapped a bit']
]
Table(data, headers=("Issue Id", "Author", "Description"), column_width=[None, None, 30])
6 changes: 3 additions & 3 deletions docs/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ is instead a pipe.

.. code:: python
from quo import echo, getchar
from quo import getchar
gc = getchar()
if gc == 'y':
echo('We will go on')
print('We will go on')
elif gc == 'n':
echo('Abort!')
print('Abort!')
Note that this reads raw input, which means that things like arrow keys
Expand Down
8 changes: 8 additions & 0 deletions examples/console/spin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from quo.console import Console

console = Console()

with console.spin():
import time
time.sleep(2)
print("Hello, world")
4 changes: 1 addition & 3 deletions examples/dialogs/inputbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

result = InputBox(
title="PromptBox shenanigans",
text="What Country are you from?:",
multiline=True,
bg=False)
text="What Country are you from?:",)

print(f"Result = {result}")

15 changes: 15 additions & 0 deletions examples/dialogs/multiline-inputbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
"""
Example of an input box dialog.
"""
from quo import print
from quo.dialog import InputBox

result = InputBox(
title="PromptBox shenanigans",
text="What Country are you from?:",
multiline=True,
bg=False)

print(f"Result = {result}")

24 changes: 11 additions & 13 deletions examples/prompts/auto-suggestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@
suggestion.
"""

from quo import echo
from quo.completion import AutoSuggestFromHistory
from quo.history import InMemoryHistory
from quo.history import MemoryHistory
from quo.prompt import Prompt

def main():
# Create some history first. (Easy for testing.)
history = InMemoryHistory()
history.append("import os")
history.append('print("hello")')
history.append('print("world")')
history.append("import path")
MemoryHistory.append("import os")
MemoryHistory.append('print("hello")')
MemoryHistory.append('print("world")')
MemoryHistory.append("import path")

# Print help.
print("This CLI has fish-style auto-suggestion enable.")
Expand All @@ -29,10 +26,11 @@ def main():
print()

session = Prompt(
history=history,
auto_suggest=AutoSuggestFromHistory(),
enable_history_search=True,
)
history=MemoryHistory,
suggest="history"
)
# auto_suggest=AutoSuggestFromHistory()
#)

while True:
try:
Expand All @@ -42,7 +40,7 @@ def main():
else:
break

echo(f"You said: {text}")
print(f"You said: {text}")


if __name__ == "__main__":
Expand Down
10 changes: 10 additions & 0 deletions examples/table/colored-table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from quo.table import Table

data = [
["Name", "Gender", "Age"],
["Alice", "F", 24],
["Bob", "M", 19],
["Dave", "M", 24]
]

Table(data, style="green")
8 changes: 8 additions & 0 deletions examples/table/headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from quo.table import Table

data = [
[1, 'John Smith', 'This is a rather long description that might look better if it is wrapped a bit']
]


Table(data, headers=("Issue Id", "Author", "Description"), column_width=[None, None, 30])
10 changes: 10 additions & 0 deletions examples/table/right-aligned.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from quo.table import Table

data = [
["Name", "Gender", "Age"],
["Alice", "F", 24],
["Bob", "M", 19],
["Dave", "M", 24]
]

Table(data, align="right")
2 changes: 1 addition & 1 deletion examples/table/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
["Dave", "M", 24]
]

Table(data, headers="secondrow")
Table(data)
2 changes: 1 addition & 1 deletion src/quo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ def print(*values, style=None, sep=" ", end="\n"):

from quo.shortcuts.utils import container

__version__ = "2022.4.6"
__version__ = "2022.5"
2 changes: 1 addition & 1 deletion src/quo/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from quo.console.current import get_app
from quo.console.run_in_terminal import run_in_terminal
from quo.completion.auto_suggest import AutoSuggest, Suggestion
from quo.cache import FastDictCache
from quo.cache.core import FastDictCache
from quo.clipboard import Data
from quo.completion.core import (
CompleteEvent,
Expand Down
4 changes: 2 additions & 2 deletions src/quo/cache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .core import SimpleCache, FastDictCache, memoized
from ._lru_cache import LRUCache
#from .core import SimpleCache, FastDictCache, memoized
#from ._lru_cache import LRUCache
2 changes: 1 addition & 1 deletion src/quo/completion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# CompleteStyle,
Completer,
Completion,
# ConditionalCompleter,
ConditionalCompleter,
# DummyCompleter,
DynamicCompleter,
ThreadedCompleter,
Expand Down
2 changes: 1 addition & 1 deletion src/quo/completion/auto_suggest.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def get_suggestion(

class ConditionalAutoSuggest(AutoSuggest):
"""
Auto suggest that can be turned on and of according to a certain condition.
Auto suggest that can be turned on and off according to a certain condition.
"""

def __init__(self, auto_suggest: AutoSuggest, filter: Union[bool, Filter]) -> None:
Expand Down
5 changes: 4 additions & 1 deletion src/quo/console/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from quo.buffer import Buffer
from quo.i_o.termui import echo
from quo import errors
from quo.cache import SimpleCache
from quo.cache.core import SimpleCache
from quo.clipboard import Clipboard, InMemoryClipboard
from quo.enums import EditingMode
from quo.eventloop import (
Expand Down Expand Up @@ -911,6 +911,9 @@ def bar(
return container(
Window(FormattedTextControl(message), height=1, style=style, align=align)
)
def spin(self) -> "Console":
from quo.spin import Spinner
return Spinner()


def run(
Expand Down
3 changes: 0 additions & 3 deletions src/quo/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,6 @@ def button_handler(v: _T) -> None:
return _create_app(dialog, style)


from quo.widget.design import ok


def _PromptBox(
title: AnyFormattedText = "",
text: AnyFormattedText = "",
Expand Down
2 changes: 1 addition & 1 deletion src/quo/filters/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TYPE_CHECKING, cast

from quo.console.current import get_app
from quo.cache import memoized
from quo.cache.core import memoized
from quo.enums import EditingMode

from .core import Condition
Expand Down
Loading

0 comments on commit 8187788

Please sign in to comment.