Skip to content

Commit

Permalink
usb: use injected properties
Browse files Browse the repository at this point in the history
  • Loading branch information
kb100 committed Aug 24, 2018
1 parent 9abe517 commit 444469b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 42 deletions.
50 changes: 21 additions & 29 deletions usb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ Dependencies: udev, python3, util-linux ( >= 2.23 )
Suggested: fonts-font-awesome

# Installation

To use with i3blocks, put `usb` somewhere convenient.
We will assume it is at `$SCRIPT_DIR/usb`.
Copy the blocklet configuration in the given `i3blocks.conf` into your
i3blocks configuration file.
The recommended i3blocks config is

```INI
Expand All @@ -33,6 +28,20 @@ command=$SCRIPT_DIR/usb
markup=pango
signal=1
interval=10
#IGNORE_LIST=[]
#IGNORE_LIST=["sdd1", "mapper/sda1_crypt"]
#INFO_TEXT_COLOR=white
#MOUNTED_COLOR=green
#PLUGGED_COLOR=gray
#LOCKED_COLOR=gray
#UNLOCKED_NOT_MOUNTED_COLOR=yellow
#PARTITIONLESS_COLOR=red
#PARTITIONLESS_TEXT=no partitions
#SEPARATOR=<span color='gray'> | </span>
#LOCKED_INDICATOR=
#UNLOCKED_INDICATOR=
#READONLY_INDICATOR=ro
#TRUNCATE_FS_LABELS=[not set by default, accepts +/- integers]
```

To update the blocklet on plug/unplug device events you can add
Expand Down Expand Up @@ -62,32 +71,15 @@ Try plugging in a usb device to make sure everything works.

# Configuration

Configuration can be done either by editing the top portion of `usb`, or by
specifying command line flags.
Run with `--help` for more information.
You will find several options that you can configure.
Probably the most useful to you will be the `-i` (ignore) flag or the `ignore`
and `fastIgnore` functions in `usb`.
These allow you to ignore devices, e.g. those that are always plugged in.
The `-i` flag can be specified multiple times and if a device does not begin
with "/" it is assumed to be in /dev/.
E.g.
`command=$SCRIPT_DIR/usb -i sda1 -i sda2 -i mapper/sda6_crypt`
will ignore /dev/sda1, /dev/sda2, and /dev/mapper/sda6_crypt.
The IGNORE_LIST variable, if set, must be a valid python representation of
a list of strings, e.g. `["sdd1", "mapper/sda1_crypt"]`.
Due to the way i3blocks parses config files, newlines are not allowed.
The strings are full device paths, with "/dev" optionally omitted.
For example, the previous "sdd1" is expanded to "/dev/sdd1" at runtime.
The list is safely parsed using ast.literal_eval, NOT eval.

If you decide not to install FontAwesome,
then you will probably want to change the `LOCKED_INDICATOR` and
`UNLOCKED_INDICATOR` variables, as these use unicode symbols provided by
FontAwesome (and not many other fonts).
You do not need to restart i3 after making a change to the config.

# Bugs

Please report bugs and suggestions to the issues page.
Contributions are always welcome.
A common way a bug will manifest is that you will get no output in your bar.
If this happens, try running `python3 $SCRIPT_DIR/usb` from the command
line to get some insight into why nothing is displayed.
You will probably see a python stack trace.
Make sure to include this in your bug report, along with the output of any
other commands that you may think are relevant (the stack trace may contain
the exact system call that failed).
14 changes: 14 additions & 0 deletions usb/i3blocks.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,17 @@ command=$SCRIPT_DIR/usb
markup=pango
signal=1
interval=10
#IGNORE_LIST=[]
#IGNORE_LIST=["sdd1", "mapper/sda1_crypt"]
#INFO_TEXT_COLOR=white
#MOUNTED_COLOR=green
#PLUGGED_COLOR=gray
#LOCKED_COLOR=gray
#UNLOCKED_NOT_MOUNTED_COLOR=yellow
#PARTITIONLESS_COLOR=red
#PARTITIONLESS_TEXT=no partitions
#SEPARATOR=<span color='gray'> | </span>
#LOCKED_INDICATOR=
#UNLOCKED_INDICATOR=
#READONLY_INDICATOR=ro
#TRUNCATE_FS_LABELS=[not set by default, accepts +/- integers]
47 changes: 35 additions & 12 deletions usb/usb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
#
# i3blocks blocklet script to output connected usb storage device info.

import os

def _default(name, default='', arg_type=str):
val = default
if name in os.environ:
val = os.environ[name]
return arg_type(val)

###############################################################################
# BEGIN CONFIG
# Most of these can be specified as command line options, run with --help for
Expand All @@ -14,29 +22,41 @@
###############################################################################

# Color options, can be a color name or #RRGGBB
INFO_TEXT_COLOR = "white"
MOUNTED_COLOR = "green"
PLUGGED_COLOR = "gray"
LOCKED_COLOR = "gray"
UNLOCKED_NOT_MOUNTED_COLOR = "yellow"
PARTITIONLESS_COLOR = "red"
INFO_TEXT_COLOR = _default("INFO_TEXT_COLOR", "white")
MOUNTED_COLOR = _default("MOUNTED_COLOR", "green")
PLUGGED_COLOR = _default("PLUGGED_COLOR", "gray")
LOCKED_COLOR = _default("LOCKED_COLOR", "gray")
UNLOCKED_NOT_MOUNTED_COLOR = _default("UNLOCKED_NOT_MOUNTED_COLOR", "yellow")
PARTITIONLESS_COLOR = _default("PARTITIONLESS_COLOR", "red")

# Default texts
PARTITIONLESS_TEXT = "no partitions"
SEPARATOR = "<span color='gray'> | </span>"
PARTITIONLESS_TEXT = _default("PARTITIONLESS_TEXT", "no partitions")
SEPARATOR = _default("SEPARATOR", "<span color='gray'> | </span>")

# Indicate whether an encrypted partition is locked/unlocked, "" is allowed.
LOCKED_INDICATOR = "\uf023 "
UNLOCKED_INDICATOR = "\uf09c "
LOCKED_INDICATOR = _default("LOCKED_INDICATOR", "\uf023 ")
UNLOCKED_INDICATOR = _default("UNLOCKED_INDICATOR", "\uf09c ")

# Shows instead of space available when a partition is mounted readonly
READONLY_INDICATOR = "ro"
READONLY_INDICATOR = _default("READONLY_INDICATOR", "ro")

# Maximum length of a filesystem label to display. Use None to disable
# truncation, a positive integer to right truncate to that many characters, or
# a negative integer to left truncate to that many characters. Setting this
# option to 0 will disable the displaying of filesystem labels.
TRUNCATE_FS_LABELS = None
TRUNCATE_FS_LABELS = _default("TRUNCASE_FS_LABELS", None)

# List of devices to ignore. Must be a valid python3 representation of a list
# of strings
IGNORE_LIST = _default("IGNORE_LIST", "[]")
if IGNORE_LIST:
import ast
IGNORE_LIST = list(map(lambda p:
p if p.startswith("/")
else "/dev/{}".format(p),
ast.literal_eval(IGNORE_LIST)
))


# Edit this function to ignore certain devices (e.g. those that are always
# plugged in).
Expand All @@ -51,6 +71,9 @@ def ignore(path, udev_attributes_dict):
# Edit this function to ignore devices before the udev attributes are
# computed in order to save time and memory.
def fastIgnore(path):
if path in IGNORE_LIST:
return True

# E.g. how to to ignore devices whose path begins with /dev/sda
#if path.startswith("/dev/sda"):
# return True
Expand Down
1 change: 0 additions & 1 deletion usb/usb.py

This file was deleted.

0 comments on commit 444469b

Please sign in to comment.