0
0
Fork 0

Adds support for scratch buffer

This commit is contained in:
sloum 2020-07-12 15:39:12 -07:00
parent 861ed38dc7
commit 2eddcb6274
2 changed files with 22 additions and 10 deletions

31
chalk
View File

@ -27,8 +27,9 @@ content = []
paste_buffer = []
file_changed = False
filepath = ''
filename = ''
filename = 'scratch buffer'
view_loc = {'last': 0, 'count': 0}
version = 2.1
###########################################################
@ -106,7 +107,7 @@ def print_help():
"",
" {}.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),
" {}.m{} - View MORE (use after using !v to see more)".format(c.b_green, c.end),
" {}.m{} - View MORE (use after using .v to see more)".format(c.b_green, c.end),
"",
" {}.#{} - Edit a line (eg .27)".format(c.b_green, c.end),
" {}.i{} - Insert empty line(s) (will request location/count)".format(c.b_green, c.end),
@ -148,7 +149,9 @@ 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:
if not filepath:
print('{} You are in the scratch buffer. Use \033[3m.a\033[23m to save your work as a file{}'.format(c.bold, c.end))
elif not user_can_write:
print('{} You do not have permission to write to {} >>{}'.format(c.bold, filename, c.end))
if file_changed:
@ -158,7 +161,7 @@ def print_file_info():
# Print banner will print the program name,
def print_banner():
print('\n Chalk 2.0 by sloum\n')
print('\n Chalk {} by sloum\n'.format(version))
print_file_info()
print(" For a command list, enter {}.?\n{}".format(c.green, c.end))
@ -174,9 +177,15 @@ def get_character_count():
# file path, the file name, and loads in any
# content contained in a file
def build_contents_from_file(path):
# No path was given, just create an empty
# content buffer and it can be saved later
if not path:
return
global content
global filepath
global filename
path = os.path.abspath(path)
filepath = os.path.expanduser(path)
filename = filepath.split('/')[-1]
@ -213,7 +222,6 @@ def yes_no(question):
# Chalk is the entry point into the editor, contains the
# input loop and does some routing
def chalk(path):
global filename
global file_changed
build_contents_from_file(path)
@ -328,6 +336,7 @@ def save_as():
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:
print('{:9}Save canceled.'.format(' '))
return False
path = os.path.abspath(path)
fp = os.path.expanduser(path)
@ -335,11 +344,12 @@ def save_as():
valid = check_file_writable(fp)
if not valid:
print('{:9}{}The path is invalid, save cancelled{}'.format(' ', c.red, c.end))
return False
filepath = fp
filename = fn
file_changed = True
save_changes()
return save_changes()
# Quit will quit the program, but first will ask to save if there
@ -349,7 +359,7 @@ def quit():
sys.exit(0)
save = yes_no(' {}Save changes to {}?{} (Y/n) '.format(c.b_green, filename, c.end))
if save:
saved = save_changes()
saved = save_changes() if filepath else save_as()
if saved:
print(' File "{}" has been saved.'.format(filename))
sys.exit(0)
@ -552,11 +562,12 @@ def view_paste_buffer():
if __name__ == '__main__':
args = sys.argv
if len(args) != 2:
if len(args) > 2:
print('Incorrect number of arguments:')
print('chalk [file path]')
print('chalk [\033[3mfile path\033[0m]')
sys.exit(1)
filepath = args[1]
elif len(args) == 2:
filepath = args[1]
# Set readline settings
readline.parse_and_bind('set editing-mode emacs')

1
newtest.txt Normal file
View File

@ -0,0 +1 @@
This is a test