Skip to content

Commit

Permalink
Prepare code for pypi
Browse files Browse the repository at this point in the history
  • Loading branch information
frazenshtein committed Aug 24, 2020
1 parent 1ccf624 commit 76fdc7c
Show file tree
Hide file tree
Showing 16 changed files with 293 additions and 157 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.egg
*.egg-info
*.pyc
.tox
/.cache/
/dist/
/build/
docs/_build

5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [1.3] - 2020-08-25
### Added
- fastcd package to the pypi
- install subcommand

105 changes: 0 additions & 105 deletions README.md

This file was deleted.

124 changes: 124 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
======
fastcd
======

Fastcd allows you to navigate quickly to the desired directory that has been visited recently.

Features
--------

* Autocomplete
* Fuzzy search
* Shortcuts for directories
* Flexible configuration

Usage
-----

Just enter ``j`` in the terminal to launch fastcd's jumper.

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

Control::

'Esc', 'F10' or 'Meta + q' to exit.
'Enter' to change directory to the selected.
'Meta + Enter' to change directory to the entered.
'Tab' for auto completion. If entered path is not valid it will be extended.
'Shift + Tab' to paste selected path.
'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.


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


Extra options and parameters can be found in ``fastcd/config.json``.
``Meta`` is usually stands for ``alt`` key.

Examples
--------

.. image:: https://raw.githubusercontent.com/frazenshtein/images/master/fastcd/example1.png

Tips
----

Missing or non-existent directories will be displayed dimmed and marked with ``*``.
However, if you press Enter twice, you will cd to the nearest existing directory.

If the entered path is not present in the database, but exists, you will still be able to go into it.

If you want to redefine shortcuts (``~/.local/share/fastcd/config.json``), but don't know their key codes - use ``key_picker.py``.

You can change palette in ``~/.local/share/fastcd/config.json``.

Your current path in shell will be first in list,
while the previous path (``OLDPWD``) will be the second.
Previous path is selected by default.
Thus fastcd (j) + enter is equivalent to ``cd -``.

Type ``j -l`` to list shortcuts and directories to which they point.

If you want to change directory immediately when pressing path shortcut (``F2-F8``) - change ``exit_after_path_shortcut_pressed`` to 1 in ``config.json``

Supported platforms
-------------------
* Linux
* MacOs

Installation
------------

1. Get the utility: ``sudo python3 -m pip install fastcd`` or ``python3 -m pip install fastcd --user``
2. Install shell hook to make fastcd able to track visited directories: ``python3 -m fastcd install``.
3. Restart console session or run ``source ~/.bashrc`` to apply changes.
4. Type ``j`` and press enter to run fastcd.

If you would like to install another alias name for fastcd use:
``python3 -m fastcd install --alias=fcd``

Installation from source code
-----------------------------
Get source code::

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


Each user who wants to use fastcd should install shell hook first::

python3 fastcd/fastcd/__main__.py install
source ~/.bashrc


Utility requires python 3 + extra module::

sudo python3 -m pip install urwid



If you do not have pip try first::

sudo apt-get install python3-pip
3 changes: 3 additions & 0 deletions bin/fastcd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env python3
from fastcd.jumper import main
main()
1 change: 1 addition & 0 deletions fastcd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '1.2.0'
7 changes: 7 additions & 0 deletions fastcd/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3
try:
from fastcd.jumper import main
except ImportError:
from jumper import main

main()
2 changes: 1 addition & 1 deletion config.json → fastcd/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.154",
"version": "1.3.154",

"history_file": "~/.local/share/fastcd/history.txt",
"shortcuts_paths_file": "~/.local/share/fastcd/shortcuts_paths.txt",
Expand Down
33 changes: 33 additions & 0 deletions fastcd/fastcd_hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

FASTCDDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
JUMPERTOOL="$FASTCDDIR/jumper.py"
FASTCDCONFDIR="$HOME/.local/share/fastcd"

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

case $PROMPT_COMMAND in
*_fastcd_hook*)
;;
*)
PROMPT_COMMAND="${PROMPT_COMMAND:+$(echo "${PROMPT_COMMAND}" | awk '{gsub(/; *$/,"")}1') ; }_fastcd_hook"
;;
esac

function fastcd {
PATHFILE="/tmp/fastcd.$$.`date +%s`.path"
python3 $JUMPERTOOL --escape-special-symbols -o $PATHFILE $@
if [[ $? -eq 0 ]] && [[ $# -eq 0 ]]
then
OUTPUTPATH=`cat $PATHFILE`
rm $PATHFILE
if [[ ! -z "$OUTPUTPATH" ]]
then
# Eval is required to interpret ~
eval cd $OUTPUTPATH
fi
fi
}
35 changes: 22 additions & 13 deletions jumper.py → fastcd/jumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
print("Old urwid version detected (%s). Please, upgrade it first 'sudo pip3 install --upgrade urwid'" % urwid.__version__)
exit(1)

import util
import search
try:
import util
import search
except ImportError:
from fastcd import util, search


DESC = '''
Jumper shows last visited directories and allows you change cwd quickly.
Fastcd's jumper shows last visited directories and allows you change cwd quickly.
You can use arrows and Page Up/Down to navigate the list.
Start typing to filter directories.
Expand Down Expand Up @@ -68,6 +71,8 @@

def parse_command_line(config):
parser = argparse.ArgumentParser(description=get_description(config["shortcuts"]), formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("install", nargs='?', help="Setup shell hook make fastcd able to track visited directories")
parser.add_argument("--alias", default='j', help="Specifies installation alias for fastcd (default: 'j')")
parser.add_argument("-l", "--list-shortcut-paths", action='store_true', help="Displays list of stored shortcut paths")
parser.add_argument("-a", "--add-path", default=None, help=argparse.SUPPRESS) # add path to base
parser.add_argument("-o", "--output", metavar="FILE", default=None, help=argparse.SUPPRESS)
Expand Down Expand Up @@ -95,19 +100,14 @@ def representative(shortcut):
return DESC.format(**shortcuts_data)


def get_reference_config_path():
module_path = os.path.dirname(os.path.realpath(__file__))
return os.path.join(module_path, "config.json")


def load_config():
def expand_paths(config):
paths = ["history_file", "shortcuts_paths_file", "user_config_file"]
for param in paths:
config[param] = expanduser(config[param])
return config

ref_config = expand_paths(util.load_json(get_reference_config_path()))
ref_config = expand_paths(util.load_json(util.get_reference_config_path()))
if os.path.exists(ref_config["user_config_file"]):
usr_config = expand_paths(util.load_json(ref_config["user_config_file"]))
config = util.patch_dict(ref_config, usr_config)
Expand All @@ -129,7 +129,7 @@ def prepare_environment(config):
# generate user config
user_config_file = config["user_config_file"]
if not os.path.exists(user_config_file):
with open(get_reference_config_path()) as afile:
with open(util.get_reference_config_path()) as afile:
data = afile.read()
# remove description warning
data = data[data.find("{"):]
Expand Down Expand Up @@ -713,7 +713,17 @@ def update_listbox(self):
self.listbox.set_focus(0)


def main(args, config):
def main():
config = load_config()
args = parse_command_line(config)

if args.install:
util.install_shell_hook(args.alias)
print("Restart console session or run 'source ~/.bashrc' to finish fastcd's installation.")
print("Then use '{}' command to run fastcd.".format(args.alias))
prepare_environment(config)
return

prepare_environment(config)

if args.list_shortcut_paths:
Expand Down Expand Up @@ -760,5 +770,4 @@ def main(args, config):


if __name__ == '__main__':
CONFIG = load_config()
main(parse_command_line(CONFIG), CONFIG)
main()
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 76fdc7c

Please sign in to comment.