/
fftabs.py
executable file
·117 lines (92 loc) · 3.03 KB
/
fftabs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
"""Firefox Tabs.
Usage:
fftabs.py -a | --all
fftabs.py -u | --url
fftabs.py -t | --title
fftabs.py (-f | --focus) ID
fftabs.py --count
fftabs.py (-c | -k | --close) ID...
fftabs.py -r | --raw
fftabs.py -h | --help
fftabs.py -V | --version
Options:
-a --all List all tabs in a readable format.
-u --url Show URLs.
-t --title Show titles.
-f --focus Focus on selected tab, where ID is the tab index.
--count Number of tabs.
-c -k --close Close (kill) selected tab, where ID is the tab index(es).
-r --raw List all tabs in raw format.
-h --help Show this screen.
-V --version Show version.
"""
INFO = """
A command line program for manipulating Firefox tabs.
Requirements:
* Firefox
* MozRepl add-on (https://addons.mozilla.org/en-US/firefox/addon/mozrepl/)
- activate the add-on (under Tools -> MozRepl, "Start" and "Activate on startup")
"""
__project__ = "Firefox Tabs"
__version__ = "0.0.1"
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
from docopt import docopt
from jabbapylib.browser import firefox as ff
def option_raw():
for e in ff.get_tab_list():
print e
def option_all():
li = ff.get_tab_list()
width = len(str(len(li)))
for d in li:
print "{index}: {url} ({title})".format(index=str(d['index']).rjust(width),
url=d['url'], title=d['title'])
def option_url():
li = ff.get_tab_list()
width = len(str(len(li)))
for d in li:
print "{index}: {url}".format(index=str(d['index']).rjust(width), url=d['url'])
def option_title():
li = ff.get_tab_list()
width = len(str(len(li)))
for d in li:
print "{index}: {title}".format(index=str(d['index']).rjust(width), title=d['title'])
def option_focus(args):
n = int(args['ID'][0])
ff.put_focus_on_tab(n)
def option_count():
print ff.get_number_of_tabs()
def option_close(args):
total = ff.get_number_of_tabs()
li = sorted([int(x) for x in args['ID'] if 0 <= int(x) < total])
for pos, val in enumerate(li):
# once a tab is closed, subsequent tabs are shifted one position to the left
ff.put_focus_on_tab(val - pos)
print '# closing {url} ({title})'.format(url=ff.get_curr_tab_url(),
title=ff.get_curr_tab_title())
ff.close_curr_tab()
def main(args):
if args['--raw']:
option_raw()
elif args['--all']:
option_all()
elif args['--url']:
option_url()
elif args['--title']:
option_title()
elif args['--focus']:
option_focus(args)
elif args['--count']:
option_count()
elif args['--close']:
option_close(args)
else:
print '# debug mode'
print args
#############################################################################
if __name__ == "__main__":
arguments = docopt(__doc__, version='{n} {v}'.format(n=__project__, v=__version__))
main(arguments)