1
# -*- coding: utf-8 -*-
3
# Copyright (c) 2007 The PIDA Project
5
#Permission is hereby granted, free of charge, to any person obtaining a copy
6
#of this software and associated documentation files (the "Software"), to deal
7
#in the Software without restriction, including without limitation the rights
8
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
#copies of the Software, and to permit persons to whom the Software is
10
#furnished to do so, subject to the following conditions:
12
#The above copyright notice and this permission notice shall be included in
13
#all copies or substantial portions of the Software.
15
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
from kiwi.ui.objectlist import ObjectList, Column
32
from pida.core.service import Service
33
from pida.core.actions import ActionsConfig
34
from pida.core.actions import TYPE_TOGGLE
36
from pida.ui.views import PidaView
38
from pida.utils.gthreads import GeneratorSubprocessTask
41
from pida.core.locale import Locale
42
locale = Locale('man')
45
class ManItem(object):
47
def __init__(self, pattern, number, description, search):
48
self.pattern = pattern
50
self.description = self._color_match(cgi.escape(description), search)
51
patternmark = self._color_match(cgi.escape(pattern), search)
52
self.markup = '%s(<span color="#0000cc">%d</span>)' % (
53
patternmark, int(self.number))
55
def _color_match(self, data, match):
56
return data.replace(match, '<span color="#c00000"><b>%s</b></span>' % match)
59
class ManView(PidaView):
61
icon_name = 'gtk-library'
66
self.__vbox = gtk.VBox(spacing=3)
67
self.__vbox.set_border_width(6)
68
self.__hbox = gtk.HBox()
69
self.__entry = gtk.Entry()
70
self.__entry.connect('changed', self.cb_entry_changed)
71
self.__check = gtk.CheckButton(label='-k')
72
self.__check.connect('toggled', self.cb_entry_changed)
73
self.__list = ObjectList([
74
Column('markup', title=_('Man page'), sorted=True,
76
Column('description', title=_('Description'),
79
self.__list.connect('double-click', self._on_man_double_click)
80
self.__list.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
81
self.__hbox.pack_start(self.__entry)
82
self.__hbox.pack_start(self.__check, expand=False)
83
self.__vbox.pack_start(self.__hbox, expand=False)
84
self.__vbox.pack_start(self.__list)
85
self.add_main_widget(self.__vbox)
86
self.__vbox.show_all()
88
def clear_items(self):
92
def add_item(self, item):
94
self.__list.append(item)
96
def _on_man_double_click(self, olist, item):
97
commandargs = ['/usr/bin/env', 'man', item.number, item.pattern]
98
directory = os.path.dirname(commandargs[0])
99
self.svc.boss.cmd('commander', 'execute',
100
commandargs=commandargs,
102
icon='gnome-library',
103
title='%(pattern)s(%(number)d)' % dict(
104
pattern=item.pattern,
105
number=int(item.number)
108
def cb_entry_changed(self, w):
110
if self.__check.get_active():
112
self.svc.cmd_find(options=options, pattern=self.__entry.get_text())
114
def can_be_closed(self):
115
self.svc.get_action('show_man').set_active(False)
118
class ManActions(ActionsConfig):
120
def create_actions(self):
131
def on_show_man(self, action):
132
if action.get_active():
139
"""Show manpage of command"""
141
actions_config = ManActions
144
self._view = ManView(self)
145
self._has_loaded = False
149
self.boss.cmd('window', 'add_view', paned='Terminal', view=self._view)
150
if not self._has_loaded:
151
self._has_loaded = True
154
self.boss.cmd('window', 'remove_view', view=self._view)
156
def cmd_find(self, options, pattern):
158
# stop and clear task
161
self._view.clear_items()
163
# don't make empty search
164
if len(pattern) <= 0:
168
cmd = '/usr/bin/env man %s "%s"' % (options, pattern)
169
reman = re.compile('[(]([\d]+)[)]')
171
# match and add each line
173
list = reman.findall(result)
176
name = result.split('(')[0].strip()
177
res = result.split('- ',1)
179
# avoid too much result
180
if self._view._count > 100:
184
self._view.add_item(ManItem(name, list[0], res[1], pattern))
186
# launch man subprocess
187
self.task = GeneratorSubprocessTask(_line)
188
self.task.start(cmd, shell=True)
193
if self.get_action('show_man').get_active():
197
# Required Service attribute for service loading
202
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: