Skip to content

Commit

Permalink
Added more type annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanslenders committed Apr 25, 2020
1 parent fb36e9e commit 6f5687b
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 162 deletions.
100 changes: 52 additions & 48 deletions pymux/arrangement.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,31 @@
import math
import os
import weakref
from typing import Optional
from enum import Enum
from typing import Dict, List, Optional, Union

from prompt_toolkit.application.current import get_app, get_app_or_none, set_app
from prompt_toolkit.application import Application, get_app, get_app_or_none, set_app
from prompt_toolkit.buffer import Buffer
from ptterm import Terminal

__all__ = (
__all__ = [
"LayoutTypes",
"Pane",
"HSplit",
"VSplit",
"Window",
"Arrangement",
)
]


class LayoutTypes:
class LayoutTypes(Enum):
# The values are in lowercase with dashes, because that is what users can
# use at the command line.
EVEN_HORIZONTAL = "even-horizontal"
EVEN_VERTICAL = "even-vertical"
MAIN_HORIZONTAL = "main-horizontal"
MAIN_VERTICAL = "main-vertical"
TILED = "tiled"

_ALL = [EVEN_HORIZONTAL, EVEN_VERTICAL, MAIN_HORIZONTAL, MAIN_VERTICAL, TILED]
EVEN_HORIZONTAL = "EVEN-HORIZONTAL"
EVEN_VERTICAL = "EVEN-VERTICAL"
MAIN_HORIZONTAL = "MAIN-HORIZONTAL"
MAIN_VERTICAL = "MAIN-VERTICAL"
TILED = "TILED"


class Pane:
Expand All @@ -50,7 +49,7 @@ class Pane:

def __init__(self, terminal: Terminal) -> None:
self.terminal = terminal
self.chosen_name = None
self.chosen_name: Optional[str] = None

# Displayed the clock instead of this pane content.
self.clock_mode = False
Expand Down Expand Up @@ -156,13 +155,13 @@ class Window:

_window_counter = 1000 # Start here, to avoid confusion with window index.

def __init__(self, index=0):
def __init__(self, index=0) -> None:
self.index = index
self.root = HSplit()
self._active_pane = None
self._prev_active_pane = None
self.chosen_name = None
self.previous_selected_layout = None
self.root: Union[VSplit, HSplit] = HSplit()
self._active_pane: Optional[Pane] = None
self._prev_active_pane: Optional[Pane] = None
self.chosen_name: Optional[str] = None
self.previous_selected_layout: Optional[LayoutTypes] = None

#: When true, the current pane is zoomed in.
self.zoom = False
Expand Down Expand Up @@ -209,7 +208,7 @@ def active_pane(self):
return self._active_pane

@active_pane.setter
def active_pane(self, value: Pane):
def active_pane(self, value: Pane) -> None:
# Remember previous active pane.
if self._active_pane:
self._prev_active_pane = weakref.ref(self._active_pane)
Expand Down Expand Up @@ -347,7 +346,7 @@ def active_process(self):
if p is not None:
return p.process

def focus_next(self, count=1):
def focus_next(self, count=1) -> None:
" Focus the next pane. "
panes = self.panes
if panes:
Expand All @@ -357,7 +356,7 @@ def focus_next(self, count=1):
else:
self.active_pane = None # No panes left.

def focus_previous(self):
def focus_previous(self) -> None:
" Focus the previous pane. "
self.focus_next(count=-1)

Expand Down Expand Up @@ -394,12 +393,10 @@ def rotate(self, count=1, with_pane_before_only=False, with_pane_after_only=Fals
split[index] = new_item
split.weights[new_item] = weight

def select_layout(self, layout_type):
def select_layout(self, layout_type: LayoutTypes):
"""
Select one of the predefined layouts.
"""
assert layout_type in LayoutTypes._ALL

# When there is only one pane, always choose EVEN_HORIZONTAL,
# Otherwise, we create VSplit/HSplit instances with an empty list of
# children.
Expand Down Expand Up @@ -453,7 +450,7 @@ def select_layout(self, layout_type):

self.previous_selected_layout = layout_type

def select_next_layout(self, count=1):
def select_next_layout(self, count: int = 1) -> None:
"""
Select next layout. (Cycle through predefined layouts.)
"""
Expand All @@ -462,10 +459,10 @@ def select_next_layout(self, count=1):
if len(self.panes) == 2:
all_layouts = [LayoutTypes.EVEN_HORIZONTAL, LayoutTypes.EVEN_VERTICAL]
else:
all_layouts = LayoutTypes._ALL
all_layouts = list(LayoutTypes)

# Get index of current layout.
layout = self.previous_selected_layout or LayoutTypes._ALL[-1]
layout = self.previous_selected_layout or list(LayoutTypes)[-1]
try:
index = all_layouts.index(layout)
except ValueError:
Expand All @@ -475,7 +472,7 @@ def select_next_layout(self, count=1):
new_layout = all_layouts[(index + count) % len(all_layouts)]
self.select_layout(new_layout)

def select_previous_layout(self):
def select_previous_layout(self) -> None:
self.select_next_layout(count=-1)

def change_size_for_active_pane(self, up=0, right=0, down=0, left=0):
Expand Down Expand Up @@ -558,18 +555,22 @@ class Arrangement:
have different windows active.
"""

def __init__(self):
self.windows = []
def __init__(self) -> None:
self.windows: List[Window] = []
self.base_index = 0

self._active_window_for_cli = weakref.WeakKeyDictionary()
self._prev_active_window_for_cli = weakref.WeakKeyDictionary()
self._active_window_for_cli: Dict[
Application, Window
] = weakref.WeakKeyDictionary()
self._prev_active_window_for_cli: Dict[
Application, Window
] = weakref.WeakKeyDictionary()

# The active window of the last CLI. Used as default when a new session
# is attached.
self._last_active_window = None
self._last_active_window: Optional[Window] = None

def invalidation_hash(self):
def invalidation_hash(self) -> str:
"""
When this changes, the layout needs to be rebuild.
"""
Expand All @@ -579,7 +580,7 @@ def invalidation_hash(self):
w = self.get_active_window()
return w.invalidation_hash()

def get_active_window(self):
def get_active_window(self) -> Window:
"""
The current active :class:`.Window`.
"""
Expand All @@ -593,15 +594,15 @@ def get_active_window(self):
)
return self.windows[0]

def set_active_window(self, window: Window):
def set_active_window(self, window: Window) -> None:
app = get_app()

previous = self.get_active_window()
self._prev_active_window_for_cli[app] = previous
self._active_window_for_cli[app] = window
self._last_active_window = window

def set_active_window_from_pane_id(self, pane_id: int):
def set_active_window_from_pane_id(self, pane_id: int) -> None:
"""
Make the window with this pane ID the active Window.
"""
Expand All @@ -610,7 +611,7 @@ def set_active_window_from_pane_id(self, pane_id: int):
if p.pane_id == pane_id:
self.set_active_window(w)

def get_previous_active_window(self):
def get_previous_active_window(self) -> Optional[Window]:
" The previous active Window or None if unknown. "
app = get_app()

Expand All @@ -625,7 +626,9 @@ def get_window_by_index(self, index):
if w.index == index:
return w

def create_window(self, pane: Pane, name: Optional[str] = None, set_active=True):
def create_window(
self, pane: Pane, name: Optional[str] = None, set_active: bool = True
) -> None:
"""
Create a new window that contains just this pane.
Expand Down Expand Up @@ -659,7 +662,7 @@ def create_window(self, pane: Pane, name: Optional[str] = None, set_active=True)
assert w.active_pane == pane
assert w._get_parent(pane)

def move_window(self, window: Window, new_index: int):
def move_window(self, window: Window, new_index: int) -> None:
"""
Move window to a new index.
"""
Expand All @@ -668,15 +671,16 @@ def move_window(self, window: Window, new_index: int):
# Sort windows by index.
self.windows = sorted(self.windows, key=lambda w: w.index)

def get_active_pane(self):
def get_active_pane(self) -> Optional[Pane]:
"""
The current :class:`.Pane` from the current window.
"""
w = self.get_active_window()
if w is not None:
return w.active_pane
return None

def remove_pane(self, pane: Pane):
def remove_pane(self, pane: Pane) -> None:
"""
Remove a :class:`.Pane`. (Look in all windows.)
"""
Expand All @@ -693,21 +697,21 @@ def remove_pane(self, pane: Pane):

self.windows.remove(w)

def focus_previous_window(self):
def focus_previous_window(self) -> None:
w = self.get_active_window()

self.set_active_window(
self.windows[(self.windows.index(w) - 1) % len(self.windows)]
)

def focus_next_window(self):
def focus_next_window(self) -> None:
w = self.get_active_window()

self.set_active_window(
self.windows[(self.windows.index(w) + 1) % len(self.windows)]
)

def break_pane(self, set_active=True):
def break_pane(self, set_active: bool = True) -> None:
"""
When the current window has multiple panes, remove the pane from this
window and put it in a new window.
Expand All @@ -721,13 +725,13 @@ def break_pane(self, set_active=True):
self.get_active_window().remove_pane(pane)
self.create_window(pane, set_active=set_active)

def rotate_window(self, count=1):
def rotate_window(self, count: int = 1) -> None:
" Rotate the panes in the active window. "
w = self.get_active_window()
w.rotate(count=count)

@property
def has_panes(self):
def has_panes(self) -> bool:
" True when any of the windows has a :class:`.Pane`. "
for w in self.windows:
if w.has_panes:
Expand Down
4 changes: 2 additions & 2 deletions pymux/client/posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

INPUT_TIMEOUT = 0.5

__all__ = (
__all__ = [
"PosixClient",
"list_clients",
)
]


class PosixClient(Client):
Expand Down
2 changes: 1 addition & 1 deletion pymux/commands/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Aliases for all commands.
(On purpose kept compatible with tmux.)
"""
__all__ = ("ALIASES",)
__all__ = ["ALIASES"]


ALIASES = {
Expand Down
Loading

0 comments on commit 6f5687b

Please sign in to comment.