Skip to content

Commit

Permalink
Only use curses if in term mode - fixes Jupyter no-start (Gallopsled#826
Browse files Browse the repository at this point in the history
)
  • Loading branch information
6tudent committed Mar 26, 2017
1 parent 4d529cd commit 6f27a47
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
32 changes: 17 additions & 15 deletions pwnlib/term/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import sys

from . import completer
from . import key
from . import keymap
from . import readline
from . import term
from . import termcap
from . import text

# Re-exports (XXX: Are these needed?)
output = term.output
width = term.width
height = term.height
getkey = key.get
Keymap = keymap.Keymap

#: This is True exactly when we have taken over the terminal using :func:`init`.
term_mode = False

Expand Down Expand Up @@ -53,6 +38,23 @@ def can_init():
return True


if can_init():
from . import completer
from . import key
from . import keymap
from . import readline
from . import term
from . import termcap
from . import text

# Re-exports (XXX: Are these needed?)
output = term.output
width = term.width
height = term.height
getkey = key.get
Keymap = keymap.Keymap


def init():
"""Calling this function will take over the terminal (iff :func:`can_init`
returns True) until the current python interpreter is closed.
Expand Down
2 changes: 2 additions & 0 deletions pwnlib/term/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ def _init_ti_table():
global _ti_table
_ti_table = []
for fname, name in zip(kc.STRFNAMES, kc.STRNAMES):
if not term_mode:
continue
seq = termcap.get(name)
if not seq:
continue
Expand Down
7 changes: 4 additions & 3 deletions pwnlib/term/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ def put(s):
def flush(): fd.flush()

def do(c, *args):
s = termcap.get(c, *args)
if s:
put(s)
if term_mode:
s = termcap.get(c, *args)
if s:
put(s)

def goto((r, c)):
do('cup', r - scroll + height - 1, c)
Expand Down
22 changes: 18 additions & 4 deletions pwnlib/term/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import sys
import types

import __init__

from . import termcap


Expand All @@ -23,7 +25,10 @@ class Module(types.ModuleType):
def __init__(self):
self.__file__ = __file__
self.__name__ = __name__
self.num_colors = termcap.get('colors', default = 8)
if __init__.term_mode:
self.num_colors = termcap.get('colors', default = 8)
else:
self.num_colors = 2
self.has_bright = self.num_colors >= 16
self.has_gray = self.has_bright
self.when = 'auto'
Expand All @@ -43,7 +48,10 @@ def __init__(self):
('bold' , 'bold'),
('underline', 'smul'),
('reverse' , 'rev')]:
s = termcap.get(y)
if __init__.term_mode:
s = termcap.get(y)
else:
s = ''
self._attributes[x] = s
self._cache = {}

Expand All @@ -56,10 +64,16 @@ def when(self, val):
self._when = eval_when(val)

def _fg_color(self, c):
return termcap.get('setaf', c) or termcap.get('setf', c)
if __init__.term_mode:
return termcap.get('setaf', c) or termcap.get('setf', c)
else:
return ''

def _bg_color(self, c):
return termcap.get('setab', c) or termcap.get('setb', c)
if __init__.term_mode:
return termcap.get('setab', c) or termcap.get('setb', c)
else:
return ''

def _decorator(self, desc, init):
def f(self, s, when = None):
Expand Down

2 comments on commit 6f27a47

@shanehuntley
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ready to become a pull request? It would be nice to use some pwntools functions in Jupyter.

@6tudent
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi, this was really meant as a quick fix to get version 3.4.1 (available via pip at the time) going in jupyter. I can have a look at what it takes to make a similar change on current dev.

Please sign in to comment.