forked from btobolaski/nvterm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvterm.py
More file actions
executable file
·84 lines (70 loc) · 2.83 KB
/
nvterm.py
File metadata and controls
executable file
·84 lines (70 loc) · 2.83 KB
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
#!/usr/bin/env python
import argparse
import os
import re
import sys
import fileinput
def getAllFiles(directory, fileExtention=".txt"):
fileList = []
for top, dirs, files in os.walk(directory):
for nm in files:
if nm.endswith(fileExtention):
fileList.append(nm)
return fileList
# this is to help with cross compatibility between platforms
def getHomeDir():
if re.match("linux.*", sys.platform) is not None:
return os.getenv("HOME")
def searchFileNames(fileList, searchString):
# Uses an unmatched list and a match list because once a file is matched, there is no
# reason to search it again.
unmatchedList = []
matchedList = fileList[0]
if fileList[0] == "":
matchedList = []
for filename in fileList[1]:
if re.match(".*{}.*".format(searchString), filename, flags=re.DOTALL) is not None:
matchedList.append(filename)
else:
unmatchedList.append(filename)
return [matchedList, unmatchedList]
def searchFileContents(directory, fileList, searchString):
unmatchedList = fileList[1]
matchedList = fileList[0]
if fileList[0] == "":
matchedList = []
fullpathnames = []
for filename in fileList[1]:
fullpathnames.append(os.path.join(directory, filename))
completeFiles = fileinput.input(fullpathnames)
for line in completeFiles:
if re.match(".*{}.*".format(searchString), line, flags=re.DOTALL) is not None:
matchedItem = re.sub(re.escape(directory + "/"), "", completeFiles.filename())
matchedList.append(matchedItem)
unmatchedList.remove(matchedItem)
completeFiles.nextfile()
return [matchedList, unmatchedList]
# Setup the argument list.
parser=argparse.ArgumentParser(description="search a Notes directory for a string")
# Optional argument to change from the default notes directory
parser.add_argument("-d, --directory", dest="directory", default="~/Dropbox/Notes",
help="The directory where the notes are stored (~/Dropbox/Notes by" +
"default) ")
parser.add_argument("-e, --editor", dest="editor", default="gvim", help="The editor to " +
"to use (the default is gvim)")
# Argument of what to search for
parser.add_argument('search_string', help="The string to search for")
args = parser.parse_args()
# Handle the ~ for home dir
args.directory = re.sub("~", getHomeDir(), args.directory)
# The format for files is [[Matched],[Unmatched]]
files = ["", getAllFiles(args.directory)]
files = searchFileNames(files, args.search_string)
files = searchFileContents(args.directory, files, args.search_string)
# open up the files with the editor
if files[0] is not "":
if type(files[0]) is list:
for filename in files[0]:
os.system('{} "{}"'.format(args.editor, os.path.join(args.directory, filename)))
else:
os.system('{} "{}"'.format(args.editor, os.path.join(args.directory,filename)))