Skip to content

Commit

Permalink
Switched to python3
Browse files Browse the repository at this point in the history
  • Loading branch information
frazenshtein committed Aug 21, 2020
1 parent 9bb39f9 commit 1ccf624
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 46 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ Fastcd allows you to navigate quickly to the desired directory that has been vis

### Usage

Just enter 'j' in the terminal to launch jumper that shows
last visited directories and allows you change cwd quickly.
Just enter 'j' in the terminal to launch fastcd's jumber.

Jumper shows last visited directories and allows you to change cwd quickly.
You can use arrows and Page Up/Down to navigate the list.
Start typing to filter directories.

Control:
'Esc', 'F10' or 'Meta + q' to exit.
'Enter' to change directory to the selected.
'Meta + Enter' to change directory to the entered.
Expand All @@ -28,16 +30,19 @@ Start typing to filter directories.
'Ctrl + w' or 'Ctrl + u' to clean up input.
'Meta + Backspace' to remove word.
'Ctrl + c' to copy selected path to clipboard (pygtk support required).

Search:
'Meta + f' to turn on/off fuzzy search.
'Meta + r' to change search position (any pos / from the beginning of the directory name).
'Meta + s' to turn on/off case sensitive search.
'Meta + l' to move search forward.
'Meta + b' to move search backward.
'Shift F2-F8' to set selected path as shortcut.
'F2-F8' to paste shortcut path.

Supported extra symbols:
Shortcuts:
'Shift + F2', 'Shift + F3', 'Shift + F4' or 'Shift + F5' to set selected path as shortcut.
'F2', 'F3', 'F4' or 'F5' to paste shortcut path.

Supported extra symbols:
* - any number of any character
$ - end of the line

Expand Down Expand Up @@ -73,6 +78,7 @@ If you want to change directory immediately when pressing path shortcut ('F2'-'F

Get the utility:

cd $HOME
git clone https://github.com/frazenshtein/fastcd

Each user who wants to use fastcd should source the
Expand All @@ -85,16 +91,15 @@ And reload bashrc in your terminal:

source ~/.bashrc

Utility requires python 2.7 + extra module.
Utility requires python 3 + extra module.
Just type the following to install required python modules:

sudo pip install urwid
sudo pip3 install urwid

If you do not have privileges try:

pip install --user urwid
python3 -mpip install --user urwid

If you do not have pip try first:

sudo apt-get install python-pip

sudo apt-get install python3-pip
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
See 'user_config_file' parameter.
*/
{
"version": "1.2.153",
"version": "1.2.154",

"history_file": "~/.local/share/fastcd/history.txt",
"shortcuts_paths_file": "~/.local/share/fastcd/shortcuts_paths.txt",
Expand Down
37 changes: 26 additions & 11 deletions jumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
try:
import urwid
except ImportError:
print("Cannot import urwid module. Install it first 'sudo pip install urwid' or 'pip install --user urwid'")
print("Cannot import urwid module. Install it first 'sudo pip3 install urwid' or 'python3 -mpip install --user urwid'")
exit(1)

URWID_VERSION = urwid.__version__.split(".")
if float(URWID_VERSION[0] + "." + URWID_VERSION[1]) < 1.1:
print("Old urwid version detected (%s). Please, upgrade it first 'sudo pip install --upgrade urwid'" % urwid.__version__)
print("Old urwid version detected (%s). Please, upgrade it first 'sudo pip3 install --upgrade urwid'" % urwid.__version__)
exit(1)

import util
Expand All @@ -38,6 +38,7 @@
You can use arrows and Page Up/Down to navigate the list.
Start typing to filter directories.
Control:
{exit} to exit.
{cd_selected_path} to change directory to the selected.
{cd_entered_path} to change directory to the entered.
Expand All @@ -46,16 +47,19 @@
{clean_input} to clean up input.
{remove_word} to remove word.
{copy_selected_path_to_clipboard} to copy selected path to clipboard (pygtk support required).
Search:
{fuzzy_search} to turn on/off fuzzy search.
{search_pos} to change search position (any pos / from the beginning of the directory name).
{case_sensitive} to turn on/off case sensitive search.
{inc_search_offset} to move search forward.
{dec_search_offset} to move search backward.
Shortcuts:
{store_shortcut_path} to set selected path as shortcut.
{cd_to_shortcut_path} to paste shortcut path.
Supported extra symbols:
* - any number of any character
$ - end of the line
Expand Down Expand Up @@ -331,10 +335,7 @@ def set_text(self, text):
self.path_edit.set_edit_pos(len(text))

def get_text(self):
text = self.path_edit.get_edit_text()
if isinstance(text, unicode):
return text.encode("utf-8")
return text
return self.path_edit.get_edit_text()

def create_pop_up(self):
return self.popup
Expand Down Expand Up @@ -386,6 +387,7 @@ def __init__(self, config):
# select by default oldpwd or last visited if there is no oldpwd
self.default_selected_item_index = 1
self.shortcuts_paths_filename = self.config["shortcuts_paths_file"]
self.shortcuts_cache = set()
self.stored_paths = self.get_stored_paths()

if len(self.stored_paths) < 2:
Expand Down Expand Up @@ -484,10 +486,13 @@ def get_stored_paths(self):
return paths

def is_shortcut(self, input):
return filter(lambda x: input in x, self.shortcuts.values()) != []
if not self.shortcuts_cache:
for x in self.shortcuts.values():
self.shortcuts_cache.update(x)
return input in self.shortcuts_cache

def input_handler(self, input):
if not isinstance(input, (str, unicode)):
if not isinstance(input, str):
return input

if input in self.shortcuts["exit"]:
Expand Down Expand Up @@ -552,14 +557,15 @@ def input_handler(self, input):

if input in self.shortcuts["case_sensitive"]:
self.case_sensitive = not self.case_sensitive
self.update_search_engine_label()

if input in self.shortcuts["fuzzy_search"]:
self.fuzzy_search = not self.fuzzy_search
self.search_engine_label.set_text(self.get_search_engine_label_text())
self.update_search_engine_label()

if input in self.shortcuts["search_pos"]:
self.search_from_any_pos = not self.search_from_any_pos
self.search_engine_label.set_text(self.get_search_engine_label_text())
self.update_search_engine_label()

if input in self.shortcuts["inc_search_offset"]:
self.search_offset += 1
Expand Down Expand Up @@ -596,6 +602,7 @@ def input_handler(self, input):

if input in self.shortcuts["clean_input"]:
self.path_filter.set_text("")
return

# display input if it is not a shortcut
if not self.is_shortcut(input):
Expand Down Expand Up @@ -653,6 +660,9 @@ def extend_path_filter_text(self):
self.path_filter.set_text(path)
return path

def update_search_engine_label(self):
self.search_engine_label.set_text(self.get_search_engine_label_text())

def get_search_engine_label_text(self):
parts = []
if self.fuzzy_search:
Expand All @@ -665,6 +675,11 @@ def get_search_engine_label_text(self):
else:
parts.append("/headed")

if self.case_sensitive:
parts.append("CS")
else:
parts.append("CIS")

return "[%s]" % " ".join(parts)[:self.search_engine_label_limit - 2]

def update_listbox(self):
Expand Down
8 changes: 4 additions & 4 deletions search.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ def build_fuzzy_fda(self, pattern, finite_state=None, level_shift=0):
left_state = State(pattern[level], pattern[level + 1], self.ANY_SYMBOL, level=level, level_shift=level_shift)

# create state for middle edge and link it with core state
middle_State = State(pattern[level - 1], level=level, level_shift=level_shift)
middle_State.left_state = core_states[level + 1]
middle_state = State(pattern[level - 1], level=level, level_shift=level_shift)
middle_state.left_state = core_states[level + 1]

state.left_state = left_state
state.middle_state = middle_State
state.middle_state = middle_state
state.right_state = core_states[level]
# shift to a deeper level
state = left_state
Expand Down Expand Up @@ -289,7 +289,7 @@ def search(self, string, pos=0):
match = self.search_automaton(string, pos, automaton)
if not match:
return None
elif match.end() == len(string):
if match.end() == len(string):
break
pos = match.end()
else:
Expand Down
4 changes: 2 additions & 2 deletions set.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ JUMPERTOOL="$FASTCDTOOLS/jumper.py"

# Set hook to collect visited dirs
fastcd_hook() {
(python $JUMPERTOOL --add-path "$(pwd)" 2>>${FASTCDTOOLS}/errors.log 1>&2 &) &>/dev/null
(python3 $JUMPERTOOL --add-path "$(pwd)" 2>>${FASTCDTOOLS}/errors.log 1>&2 &) &>/dev/null
}

case $PROMPT_COMMAND in
Expand All @@ -23,7 +23,7 @@ esac
# Set (J)umper
function j {
PATHFILE="/tmp/`date +%s`.path"
python $JUMPERTOOL --escape-special-symbols -o $PATHFILE $@
python3 $JUMPERTOOL --escape-special-symbols -o $PATHFILE $@
if [[ $? -eq 0 ]] && [[ ! $@ == "--help" ]] && [[ ! $@ == "-h" ]] && [[ ! $@ == "--list-shortcut-paths" ]] && [[ ! $@ == "-l" ]]
then
OUTPUTPATH=`cat $PATHFILE`
Expand Down
20 changes: 2 additions & 18 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,12 @@ def get_nearest_existing_dir(dir):
return dir


def convert_json(data):
if isinstance(data, dict):
return {convert_json(key): convert_json(value) for key, value in data.iteritems()}
elif isinstance(data, list):
return [convert_json(element) for element in data]
elif isinstance(data, unicode):
string = data.encode("utf-8")
# if string.isdigit():
# return int(string)
return string
else:
return data


def load_json(filename):
with open(filename) as file:
data = file.read()
# remove comments
data = re.sub(r"\/\*.*?\*\/", "", data, flags=re.MULTILINE|re.DOTALL)
jsonData = json.loads(data)
return convert_json(jsonData)
return json.loads(data)


def get_stdin_buffer(one_line=False):
Expand All @@ -97,8 +82,7 @@ def get_stdin_buffer(one_line=False):
break
symbols.append(char)
return ''.join(symbols)
else:
return sys.stdin.read()
return sys.stdin.read()
finally:
termios.tcsetattr(stdin, termios.TCSANOW, original_tty_attrs)
except Exception:
Expand Down

0 comments on commit 1ccf624

Please sign in to comment.