aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/data/plugins/progress_bar.py
diff options
context:
space:
mode:
authorGravatar Mason Larobina <mason.larobina@gmail.com>2010-04-04 04:44:03 +0800
committerGravatar Mason Larobina <mason.larobina@gmail.com>2010-04-04 04:44:03 +0800
commitd3cbe16bf16ff63c0e3db15d645e958712bd02d8 (patch)
tree6bea7bda62f07b73329fdb136a0b907661ca356c /examples/data/plugins/progress_bar.py
parentae15d257a858fe27090f3e5798357aea2f5a76da (diff)
Huge plugin & event manager upgrades.
1. Removed unused modules 2. Re-use event handlers with identical callbacks and args. 3. Removed plugin exceptions in favour of assertions. 4. Remove useless raw_keycmd and raw_bind config vars. 5. Implemented and use `after` and `cleanup` plugin hooks (correctly) 6. EM & plugins now use the python logging module to output messages 7. Null config items are removed automatically 8. Simpler mode plugin 9. The init plugins function is called after the INSTANCE_START event 10. New optparse option to silence event echoing to stdout 11. Close instance socket on INSTANCE_EXIT before event handling 12. Caught signals are logged 13. Show times on the messages in the log file 14. Refactor bind pluin to use uzbl.bindlet directly. 15. Refactor keycmd plugin to use uzbl.keycmd directly. 16. Refactored on_event plugin to use uzbl.on_events dict over UZBLS dict 17. Refactor completion plugin to use uzbl.completion set object. 18. Modified progress plugin to use config vars instead of `@progress k = v` 19. mode_config now a defaultdict(dict) (I.e. this allows you to `uzbl.mode_config[mode][var] = value` without needing to check `mode` is in the `uzbl.mode_config` dict). 20. Removed all default mode config values. 21. Removed all `get_mode()` and `set_mode(..)` functions (and the like). 22. Setting the mode is now done via the config object directly (I.e. `uzbl.config['mode'] = 'insert'`). 23. Uses the on_set plugin to watch for 'mode' and 'default_mode' config changes. 24. Don't raise the useless NEW_ON_SET event, missing ON_SET connect. 25. Plugin and EventHandler aren't suited as dict objects. 26. Also using collections.defaultdict(list) for uzbl.handlers dict. 27. Plugin `on_set.py` allows you to attach handlers to config var changes 28. Config plugin reduced to one `uzbl.config` dict-like object. 29. Update export and connect calls in plugins. 30. The functions connect, connect_dict, export, export_dict, require, logging are exported directly to the plugin namespace. 31. Moved parse_msg into Uzbl class. 32. Generally improved comments. 33. UzblEventDaemon now an object. 34. Various variable, function & class renames.
Diffstat (limited to 'examples/data/plugins/progress_bar.py')
-rw-r--r--examples/data/plugins/progress_bar.py152
1 files changed, 43 insertions, 109 deletions
diff --git a/examples/data/plugins/progress_bar.py b/examples/data/plugins/progress_bar.py
index 89ba175..b2edffc 100644
--- a/examples/data/plugins/progress_bar.py
+++ b/examples/data/plugins/progress_bar.py
@@ -1,39 +1,7 @@
-import sys
+UPDATES = 0
-UZBLS = {}
-
-DEFAULTS = {'width': 8,
- 'done': '=',
- 'pending': '.',
- 'format': '[%d%a%p]%c',
- 'spinner': '-\\|/',
- 'sprites': 'loading',
- 'updates': 0,
- 'progress': 100}
-
-
-def error(msg):
- sys.stderr.write("progress_bar plugin: error: %s\n" % msg)
-
-
-def add_instance(uzbl, *args):
- UZBLS[uzbl] = dict(DEFAULTS)
-
-
-def del_instance(uzbl, *args):
- if uzbl in UZBLS:
- del UZBLS[uzbl]
-
-
-def get_progress_config(uzbl):
- if uzbl not in UZBLS:
- add_instance(uzbl)
-
- return UZBLS[uzbl]
-
-
-def update_progress(uzbl, prog=None):
- '''Updates the progress_format variable on LOAD_PROGRESS update.
+def update_progress(uzbl, progress=None):
+ '''Updates the progress.output variable on LOAD_PROGRESS update.
The current substitution options are:
%d = done char * done
@@ -44,116 +12,82 @@ def update_progress(uzbl, prog=None):
%t = percent pending
%o = int pending
%r = sprites
+
+ Default configuration options:
+ progress.format = [%d>%p]%c
+ progress.width = 8
+ progress.done = =
+ progress.pending =
+ progress.spinner = -\|/
+ progress.sprites = loading
'''
- prog_config = get_progress_config(uzbl)
- config = uzbl.get_config()
+ global UPDATES
- if prog is None:
- prog = prog_config['progress']
+ if progress is None:
+ UPDATES = 0
+ progress = 100
else:
- prog = int(prog)
- prog_config['progress'] = prog
+ UPDATES += 1
+ progress = int(progress)
- prog_config['updates'] += 1
- format = prog_config['format']
- width = prog_config['width']
+ # Get progress config vars.
+ format = uzbl.config.get('progress.format', '[%d>%p]%c')
+ width = int(uzbl.config.get('progress.width', 8))
+ done_symbol = uzbl.config.get('progress.done', '=')
+ pend = uzbl.config.get('progress.pending', None)
+ pending_symbol = pend if pend else ' '
# Inflate the done and pending bars to stop the progress bar
# jumping around.
if '%c' in format or '%i' in format:
count = format.count('%c') + format.count('%i')
- width += (3-len(str(prog))) * count
+ width += (3-len(str(progress))) * count
if '%t' in format or '%o' in format:
count = format.count('%t') + format.count('%o')
- width += (3-len(str(100-prog))) * count
+ width += (3-len(str(100-progress))) * count
- done = int(((prog/100.0)*width)+0.5)
+ done = int(((progress/100.0)*width)+0.5)
pending = width - done
if '%d' in format:
- format = format.replace('%d', prog_config['done']*done)
+ format = format.replace('%d', done_symbol * done)
if '%p' in format:
- format = format.replace('%p', prog_config['pending']*pending)
+ format = format.replace('%p', pending_symbol * pending)
if '%c' in format:
- format = format.replace('%c', '%d%%' % prog)
+ format = format.replace('%c', '%d%%' % progress)
if '%i' in format:
- format = format.replace('%i', '%d' % prog)
+ format = format.replace('%i', '%d' % progress)
if '%t' in format:
- format = format.replace('%t', '%d%%' % (100-prog))
+ format = format.replace('%t', '%d%%' % (100-progress))
if '%o' in format:
- format = format.replace('%o', '%d' % (100-prog))
+ format = format.replace('%o', '%d' % (100-progress))
if '%s' in format:
- spinner = prog_config['spinner']
- spin = '-' if not spinner else spinner
- index = 0 if prog == 100 else prog_config['updates'] % len(spin)
- char = '\\\\' if spin[index] == '\\' else spin[index]
- format = format.replace('%s', char)
+ spinner = uzbl.config.get('progress.spinner', '-\\|/')
+ index = 0 if progress == 100 else UPDATES % len(spinner)
+ spin = '\\\\' if spinner[index] == '\\' else spinner[index]
+ format = format.replace('%s', spin)
if '%r' in format:
- sprites = prog_config['sprites']
- sprites = '-' if not sprites else sprites
- index = int(((prog/100.0)*len(sprites))+0.5)-1
+ sprites = uzbl.config.get('progress.sprites', 'loading')
+ index = int(((progress/100.0)*len(sprites))+0.5)-1
sprite = '\\\\' if sprites[index] == '\\' else sprites[index]
format = format.replace('%r', sprite)
- if 'progress_format' not in config or config['progress_format'] != format:
- config['progress_format'] = format
-
-
-def progress_config(uzbl, args):
- '''Parse PROGRESS_CONFIG events from the uzbl instance.
-
- Syntax: event PROGRESS_CONFIG <key> = <value>
- '''
-
- split = args.split('=', 1)
- if len(split) != 2:
- return error("invalid syntax: %r" % args)
-
- key, value = map(unicode.strip, split)
- prog_config = get_progress_config(uzbl)
-
- if key not in prog_config:
- return error("key error: %r" % args)
-
- if type(prog_config[key]) == type(1):
- try:
- value = int(value)
-
- except:
- return error("invalid type: %r" % args)
-
- elif not value:
- value = ' '
-
- prog_config[key] = value
- update_progress(uzbl)
-
-
-def reset_progress(uzbl, args):
- '''Reset the spinner counter, reset the progress int and re-draw the
- progress bar on LOAD_COMMIT.'''
-
- prog_dict = get_progress_config(uzbl)
- prog_dict['updates'] = prog_dict['progress'] = 0
- update_progress(uzbl)
-
+ if uzbl.config.get('progress.output', None) != format:
+ uzbl.config['progress.output'] = format
+# plugin init hook
def init(uzbl):
- # Event handling hooks.
- uzbl.connect_dict({
- 'INSTANCE_EXIT': del_instance,
- 'INSTANCE_START': add_instance,
- 'LOAD_COMMIT': reset_progress,
+ connect_dict(uzbl, {
+ 'LOAD_COMMIT': lambda uzbl, uri: update_progress(uzbl),
'LOAD_PROGRESS': update_progress,
- 'PROGRESS_CONFIG': progress_config,
})