0
0
Fork 0

Started splitting up the program and normalizing some readline features

This commit is contained in:
sloum 2020-04-16 22:13:27 -07:00
parent afe2d4149c
commit a68f3ebd15
2 changed files with 54 additions and 60 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
test

113
chalk
View File

@ -38,46 +38,18 @@ def input_editable(prompt, prefill=''):
readline.set_startup_hook()
def validate_filename(filename):
if re.match(r'^[\w.\-]{1,100}', filename):
return True
return False
def validate_path(path):
if not path[0] in ['.','~','/']:
path = './{}'.format(path)
temp = path.split('/')
temp.pop()
temp = '/'.join(temp)
loc = subprocess.run(['ls',temp], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
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(' ',' ')
path_body_list = path.split('/')
path_body_list.pop()
path_body = '/'.join(path_body_list)
is_path = os.path.isdir(path_body)
if not is_path:
print("Invalid filepath")
os.exit(2)
def print_help():
helptext = [
"",
"",
"{}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),
@ -88,42 +60,61 @@ def chalk(path):
" {}!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),
" {}!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):
with open(valid_path,'r') as f:
def print_banner(fn):
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')
if content[-1] == '':
content.pop()
except FileNotFoundError:
content = []
filename = path.split('/')[-1]
edited_file = False
print('\n Chalk 0.8 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))
print(header)
print_banner(filename)
print_ruler()
while True:
ln = input('{:6} {}>{} '.format(len(content), c.yellow, c.end))
if ln == '.':
break
elif ln == '!?':
print('')
for x in helptext:
print('{:8} {}'.format(' ',x))
print('')
print(header)
print_help()
elif ln == '!g':
print('')
print(header)
print_ruler()
elif ln == '!d':
print('')
print('\n - - -')
for i, x in enumerate(content):
print('{:6} - {}{}{}'.format(i, c.green, x, c.end))
print('')
print(header)
print(' - - -\n')
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))
delete = input('{:6} {}>{} '.format(' ', c.b_red, c.end))
@ -179,7 +170,7 @@ def chalk(path):
for x in range(beg, end + 1):
print('{:6} - {}{}{}'.format(x, c.green, content[x], c.end))
print('')
print(header)
print_ruler()
except:
print('{}{:8} Invalid entry{}'.format(c.red, ' ', c.end))
@ -195,7 +186,7 @@ def chalk(path):
except:
print('{}{:8} Invalid entry!{}'.format(c.b_red, ' ', c.end))
print('')
print(header)
print_ruler()
else:
edited_file = True
content.append(ln)
@ -207,7 +198,7 @@ def chalk(path):
confirmation = ''
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):
continue
@ -217,11 +208,11 @@ def chalk(path):
text = '\n'.join(content)
text += '\n'
try:
with open(valid_path, 'w') as f:
with open(abspath, 'w') as f:
f.write(text)
print('Done.\n')
print('Saved \033[1m{}\033[0m'.format(abspath))
sys.exit(0)
except:
except PermissionError:
print('{} You do not have permission to write to this file.{}'.format(c.red, c.end))
sys.exit(1)
else:
@ -235,4 +226,6 @@ if __name__ == '__main__':
print('Incorrect number of arguments.')
sys.exit(1)
filepath = args[1]
readline.parse_and_bind('set editing-mode emacs')
readline.parse_and_bind('set show-mode-in-prompt off')
chalk(filepath)