All Projects → smetj → Termfunk

smetj / Termfunk

Licence: gpl-3.0
A lightweight framework to organize, access and execute your Python functions from the terminal.

Programming Languages

python
139335 projects - #7 most used programming language
python3
1442 projects
bash
514 projects

Labels

Projects that are alternatives of or similar to Termfunk

Curlie
The power of curl, the ease of use of httpie.
Stars: ✭ 877 (+2729.03%)
Mutual labels:  terminal
Nord Guake
An arctic, north-bluish clean and elegant Guake color theme.
Stars: ✭ 20 (-35.48%)
Mutual labels:  terminal
Tldr
📚 Collaborative cheatsheets for console commands
Stars: ✭ 36,408 (+117345.16%)
Mutual labels:  terminal
Radian
A 21 century R console
Stars: ✭ 878 (+2732.26%)
Mutual labels:  terminal
Pixelart
video to character animation magic
Stars: ✭ 20 (-35.48%)
Mutual labels:  terminal
Prompt Checkbox
This repository has been archived, use Enquirer instead.
Stars: ✭ 21 (-32.26%)
Mutual labels:  terminal
Tui
A text-based user interface library for golang based on termbox
Stars: ✭ 12 (-61.29%)
Mutual labels:  terminal
Termite Color Switcher
termite color switcher
Stars: ✭ 28 (-9.68%)
Mutual labels:  terminal
Teds Terminal
A modern terminal emulator with high DPI support, mouse wheel scaling, 32-bit colour and tty compatible.
Stars: ✭ 20 (-35.48%)
Mutual labels:  terminal
Alacritty
Alacritty is a modern terminal emulator that comes with sensible defaults, but allows for extensive configuration. By integrating with other applications, rather than reimplementing their functionality, it manages to provide a flexible set of features with high performance. The supported platforms currently consist of BSD, Linux, macOS and Windows.
Stars: ✭ 36,273 (+116909.68%)
Mutual labels:  terminal
Catimg
🦦 Insanely fast image printing in your terminal
Stars: ✭ 880 (+2738.71%)
Mutual labels:  terminal
Awesome Windows Terminal
A collection of awesome things regarding the upcoming Windows Terminal
Stars: ✭ 20 (-35.48%)
Mutual labels:  terminal
Viu
Simple terminal image viewer written in Rust.
Stars: ✭ 911 (+2838.71%)
Mutual labels:  terminal
Create Component App
Tool to generate different types of React components from the terminal. 💻
Stars: ✭ 879 (+2735.48%)
Mutual labels:  terminal
Goat
POSIX-compliant shell movement boosting hack for real ninjas (aka `cd x` and `cd ...`)
Stars: ✭ 27 (-12.9%)
Mutual labels:  terminal
Closestx11color
Find the closest xterm-256 colors (between 0 and 255) to an arbitrary HTML hexa color (e.g. #ABCDEF)
Stars: ✭ 13 (-58.06%)
Mutual labels:  terminal
Lsankidb
☆ `ls` for your local Anki database.
Stars: ✭ 21 (-32.26%)
Mutual labels:  terminal
Yori
Yori is a CMD replacement shell that supports backquotes, job control, and improves tab completion, file matching, aliases, command history, and more.
Stars: ✭ 948 (+2958.06%)
Mutual labels:  terminal
Gomu
golang TUI music player
Stars: ✭ 27 (-12.9%)
Mutual labels:  terminal
Edex Ui
A cross-platform, customizable science fiction terminal emulator with advanced monitoring & touchscreen support.
Stars: ✭ 34,770 (+112061.29%)
Mutual labels:  terminal

Python functions from the terminal.

TermFunk is a lightweight framework to organize, access and execute your Python functions from the terminal.

TermFunk is a convenience wrapper to bring Python closer to your terminal so you can develop script logic in Python whilst keep using your favorite terminal as you know it. Python function parameters are automatically mapped to command parameters which can be provided on the command line, through environment variables or interactively.

The docstring of each version is exposed by issuing --help.


Installation and usage

Installation

TermFunk needs Python3 and can be installed by running:

pip install termfunk

Configuring Bash autocompletion

TermFunk has support builtin to configure Bash autocompletion.

Assuming you have saved your script to /usr/local/bin/fnc, add the following command to your .bashrc file:

eval "$(/usr/share/bin/fnc complete)"

The complete command prints a Bash script to STDOUT which can be eval'ed to fully configure Bash completion for your defined functions and their parameters.

Usage

TermFunk is a baseclass for the class you write containing your functions:

#!/usr/bin/env python

from termfunk import TermFunk
from termfunk import Ask
from termfunk import Choice
from termfunk import File


class MyFunctions(TermFunk):
    def function_greet(self):
        """
        Just say hello!
        """

        for _ in range(10):
            print("Hello World!")

    def function_parameterdemo(
        self,
        url=Ask(),
        username=Ask(),
        password=Ask(secret=True),
        value=Choice(["a_one", "a_two", "b_three"]),
    ):
        """
        Demo access to parameters
        """
        print(
            "I will request url '%s' with username '%s' and password '%s' using values '%s'."
            % (url, username, password, value)
        )

    def function_validate(
        self,
        file=File(),
    ):
        """
        Validates file
        """
        print("I will validate file %s" % (file))


def main():

    MyFunctions()


if __name__ == "__main__":
    main()

After the script has been saved as "demo" and made executable is can be executed as such:

$ ./demo --help
usage: demo [-h] {list,greet,parameterdemo} ...

TermFunk

positional arguments:
  {list,greet,parameterdemo}

optional arguments:
  -h, --help            show this help message and exit


Executing the function from your terminal is simple as:

$ ./demo greet
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

Accessing help with information about the function and its parameters can be done using --help. Each parameter shows how it can retrieve information.

./demo parameterdemo --help
usage: demo parameterdemo [-h] [--url URL] [--username USERNAME]
                          [--password PASSWORD]

Demo access to parameters

optional arguments:
  -h, --help           show this help message and exit
  --url URL            : <$DEMO_URL> or <Interactive>
  --username USERNAME  : <$DEMO_USERNAME> or <Interactive>
  --password PASSWORD  : <$DEMO_PASSWORD> or <Interactive>

Parameters values can be provided during execution:

$ ./demo parameterdemo --url http://hello --username john --password doe
I will request url 'http://hello' with username 'john' and password 'doe'.

Parameter values can be set by environment variables. Each variable is in uppercase and is prefixed with the name of the script:

$ export DEMO_URL="http://some-place-in-the-clouds"
$ ./demo parameterdemo --username john --password doe
I will request url 'http://some-place-in-the-clouds' with username 'john' and password 'doe'.

If a parameter value is retrieved from an environment variable --help will tell you that:

$ ./demo parameterdemo --help
usage: demo parameterdemo [-h] [--url URL] [--username USERNAME]
                          [--password PASSWORD]

Demo access to parameters

optional arguments:
  -h, --help           show this help message and exit
  --url URL            : <$DEMO_URL http://some-place-in-the-clouds>
  --username USERNAME  : <$DEMO_USERNAME> or <Interactive>
  --password PASSWORD  : <$DEMO_PASSWORD> or <Interactive>

If you define Ask(secret=True) TermFunk will obfuscate that value in the --help output:

$ export DEMO_PASSWORD="some secret value"
$ ./demo parameterdemo --help
usage: demo parameterdemo [-h] [--url URL] [--username USERNAME]
                          [--password PASSWORD]

Demo access to parameters

optional arguments:
  -h, --help           show this help message and exit
  --url URL            : <$DEMO_URL http://some-place-in-the-clouds>
  --username USERNAME  : <$DEMO_USERNAME> or <Interactive>
  --password PASSWORD  : <$DEMO_PASSWORD **********>

If you don't define a parameter value on CLI then you can provide them interactively. Since all other parameters are know at this stage, only the unknown ones are asked for.

$ ./demo parameterdemo
Value for username: Happy Cat
I will request url 'http://some-place-in-the-clouds' with username 'Happy Cat' and password 'some secret value'.

You can define a set of predefined parameter values which can be auto- completed using the Choice() type:

$ ./demo parameterdemo --value a_<tab>
a_one a_two

File completion can be triggerd by assigning the File() to the desired parameter.

$ ./demo validate --name <tab>
demo .git .gitignore setup.py

Change Log

0.3.0

  • Added type File() offering file completion.

0.2.0

  • Removed list command in favor of complete
  • Added complete command which returns a Bash completion config which can be eval'ed.
  • Fixed bug where functions without a doc-string would break the script.
  • Added Choice type allowing you to auto-complete predetermined values.

0.1.0

  • Initial commit.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].