Skip to content
This repository was archived by the owner on Sep 12, 2022. It is now read-only.

Commit 18f791d

Browse files
committed
hooker support on nodes
1 parent 0386b0f commit 18f791d

File tree

4 files changed

+74
-26
lines changed

4 files changed

+74
-26
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,24 @@ alias palette='for i in {0..255}; do echo -e "\e[38;05;${i}m${i}"; done | column
156156
```
157157

158158

159+
### Writing plugin for vim-netranger
160+
vim-netranger will expose some api so that users can write (python) code to customize the appearance of vim-netranger. An example plugin is [netranger-diricon](https://github.com/ipod825/netranger-diricon), which shows a small icon indicating whether a directory is expanded or not. Generally, in you `plugin/YOURPLUGIN.vim` file, you'll have the following boilplate code:
161+
```vim
162+
let s:pyx = 'python3 '
163+
exec s:pyx 'from netranger.hooker import RegisterHooker'
164+
exec s:pyx 'from YOURPLUGIN.YOURPLUGIN import node_highlight_content_l'
165+
exec s:pyx 'RegisterHooker(node_highlight_content_l)'
166+
```
167+
Your registered function name must be a valid vim-netranger api.
168+
169+
159170
## Known Issues
160171
1. In neovim, when opening two vim buffers for the same directory, there is a delay for moving cursor up and down. This seems to be an nvim api [issue](https://github.com/neovim/neovim/issues/7756)
161172
2. When remote directory is empty, it will not be copied to remote. It is an rclone [bug] (https://github.com/ncw/rclone/issues/1837), which is expected to be fixed in next release.
162173

163174

175+
176+
164177
## Contributing
165178
Pull request is welcomed. However, please run tests and coding style check before sending pull request.
166179

pythonx/netranger/hooker.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Hookers = {
2+
'node_highlight_content_l': [],
3+
'node_highlight_content_r': [],
4+
}
5+
6+
7+
def RegisterHooker(hooker):
8+
Hookers[hooker.__name__].append(hooker)
9+
10+
11+
def has_hooker(*hooker_names):
12+
for name in hooker_names:
13+
if len(Hookers[name]) > 0:
14+
return True
15+
return False

pythonx/netranger/netranger.py

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import fnmatch
55
import datetime
66
from netranger.fs import FS, Rclone
7-
from netranger.util import log, Shell
7+
from netranger.util import log, Shell, c256
88
from netranger import default
99
from netranger.colortbl import colortbl
1010
from netranger.ui import BookMarkUI, HelpUI, SortUI, AskUI
@@ -13,6 +13,7 @@
1313
from netranger.enum import Enum
1414
from collections import defaultdict
1515
from netranger.config import file_sz_display_wid
16+
from netranger.hooker import Hookers, has_hooker
1617

1718
from sys import platform
1819
if platform == "win32":
@@ -38,18 +39,16 @@ def __init__(self, fullpath, name, highlight, level=0):
3839
self.set_highlight(highlight)
3940
self.level = level
4041
self.state = Node.State.NORMAL
42+
self.is_cursor_on = False
4143

42-
def set_highlight(self, highlight, cursor_on=False):
44+
def set_highlight(self, highlight):
4345
if type(highlight) is str:
4446
highlight = colortbl[highlight]
45-
if cursor_on:
46-
self.highlight = '[38;5;{};7'.format(highlight)
47-
else:
48-
self.highlight = '[38;5;{}'.format(highlight)
47+
self.highlight = highlight
4948

5049
@property
5150
def highlight_content(self):
52-
return '{}m{}[0m'.format(self.highlight, self.name)
51+
return c256(self.name, self.highlight, self.is_cursor_on)
5352

5453
@property
5554
def isDir(self):
@@ -60,10 +59,10 @@ def isHeader(self):
6059
return False
6160

6261
def cursor_on(self):
63-
pass
62+
self.is_cursor_on = True
6463

6564
def cursor_off(self):
66-
pass
65+
self.is_cursor_on = False
6766

6867
def toggle_pick(self):
6968
return Node.ToggleOpRes.INVALID
@@ -77,6 +76,10 @@ def __init__(self, fullpath):
7776
def re_stat(self, fs=None):
7877
self.stat = os.stat(self.fullpath)
7978

79+
@property
80+
def highlight_content(self):
81+
return c256(self.name, self.highlight, False)
82+
8083
@property
8184
def isHeader(self):
8285
return True
@@ -111,10 +114,32 @@ def highlight_content(self):
111114

112115
left = levelPad
113116
right = size_info
114-
return '{}m{}{}{}'.format(self.highlight,
115-
left,
116-
self.abbrev_name(width-len(left)-len(right)),
117-
right).strip()
117+
118+
def c(msg):
119+
return c256(msg, self.highlight, self.is_cursor_on)
120+
121+
if has_hooker('node_highlight_content_l', 'node_highlight_content_r'):
122+
left_extra = ''
123+
left_extra_len = 0
124+
for hooker in Hookers['node_highlight_content_l']:
125+
l_s, l_h = hooker(self)
126+
left_extra_len += len(l_s)
127+
left_extra += c256(l_s, l_h, False)
128+
129+
right_extra = ''
130+
right_extra_len = 0
131+
for hooker in Hookers['node_highlight_content_r']:
132+
r_s, r_h = hooker(self)
133+
right_extra_len += len(r_s)
134+
right_extra += c256(r_s, r_h, False)
135+
136+
return c(left) +\
137+
left_extra +\
138+
c(self.abbrev_name(width-len(left)-len(right)-left_extra_len-right_extra_len)) +\
139+
c(right) +\
140+
right_extra
141+
else:
142+
return c('{}{}{}'.format(left, self.abbrev_name(width-len(left)-len(right)), right))
118143

119144
def __init__(self, fullpath, name, fs, level=0):
120145
self.fullpath = fullpath
@@ -170,18 +195,6 @@ def decide_hi(self):
170195
else:
171196
return default.color['file']
172197

173-
def cursor_on(self):
174-
hiArr = self.highlight.split(';')
175-
if len(hiArr) == 3:
176-
hiArr.append('7')
177-
self.highlight = ';'.join(hiArr)
178-
179-
def cursor_off(self):
180-
hiArr = self.highlight.split(';')
181-
if len(hiArr) == 4:
182-
hiArr = hiArr[:-1]
183-
self.highlight = ';'.join(hiArr)
184-
185198
def rename(self, name):
186199
ori = self.fullpath
187200
dirname = os.path.dirname(self.fullpath)
@@ -195,7 +208,7 @@ def change_dirname(self, oridirname, dirname):
195208
def toggle_pick(self):
196209
if self.state == Node.State.NORMAL:
197210
self.state = Node.State.PICKED
198-
self.set_highlight(default.color['pick'], cursor_on=True)
211+
self.set_highlight(default.color['pick'])
199212
return Node.ToggleOpRes.ON
200213
elif self.state == Node.State.PICKED:
201214
self.state = Node.State.NORMAL

pythonx/netranger/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,10 @@ def urldownload(cls, url, dst):
114114
hstream = urllib.urlopen(url)
115115
with open(dst, 'wb') as f:
116116
f.write(hstream.read())
117+
118+
119+
def c256(msg, c, background):
120+
if background:
121+
return '[38;5;{};7m{}'.format(c, msg)
122+
else:
123+
return '[38;5;{}m{}'.format(c, msg)

0 commit comments

Comments
 (0)