forked from crftwr/cfiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfiler_commandline.py
223 lines (166 loc) · 6.54 KB
/
cfiler_commandline.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import os
import inspect
import ckit
from ckit.ckit_const import *
import cfiler_misc
import cfiler_native
import cfiler_error
import cfiler_debug
## @addtogroup commandline
## @{
#--------------------------------------------------------------------
## コマンドラインからのコマンド実行機能
class commandline_Launcher:
def __init__( self, main_window ):
self.main_window = main_window
self.command_list = []
def onCandidate( self, update_info ):
pane = self.main_window.activePane()
basedir = pane.file_list.getLocation()
left = update_info.text[ : update_info.selection[0] ]
left_lower = left.lower()
pos_arg = left.rfind(";")+1
arg = left[ pos_arg : ]
pos_dir = max( arg.rfind("/")+1, arg.rfind("\\")+1 )
directory = arg[:pos_dir]
directory_lower = directory.lower()
name_prefix = arg[pos_dir:].lower()
dirname_list = []
filename_list = []
candidate_list = []
if len(arg)>0:
try:
path = ckit.joinPath( basedir, directory )
drive, tail = os.path.splitdrive(path)
unc = ( drive.startswith("\\\\") or drive.startswith("//") )
if unc:
cfiler_misc.checkNetConnection(path)
if unc and not tail:
servername = drive.replace('/','\\')
infolist = cfiler_native.enumShare(servername)
for info in infolist:
if info[1]==0:
if info[0].lower().startswith(name_prefix):
if ckit.pathSlash():
dirname_list.append( info[0] + "/" )
else:
dirname_list.append( info[0] + "\\" )
else:
infolist = cfiler_native.findFile( ckit.joinPath(path,'*'), use_cache=True )
for info in infolist:
if info[0].lower().startswith(name_prefix):
if info[3] & ckit.FILE_ATTRIBUTE_DIRECTORY:
if ckit.pathSlash():
dirname_list.append( info[0] + "/" )
else:
dirname_list.append( info[0] + "\\" )
else:
filename_list.append( info[0] )
except Exception:
cfiler_debug.printErrorInfo()
for item in self.command_list:
item_lower = item[0].lower()
if item_lower.startswith(left_lower) and len(item_lower)!=len(left_lower):
candidate_list.append( item[0] )
for item in self.main_window.enumCommand():
item_lower = item.lower()
if item_lower.startswith(left_lower) and len(item_lower)!=len(left_lower):
candidate_list.append( item )
return dirname_list + filename_list + candidate_list
def onEnter( self, commandline, text, mod ):
args = text.split(';')
for i in range(len(args)):
args[i] = args[i].strip()
command_name = args[0].lower()
def found(command):
info = ckit.CommandInfo()
info.args = args[1:]
info.mod = mod
commandline.planCommand( command, info, text )
commandline.quit()
for command in self.command_list:
if command[0].lower() == command_name:
found(command[1])
return True
for command in self.main_window.enumCommand():
if command.lower() == command_name:
found( getattr(self.main_window.command, command) )
return True
return False
def onStatusString( self, text ):
text_lower = text.lower()
for command in self.command_list:
if command[0].lower() == text_lower:
return "OK"
return None
## コマンドラインでの計算機能
class commandline_Calculator:
def __init__(self):
pass
def onCandidate( self, update_info ):
return []
def onEnter( self, commandline, text, mod ):
from math import sin, cos, tan, acos, asin, atan
from math import e, fabs, log, log10, pi, pow, sqrt
try:
result = eval(text)
except (SyntaxError,TypeError):
return False
if isinstance(result,int):
result_string = "%d" % result
elif isinstance(result,float):
result_string = "%f" % result
else:
return False
commandline.appendHistory( text )
if result_string!=text:
print( "%s => %s" % ( text, result_string ) )
commandline.setText( result_string )
commandline.selectAll()
return True
def onStatusString( self, text ):
return None
## コマンドラインでの 10進 <-> 16進 変換機能
class commandline_Int32Hex:
def __init__(self):
pass
def onCandidate( self, update_info ):
return []
def onEnter( self, commandline, text, mod ):
def base16to10(src):
if src[:2].lower() != "0x":
raise ValueError
i = int( src, 16 )
if i<0 or i>0xffffffff:
raise ValueError
if i>=0x80000000:
dst = "%d" % ( -0x80000000 + ( i - 0x80000000 ) )
else:
dst = "%d" % i
return dst
def base10to16(src):
i = int( src, 10 )
if i<-0x80000000 or i>=0x80000000:
raise ValueError
if i<0:
dst = "0x%08x" % ( 0x80000000 + ( i + 0x80000000 ) )
else:
dst = "0x%08x" % i
return dst
text = text.strip()
try:
result_string = base10to16(text)
except ValueError:
try:
result_string = base16to10(text)
except ValueError:
return False
commandline.appendHistory( text )
if result_string!=text:
print( "%s => %s" % ( text, result_string ) )
commandline.setText( result_string )
commandline.selectAll()
return True
def onStatusString( self, text ):
return None
## @} commandline