tchar / ulauncher-albert-calculate-anything

A ULauncher/Albert extension that supports currency, units and date time conversion, as well as a calculator that supports complex numbers and functions.
MIT License
103 stars 13 forks source link
albert albert-extensions albertlauncher calculator conversion converter launcher launcher-utility python python3 ulauncher ulauncher-extension ulauncher-extensions

drawing Calculate Anything

Platforms Python Versions License Latest Version

Linux CI Benchmark CodeQL Codecov

Code Style

Benchmarks

Ulauncher/Albert Calculate Anything is an extension for Ulauncher and Albert to calculate things like currency, time, percentage, units, complex equations, base-n expressions and more.

The calculate_anything module does not depend on Ulauncher or Albert so it is extensible for other use cases (see demo with prompt_toolkit).

See Features for supported features, Contents for installation instructions and more

Albert Demo

Albert Demo

Ulauncher Demo

Ulauncher Demo

Features

Calculator for Anything

The only launcher specific files for ulauncher and albert are main.py, __init__.py in the root of this project

Dependencies: simpleeval, pint parsedatetime and pytz (for parsedatetime) Currency and Unit converter as well as a Calculator for numbers, complex numbers, percentages and time that supports mathematical functions and Complex Numbers.

Optional Dependencies: babel. Installing this will format your results in your language/locale.

Contents

Install for Ulauncher

Thus extension depends on Pint, simpleeval and parsedatetime. Install them with:

# You probably have some of them already installed
/usr/bin/python3 -m pip install Pint simpleeval parsedatetime pytz

# Optionally for translations and formatting to your locale
/usr/bin/python3 -m pip install babel

Open Ulauncher go to Extensions > Add extension and paste https://github.com/tchar/ulauncher-albert-calculate-anything

Install for Albert

Similarly to Ulauncher the same dependencies are are required.

To install the extension for Albert run

# You probably have some of them already installed
/usr/bin/python3 -m pip install Pint simpleeval parsedatetime pytz

# Optionally for translations and formatting to your locale
/usr/bin/python3 -m pip install babel

# Determine Install location
[ -z "$XDG_DATA_HOME" ] && INSTALL_DIR=~/.local/share || INSTALL_DIR=$XDG_DATA_HOME

# Create module directory if not exists
mkdir -p $INSTALL_DIR/albert/org.albert.extension.python/modules/

# Install extension
git clone https://github.com/tchar/ulauncher-albert-calculate-anything $INSTALL_DIR/albert/org.albert.extension.python/modules/

Open albert, enable Python extensions and then enable the Calculate Anything extension.

You can double click it to open module's location and edit __init__.py to add your preferences.

How to Setup

Albert

If you are using Albert open the extension location normally at ~/.local/share/albert/org.albert.extension.python/modules/ulauncher-albert-calculate-anything/__init__.py and edit the preferences mentioned below in the apropriate variable API_KEY, CACHE, DEFAULT_CURRENCIES, DEFAULT_CITIES, SHOW_EMPTY_PLACEHOLDER or __triggers__ for the keyword

The extension can work in albert without keywords if you comment out the __triggers__ option, however if another extension has the keyword you type, Calculate Anything won't trigger (see relevant issue)

Ulauncher

If you are using Ulauncher use the extension preferences.

Set Currency Provider

You can select from different currency providers. Supported providers are:

Preferences:

Set API Key

In order for the currency conversion to work for providers that need an API Key, you need to set it in the preferences.

Cache

For currency conversion you can enable the cache for a minimum of 1 day up to 1 year. This will store the results fetched by your currency provider to prevent redundant requests. This is especially helpful if you have a free plan on a paid currency provider that limits your requests. It will also display the results faster, since no request is made. If all requested currencies have been cached, no request is made.

Default currency

In the preferences you can define a comma separated list of default currencies to show when typing conversion without target unit/currency. Defaults to USD,EUR,CAD,GBP,AUD

Default cities

In the preferences you can define a comma separated list of default cities when using the time command

Units Conversion Mode

In the preferences you can define a units conversion mode. For now there is normal (default) and crazy.

Crazy means that the unit converter/calculator tries to convert all possible units (currency included) available under the name.

See Currency and Units for more

Crazy mode is experimental and bugs are to be expected

Show Empty Placeholder

Commands and Syntax

To calculate/convert anything you can use the keywords

You can go directly to examples or use the ones from the demo

To convert currency type your keyword and then

To convert units use

Comma separated units and currencies can have spaces between them.

For time you can use the time keyword with a syntax

To calculate an expression just type your expression as in the demo

To calculate percentages you can use one of the following

If you select one results it will be copied to clipboard.

Examples

Currency

Simple Conversion

Multiple Conversion

Crazy Conversion

crazy mode must be enabled in preferences

Time

You can also add and subtract time For example if now is 2021-07-05 14:14:42 then you can use the following

Be careful to use date timespans like 2 years 5 months 2 weeks 3 days 1 hour 4 minutes 3 seconds and not dates like December 2022.

In the following examples the time returned is accompanied by the date time in the default cities you specified in the extension preferences

Specifying a target city

You can use all the commands above followed by at CITY NAME or at CITY NAME, COUNTRY NAME|COUNTRY CODE|STATE CODE to get te result in your local time as well as the specified city

Using until

You can also use the until command (Experimental) to calculate duration of time until a specific date

Note: The midnight keyword shifts one day after, so midnight is considered to belong in the next day

In the following examples you can specify a specific date and time or say for example a number of years months etc.

Keywords such as a/next/last/previous/ago, years/months/weeks/days/hours/minutes/seconds, morning/noon/afternoon/evening/night/midnight, tomorrow/yesterday and the combination of those will work like in the normal mode.

And many more combinations

Units

The units supported are all units that pint supports (which is quite a lot)

Simple Conversion

Multiple Conversion

Advanced Conversion

You can lieterally convert anything if the apropriate units match

Crazy Conversion

crazy mode must be enabled in preferences

Percentages

Simple Cases

Advanced Cases

Calculator

The calculator works like a normal calculator, but is able to work with complex numbers too.

The following constants exist: pi, e, tau and others from cmath

The following functions exist: phase, polar, rect, exp, log, log10, sqrt, acos, asin, atan, cos, sin, tan, acosh, asinh, atanh, cosh, sinh, tanh and others from cmath

Simple Cases

Complex Numbers

Use i as the imaginary unit

Base N Calculator

Use with the keywords hex, dec, bin, oct by default.

Simple Cases

Special cases with hex

The hex calculator will always produce the byte representation of its input query.

Color Conversion with hex

If the input is in the format of #xxxxxx where xxxxxx is a valid hex number, it will convert the number representing a color to other color formats.

Advanced Cases

Known Issues

If at any moment currency stops showing try removing the currency cache file and restart the Launcher/program

Linux

rm ~/.cache/com.github.tchar.calculate-anything/currency_data.json

Windows

rm ~\AppData\Local\tchar\com.github.tchar.calculate-anything\Cache\currency_data.json

macOS

rm ~/Library/Caches/com.github.tchar.calculate-anything/currency_data.json

Extending and More

The calculate_anything module does not depend on ulauncher or albert, only the main.py (for Ulauncher) and __init__.py (for albert) do. You can extend it for other cases.

See the documentation for API call examples

You can also find a sample usage of the API using prompt_toolkit at the prompt.py file.

Here is a demo

Adding flags

If your currencie's flag is missing you can place it in the extension's flags directory at calculate_anything/images/flags/ and restart your launcher or make a pull request to include it.

Make sure to name your flag image in uppercase 2 letter name of your country. To make a currency flag, simply link the country flag you want to the currency e.g ln -s US.svg USD.svg or add a completely new flag For example American Dollar's flag is in calculate_anything/images/flags/USD.svg. You can use most image formats (i.e svg, png)

Flag Icons made by Freepik from www.flaticon.com