0
0
Fork 0

Removes readline codes from regular print lines, adds file permission info

This commit is contained in:
sloum 2020-07-12 11:35:20 -07:00
parent f17bde0e15
commit 861ed38dc7
3 changed files with 53 additions and 29 deletions

View File

@ -19,6 +19,7 @@ Commands are entered as the only text on their row. Enter the command and press
|:-------:|:------------------------------------------------------------------------------:|
| .? | Print the command list |
| .a | Save AS (will prompt for new path) |
| .f | Print file info |
| .g | Print the GUIDE (ruler) |
| .d | DISPLAY the whole file |
| .v | VIEW a portion of the file |

81
chalk
View File

@ -39,19 +39,20 @@ class c:
# \001 and \002 fix a readline issue
# See: https://stackoverflow.com/questions/9468435/how-to-fix-column-calculation-in-python-readline-if-using-color-prompt
black = ''
red = '\001\033[0;31m\002'
b_red = '\001\033[1;31m\002'
yellow = '\001\033[1;33m\002'
green = '\001\033[0;32m\002'
b_green = '\001\033[1;32m\002'
cyan = '\001\033[0;36m\002'
b_cyan = '\001\033[1;36m\002'
purple = '\001\033[1;35m\002'
blue = '\001\033[0;34m\002'
b_blue = '\001\033[1;34m\002'
white = '\001\033[1;37m\002'
end = '\001\033[0m\002'
bold = '\001\033[1m\002'
red = '\033[0;31m'
b_red = '\033[1;31m'
yellow = '\033[1;33m'
green = '\033[0;32m'
b_green = '\033[1;32m'
cyan = '\033[0;36m'
b_cyan = '\033[1;36m'
purple = '\033[1;35m'
blue = '\033[0;34m'
b_blue = '\033[1;34m'
white = '\033[1;37m'
end = '\033[0m'
invert = '\033[7m'
bold = '\033[1m'
###########################################################
@ -101,6 +102,7 @@ def print_help():
"",
" {}.?{} - Print this help message".format(c.b_green, c.end),
" {}.g{} - Print the ruler/guide".format(c.b_green, c.end),
" {}.f{} - Print file info".format(c.b_green, c.end),
"",
" {}.d{} - Display the whole file".format(c.b_green, c.end),
" {}.v{} - View range of lines (will request location/count)".format(c.b_green, c.end),
@ -142,13 +144,32 @@ def print_ruler():
print(ticker)
def print_file_info():
user_can_write = os.access(filepath, os.W_OK)
print('{} Writing:{} {}{}'.format(c.yellow, c.white, filename, c.end))
print('{} Length :{} {} rows / {} chars{}'.format(c.yellow, c.white, len(content), get_character_count(), c.end))
if not user_can_write:
print('{} You do not have permission to write to {} >>{}'.format(c.bold, filename, c.end))
if file_changed:
print('{} File has unsaved changes!{}\n'.format(c.bold, c.end))
# Print banner will print the program name,
def print_banner(fn):
print('\n Chalk 1.0 by sloum')
print('\n{} Writing:{} {}{}'.format(c.yellow, c.white, fn, c.end))
def print_banner():
print('\n Chalk 2.0 by sloum\n')
print_file_info()
print(" For a command list, enter {}.?\n{}".format(c.green, c.end))
def get_character_count():
chars = 0
for row in content:
chars += len(row)
return chars
# Build contents from file sets the absolute
# file path, the file name, and loads in any
# content contained in a file
@ -197,11 +218,11 @@ def chalk(path):
build_contents_from_file(path)
print_banner(filename)
print_banner()
print_ruler()
while True:
ln = input('{:6} {}>{} '.format(len(content), c.yellow, c.end))
ln = input('{:6} \001{}\002>\001{}\002 '.format(len(content), c.yellow, c.end))
if ln == '.':
# End the editing session (quit)
# Will query for save if the file has been changed
@ -230,6 +251,8 @@ def command_router(ln):
copy_rows()
elif ln == '.d':
display_file()
elif ln == '.f':
print_file_info()
elif ln == '.g':
print_ruler()
elif ln == '.i':
@ -259,7 +282,7 @@ def edit_line(ln):
try:
row = int(ln[1:])
text = content[row]
newln = input_editable('{:6} {}>{} '.format(row, c.b_blue, c.end), content[row])
newln = input_editable('{:6} \001{}\002>\001{}\002 '.format(row, c.b_blue, c.end), content[row])
if newln != text:
content[row] = newln
file_changed = True
@ -300,7 +323,7 @@ def save_as():
global file_changed
print('{:9}{}Enter the new save path (can be relative):{}'.format(' ', c.cyan, c.end))
path = input('{:6} {}>{} '.format(' ', c.green, c.end))
path = input('{:6} \001{}\002>\001{}\002 '.format(' ', c.green, c.end))
continue_save_as = yes_no('{:9}{}Are you sure you want to save as {}? (y/n){} '.format(' ', c.cyan, path, c.end))
if not continue_save_as:
@ -351,10 +374,10 @@ def cut_lines():
global paste_buffer
print('{:9}{}Enter the line number you want to start deleting at:{}'.format(' ', c.cyan, c.end))
beg = input('{:6} {}>{} '.format(' ', c.b_red, c.end))
beg = input('{:6} \001{}\002>\001{}\002 '.format(' ', c.b_red, c.end))
print('{:9}{}Enter the last line number you want to delete (or $ for end of file){}:'.format(' ', c.cyan, c.end))
end = input('{:6} {}>{} '.format(' ', c.b_red, c.end))
end = input('{:6} \001{}\002>\001{}\002 '.format(' ', c.b_red, c.end))
continue_delete = yes_no('{:9}{}Are you sure you want to delete lines {} - {}? (y/n){} '.format(' ', c.cyan, beg, end, c.end))
if not continue_delete:
@ -385,10 +408,10 @@ def insert_lines():
global file_changed
print('{:9}{}Enter the line number you want to insert lines before:{}'.format(' ', c.cyan, c.end))
beg = input('{:6} {}>{} '.format(' ', c.b_green, c.end))
beg = input('{:6} \001{}\002>\001{}\002 '.format(' ', c.b_green, c.end))
print('{:9}{}Enter the number of rows you want to insert{}:'.format(' ', c.cyan, c.end))
count = input('{:6} {}>{} '.format(' ', c.b_green, c.end))
count = input('{:6} \001{}\002>\001{}\002 '.format(' ', c.b_green, c.end))
continue_insert = yes_no('{:9}{}Are you sure you want to insert {} rows before line {}? (y/n){} '.format(' ', c.cyan, count, beg, c.end))
if not continue_insert:
@ -413,10 +436,10 @@ def insert_lines():
def view_rows():
global view_loc
print('{:9}{}Enter the line number you want to start viewing from:{}'.format(' ', c.cyan, c.end))
start = input('{:6} {}>{} '.format(' ', c.yellow, c.end))
start = input('{:6} \001{}\002>\001{}\002 '.format(' ', c.yellow, c.end))
print('{:9}{}Enter the number of rows you want to view{}:'.format(' ', c.cyan, c.end))
count = input('{:6} {}>{} '.format(' ', c.yellow, c.end))
count = input('{:6} \001{}\002>\001{}\002 '.format(' ', c.yellow, c.end))
try:
beg = int(start)
@ -465,10 +488,10 @@ def copy_rows():
global paste_buffer
print('{:9}{}Enter the line number you want to start copying from:{}'.format(' ', c.cyan, c.end))
start = input('{:6} {}>{} '.format(' ', c.yellow, c.end))
start = input('{:6} \001{}\002>\001{}\002 '.format(' ', c.yellow, c.end))
print('{:9}{}Enter the last line you want to copy ($ for end of file):{}'.format(' ', c.cyan, c.end))
finish = input('{:6} {}>{} '.format(' ', c.yellow, c.end))
finish = input('{:6} \001{}\002>\001{}\002 '.format(' ', c.yellow, c.end))
if finish == '$':
finish = len(content) - 1
@ -491,7 +514,7 @@ def paste_from_buffer():
global file_changed
print('{:9}{}Enter a line number. The pasted data will be inserted {}before{} the given line:{}'.format(' ', c.cyan, '\033[4m', '\033[24m', c.end))
beg = input('{:6} {}>{} '.format(' ', c.b_green, c.end))
beg = input('{:6} \001{}\002>\001{}\002 '.format(' ', c.b_green, c.end))
continue_paste = yes_no('{:9}{}Are you sure you want to paste from the paste buffer before line {}? (y/n){} '.format(' ', c.cyan, beg, c.end))
if not continue_paste:

0
testperms Normal file
View File