Started splitting up the program and normalizing some readline features
This commit is contained in:
parent
afe2d4149c
commit
a68f3ebd15
|
@ -0,0 +1 @@
|
||||||
|
test
|
113
chalk
113
chalk
|
@ -38,46 +38,18 @@ def input_editable(prompt, prefill=''):
|
||||||
readline.set_startup_hook()
|
readline.set_startup_hook()
|
||||||
|
|
||||||
|
|
||||||
def validate_filename(filename):
|
|
||||||
if re.match(r'^[\w.\-]{1,100}', filename):
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def validate_path(path):
|
def validate_path(path):
|
||||||
if not path[0] in ['.','~','/']:
|
path_body_list = path.split('/')
|
||||||
path = './{}'.format(path)
|
path_body_list.pop()
|
||||||
temp = path.split('/')
|
path_body = '/'.join(path_body_list)
|
||||||
temp.pop()
|
is_path = os.path.isdir(path_body)
|
||||||
temp = '/'.join(temp)
|
if not is_path:
|
||||||
loc = subprocess.run(['ls',temp], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
print("Invalid filepath")
|
||||||
|
os.exit(2)
|
||||||
if loc.returncode > 0:
|
|
||||||
return False
|
|
||||||
|
|
||||||
if temp[-1] == '/':
|
|
||||||
temp = temp[:-1]
|
|
||||||
if filepath:
|
|
||||||
temp = temp + '/' + path.split('/')[-1]
|
|
||||||
return temp
|
|
||||||
|
|
||||||
|
|
||||||
def chalk(path):
|
|
||||||
fn = path.split('/')[-1]
|
|
||||||
valid_fn = validate_filename(fn)
|
|
||||||
|
|
||||||
if not valid_fn:
|
|
||||||
print('Invalid filename')
|
|
||||||
sys.exit(2)
|
|
||||||
|
|
||||||
valid_path = validate_path(path)
|
|
||||||
if not valid_path:
|
|
||||||
print('Invalid path')
|
|
||||||
sys.exit(2)
|
|
||||||
|
|
||||||
header = "{:7} 5 10 15 20 25 30 35 40 45 50 55 60 65\n{:7} ....|....|....|....|....|....|....|....|....|....|....|....|....|".format(' ',' ')
|
|
||||||
|
|
||||||
|
def print_help():
|
||||||
helptext = [
|
helptext = [
|
||||||
|
"",
|
||||||
"",
|
"",
|
||||||
"{}Commands are entered as the only entry for their row:{}".format(c.yellow, c.end),
|
"{}Commands are entered as the only entry for their row:{}".format(c.yellow, c.end),
|
||||||
" {}.{} - Finish writing/exit, will prompt for save".format(c.b_green, c.end),
|
" {}.{} - Finish writing/exit, will prompt for save".format(c.b_green, c.end),
|
||||||
|
@ -88,42 +60,61 @@ def chalk(path):
|
||||||
" {}!x{} - Delete line(s), prompt will request line or range".format(c.b_green, c.end),
|
" {}!x{} - Delete line(s), prompt will request line or range".format(c.b_green, c.end),
|
||||||
" {}!i{} - Insert line(s), prompt will request start point and optional quantity".format(c.b_green, c.end),
|
" {}!i{} - Insert line(s), prompt will request start point and optional quantity".format(c.b_green, c.end),
|
||||||
" {}!g{} - Print the ruler/guide".format(c.b_green, c.end),
|
" {}!g{} - Print the ruler/guide".format(c.b_green, c.end),
|
||||||
|
"",
|
||||||
""
|
""
|
||||||
]
|
]
|
||||||
|
for x in helptext:
|
||||||
|
print('{:8} {}'.format(' ',x))
|
||||||
|
|
||||||
content = []
|
def print_ruler():
|
||||||
|
width = os.get_terminal_size()[0] - 9
|
||||||
|
counter = " "
|
||||||
|
ticker = " "
|
||||||
|
current = 5
|
||||||
|
while current < width - 5:
|
||||||
|
counter += "{:5}".format(current)
|
||||||
|
ticker += "....|"
|
||||||
|
current += 5
|
||||||
|
print(counter)
|
||||||
|
print(ticker)
|
||||||
|
|
||||||
if os.path.exists(valid_path):
|
def print_banner(fn):
|
||||||
with open(valid_path,'r') as f:
|
print('\n Chalk 1.0 by sloum')
|
||||||
|
print('\n{} Writing:{} {}{}'.format(c.yellow, c.white, fn, c.end))
|
||||||
|
print(" For a command list, enter {}!?\n{}".format(c.green, c.end))
|
||||||
|
|
||||||
|
|
||||||
|
def chalk(path):
|
||||||
|
abspath = os.path.abspath(path)
|
||||||
|
try:
|
||||||
|
validate_path(abspath)
|
||||||
|
with open(os.path.abspath(abspath), 'r') as f:
|
||||||
content = f.read().split('\n')
|
content = f.read().split('\n')
|
||||||
if content[-1] == '':
|
if content[-1] == '':
|
||||||
content.pop()
|
content.pop()
|
||||||
|
except FileNotFoundError:
|
||||||
|
content = []
|
||||||
|
|
||||||
|
filename = path.split('/')[-1]
|
||||||
|
|
||||||
edited_file = False
|
edited_file = False
|
||||||
|
|
||||||
print('\n Chalk 0.8 by sloum')
|
print_banner(filename)
|
||||||
print('\n{} Writing:{} {}{}'.format(c.yellow, c.white, fn, c.end))
|
print_ruler()
|
||||||
print(" For a command list, enter {}!?\n{}".format(c.green, c.end))
|
|
||||||
print(header)
|
|
||||||
while True:
|
while True:
|
||||||
ln = input('{:6} {}>{} '.format(len(content), c.yellow, c.end))
|
ln = input('{:6} {}>{} '.format(len(content), c.yellow, c.end))
|
||||||
if ln == '.':
|
if ln == '.':
|
||||||
break
|
break
|
||||||
elif ln == '!?':
|
elif ln == '!?':
|
||||||
print('')
|
print_help()
|
||||||
for x in helptext:
|
|
||||||
print('{:8} {}'.format(' ',x))
|
|
||||||
print('')
|
|
||||||
print(header)
|
|
||||||
elif ln == '!g':
|
elif ln == '!g':
|
||||||
print('')
|
print_ruler()
|
||||||
print(header)
|
|
||||||
elif ln == '!d':
|
elif ln == '!d':
|
||||||
print('')
|
print('\n - - -')
|
||||||
for i, x in enumerate(content):
|
for i, x in enumerate(content):
|
||||||
print('{:6} - {}{}{}'.format(i, c.green, x, c.end))
|
print('{:6} - {}{}{}'.format(i, c.green, x, c.end))
|
||||||
print('')
|
print(' - - -\n')
|
||||||
print(header)
|
|
||||||
elif ln == '!x':
|
elif ln == '!x':
|
||||||
print('\n{:8} {}Enter line to delete, for a range enter the start and\n{:8} end separated by a space. To cancel, enter -1.\n{:8} Deletion cannot be undone. Be careful.{}'.format(' ', c.cyan, ' ', ' ', c.end))
|
print('\n{:8} {}Enter line to delete, for a range enter the start and\n{:8} end separated by a space. To cancel, enter -1.\n{:8} Deletion cannot be undone. Be careful.{}'.format(' ', c.cyan, ' ', ' ', c.end))
|
||||||
delete = input('{:6} {}>{} '.format(' ', c.b_red, c.end))
|
delete = input('{:6} {}>{} '.format(' ', c.b_red, c.end))
|
||||||
|
@ -179,7 +170,7 @@ def chalk(path):
|
||||||
for x in range(beg, end + 1):
|
for x in range(beg, end + 1):
|
||||||
print('{:6} - {}{}{}'.format(x, c.green, content[x], c.end))
|
print('{:6} - {}{}{}'.format(x, c.green, content[x], c.end))
|
||||||
print('')
|
print('')
|
||||||
print(header)
|
print_ruler()
|
||||||
|
|
||||||
except:
|
except:
|
||||||
print('{}{:8} Invalid entry{}'.format(c.red, ' ', c.end))
|
print('{}{:8} Invalid entry{}'.format(c.red, ' ', c.end))
|
||||||
|
@ -195,7 +186,7 @@ def chalk(path):
|
||||||
except:
|
except:
|
||||||
print('{}{:8} Invalid entry!{}'.format(c.b_red, ' ', c.end))
|
print('{}{:8} Invalid entry!{}'.format(c.b_red, ' ', c.end))
|
||||||
print('')
|
print('')
|
||||||
print(header)
|
print_ruler()
|
||||||
else:
|
else:
|
||||||
edited_file = True
|
edited_file = True
|
||||||
content.append(ln)
|
content.append(ln)
|
||||||
|
@ -207,7 +198,7 @@ def chalk(path):
|
||||||
|
|
||||||
confirmation = ''
|
confirmation = ''
|
||||||
while confirmation.lower() not in ['y','yes','n','no']:
|
while confirmation.lower() not in ['y','yes','n','no']:
|
||||||
confirmation = input('{}Save {}?{} (Y/n) '.format(c.b_green, fn, c.end))
|
confirmation = input('{}Save {}?{} (Y/n) '.format(c.b_green, filename, c.end))
|
||||||
|
|
||||||
if not len(confirmation):
|
if not len(confirmation):
|
||||||
continue
|
continue
|
||||||
|
@ -217,11 +208,11 @@ def chalk(path):
|
||||||
text = '\n'.join(content)
|
text = '\n'.join(content)
|
||||||
text += '\n'
|
text += '\n'
|
||||||
try:
|
try:
|
||||||
with open(valid_path, 'w') as f:
|
with open(abspath, 'w') as f:
|
||||||
f.write(text)
|
f.write(text)
|
||||||
print('Done.\n')
|
print('Saved \033[1m{}\033[0m'.format(abspath))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
except:
|
except PermissionError:
|
||||||
print('{} You do not have permission to write to this file.{}'.format(c.red, c.end))
|
print('{} You do not have permission to write to this file.{}'.format(c.red, c.end))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
|
@ -235,4 +226,6 @@ if __name__ == '__main__':
|
||||||
print('Incorrect number of arguments.')
|
print('Incorrect number of arguments.')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
filepath = args[1]
|
filepath = args[1]
|
||||||
|
readline.parse_and_bind('set editing-mode emacs')
|
||||||
|
readline.parse_and_bind('set show-mode-in-prompt off')
|
||||||
chalk(filepath)
|
chalk(filepath)
|
||||||
|
|
Loading…
Reference in New Issue