Skip to content

Commit

Permalink
tree-pick is pickpack
Browse files Browse the repository at this point in the history
  • Loading branch information
anafvana committed Jul 12, 2022
1 parent 67c9c81 commit 1259679
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ test:

.PHONY=clean
clean:
rm -fr build dist tree_pick.egg-info
rm -fr build dist pickpack.egg-info
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# tree_pick
# pickpack

[![ci](https://github.com/anafvana/tree_pick/actions/workflows/ci.yml/badge.svg?event=status)](https://github.com/anafvana/tree_pick/actions/workflows/ci.yml)
[![ci](https://github.com/anafvana/pickpack/actions/workflows/ci.yml/badge.svg)](https://github.com/anafvana/pickpack/actions/workflows/ci.yml)

**tree_pick** is a small python library based on [wong2's pick](https://github.com/wong2/pick) which allows you to create a curses-based interactive selection tree in the terminal.
**pickpack** is a small python library based on [wong2's pick](https://github.com/wong2/pick) which allows you to create a curses-based interactive selection tree in the terminal.

![Demo](https://github.com/anafvana/pick/raw/master/example/basic-tree.gif)

It was made with installation processes in mind, so that a user can select a parent node and get all children elements included. Different configurations allow for different outputs.

## Installation

$ pip install tree-pick
$ pip install pickpack

## Options

Expand All @@ -28,10 +28,10 @@ It was made with installation processes in mind, so that a user can select a par

## Usage

**tree_pick** can be used by creating a tree and passing it into tree_pick:
**pickpack** can be used by creating a tree and passing it into pickpack:

from anytree import Node, RenderTree
from tree_pick import tree_pick
from pickpack import pickpack

title = 'Please choose one: '

Expand All @@ -40,18 +40,18 @@ It was made with installation processes in mind, so that a user can select a par
p1 = Node('parent', children=[c1,c2])

options = RenderTree(p1)
option, index = tree_pick(options, title)
option, index = pickpack(options, title)
print(option, index)

**outputs**:

Node('/parent/child1', index=1)
1

**tree_pick** multiselect example returning node-name and index:
**pickpack** multiselect example returning node-name and index:

from anytree import Node, RenderTree
from tree_pick import tree_pick
from pickpack import pickpack

title = 'Please choose one: '

Expand All @@ -60,7 +60,7 @@ It was made with installation processes in mind, so that a user can select a par
p1 = Node('parent', children=[c1,c2])

options = RenderTree(p1)
option, index = tree_pick(options, title, multiselect=True, min_selection_count=1, output_format='nameindex')
option, index = pickpack(options, title, multiselect=True, min_selection_count=1, output_format='nameindex')
print(option, index)

**outputs**::
Expand All @@ -72,15 +72,15 @@ It was made with installation processes in mind, so that a user can select a par
To register custom handlers for specific keyboard keypresses, you can use the `register_custom_handler` property:

from anytree import Node, RenderTree
from tree_pick import TreePicker
from pickpack import PickPacker

title = 'Please choose one: '
c1 = Node('child1')
c2 = Node('child2')
p1 = Node('parent', children=[c1,c2])
options = RenderTree(p1)

picker = TreePicker(options, title)
picker = PickPacker(options, title)
def go_back(picker):
return None, -1
picker.register_custom_handler(ord('h'), go_back)
Expand All @@ -100,10 +100,10 @@ The function must take in elements of the type you passed into the options (`Nod

You may also store any additional information as a custom property within the node.

**tree_pick** options map function example:
**pickpack** options map function example:

from anytree import Node, RenderTree
from tree_pick import tree_pick
from pickpack import pickpack

title = 'Please choose an option: '
options = [
Expand All @@ -115,7 +115,7 @@ You may also store any additional information as a custom property within the no
def get_node(option):
return Node(option.get('label'), abbreviation=option.get('abbreviation'))

picker = TreePicker(options, title, indicator='*', options_map_func=get_node, output_format='nameindex')
picker = PickPacker(options, title, indicator='*', options_map_func=get_node, output_format='nameindex')

**displays**:

Expand All @@ -132,10 +132,10 @@ You may also store any additional information as a custom property within the no

#### Map function for nested lists

**tree_pick** options map function example for lists with **nesting**:
**pickpack** options map function example for lists with **nesting**:

from anytree import Node, RenderTree
from tree_pick import tree_pick
from pickpack import pickpack

title = 'Please choose an option: '
options = [
Expand All @@ -156,7 +156,7 @@ You may also store any additional information as a custom property within the no
else:
return Node(option.get('label'), children=None, abbreviation=option.get('abbreviation'))

picker = TreePicker(options, title, indicator='*', options_map_func=get_node, output_format='nameindex')
picker = PickPacker(options, title, indicator='*', options_map_func=get_node, output_format='nameindex')

**displays**:

Expand Down
File renamed without changes
4 changes: 2 additions & 2 deletions example/basic-tree.py → example/basic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from anytree import Node, RenderTree
from tree_pick import tree_pick
from pickpack import pickpack

title = 'Please choose your favorite programming language: '
java = Node("Java")
Expand All @@ -18,5 +18,5 @@
root = Node("Select all", children=[fun, imp, oop, mul])

options = RenderTree(root)
option, index = tree_pick(options, title, indicator='=>', default_index=2)
option, index = pickpack(options, title, indicator='=>', default_index=2)
print(option, index)
4 changes: 2 additions & 2 deletions example/custom-tree.py → example/custom.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import curses

from anytree import Node, RenderTree
from tree_pick import TreePicker
from pickpack import PickPacker


def go_back(picker):
Expand All @@ -25,7 +25,7 @@ def go_back(picker):

options = RenderTree(root)

picker = TreePicker(options, title)
picker = PickPacker(options, title)
picker.register_custom_handler(curses.KEY_LEFT, go_back)
option, index = picker.start()
print(option, index)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from anytree import Node, RenderTree
from tree_pick import tree_pick
from pickpack import pickpack

title = 'Please choose your favorite fruit: '
options = [
Expand All @@ -15,5 +15,5 @@ def get_description_for_display(option):
# format the option data for display
return Node('{0} (grow on {1})'.format(option.get('name'), option.get('grow_on')))

option, index = tree_pick(options, title, indicator='=>', options_map_func=get_description_for_display)
option, index = pickpack(options, title, indicator='=>', options_map_func=get_description_for_display)
print(option, index)
4 changes: 2 additions & 2 deletions example/scroll-tree.py → example/scroll.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from anytree import Node, RenderTree
from tree_pick import tree_pick
from pickpack import pickpack

title = 'Select:'
children = [Node('foo.bar%s.baz'%x, index=x) for x in range(1, 71)]
root = Node("All", children=children)

options = RenderTree(root)
option, index = tree_pick(options, title, indicator='=>', default_index=2)
option, index = pickpack(options, title, indicator='=>', default_index=2)
print(option, index)
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[tool.poetry]
name = "tree_pick"
name = "pickpack"
version = "1.0.0"
description = "Based on wong2's pick, tree_pick is a terminal GUI which allows you to pick one or more options from a tree structure"
description = "Based on wong2's pick, pickpack is a terminal GUI which allows you to pick one or more options from a tree structure"
authors = ["Ana <anafvana@gmail.com>", "wong2 <wonderfuly@gmail.com>"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/anafvana/tree_pick"
homepage = "https://github.com/anafvana/tree_pick"
repository = "https://github.com/anafvana/pickpack"
homepage = "https://github.com/anafvana/pickpack"
keywords = ["terminal", "gui", "pick", "tree"]

[tool.poetry.dependencies]
Expand Down
26 changes: 13 additions & 13 deletions src/tree_pick/__init__.py → src/pickpack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
''' tree_pick.py '''
''' pickpack.py '''

from __future__ import annotations

Expand All @@ -9,11 +9,11 @@

from anytree import Node, RenderTree

from tree_pick.anytree_utils import (add_indices, count_leaves, count_nodes,
find_by_index, get_descendants,
get_leaves_only)
from pickpack.anytree_utils import (add_indices, count_leaves, count_nodes,
find_by_index, get_descendants,
get_leaves_only)

__all__ = ['TreePicker', 'tree_pick']
__all__ = ['PickPacker', 'pickpack']


KEYS_ENTER = (curses.KEY_ENTER, ord('\n'), ord('\r'))
Expand All @@ -30,8 +30,8 @@ class OutputMode(enum.Enum):


@dataclass
class TreePicker:
"""The :class:`TreePicker <TreePicker>` object
class PickPacker:
"""The :class:`PickPacker <PickPacker>` object
:param options: a RenderTree (anytree) or a list of options to choose from
:param title: (optional) a title above options list
Expand Down Expand Up @@ -59,7 +59,7 @@ class TreePicker:
output_format: int|str = "nodeindex"
options_map_func: Optional[Callable[[Dict], Node]] = None
all_selected: List[str] = field(init=False, default_factory=list)
custom_handlers: Dict[str, Callable[["TreePicker"], str]] = field(
custom_handlers: Dict[str, Callable[["PickPacker"], str]] = field(
init=False, default_factory=dict
)
index: int = field(init=False, default=0)
Expand Down Expand Up @@ -358,20 +358,20 @@ def _start(self, screen):
def start(self):
return curses.wrapper(self._start)

def tree_pick(*args, **kwargs):
"""Construct and start a :class:`TreePicker <TreePicker>`.
def pickpack(*args, **kwargs):
"""Construct and start a :class:`PickPacker <PickPacker>`.
Usage::
>>> from tree_pick import tree_pick
>>> from pickpack import pickpack
>>> from anytree import Node, RenderTree
>>> child = Node("Child")
>>> root = Node("Root", children=[child])
>>> options = RenderTree(root)
>>> title = 'Please choose an option: '
>>> option, index = tree_pick(options, title)
>>> option, index = pickpack(options, title)
"""
picker = TreePicker(*args, **kwargs)
picker = PickPacker(*args, **kwargs)
return picker.start()
File renamed without changes.

0 comments on commit 1259679

Please sign in to comment.