This commit is contained in:
Paul Gauthier 2023-04-06 22:30:52 -07:00
parent 2d52a4833f
commit f3ff57c28a

View file

@ -24,28 +24,31 @@ You are an expert at understanding code and proposing code changes in response t
Ask any questions you need to fully understand the user's request. Ask any questions you need to fully understand the user's request.
DO NOT REPLACE ENTIRE FILES!
YOU MUST ONLY RETURN CODE USING THESE COMMANDS: YOU MUST ONLY RETURN CODE USING THESE COMMANDS:
- BEFORE/AFTER - BEFORE/AFTER
- DELETE - DELETE
- APPEND - APPEND
- PREPEND - PREPEND
** This is how to use the CHANGE command: ** This is how to replace lines from a file with a new set of lines.
BEFORE path/to/filename.ext BEFORE path/to/filename.ext
``` ```
... a series of lines from ... ... a series of lines from ...
... the original file ... ... the original file ...
... completely unchanged ... ... completely unchanged ...
... include only the sections of the file which need changes! ... ... include ONLY the sections of the file which need changes! ...
... don't include the entire before file! ... ... DO NOT USE THIS TO REPLACE AN ENTIRE FILES CONTENTS ...
``` ```
AFTER AFTER
``` ```
... the lines to replace them with ... ... all occurances of the before lines ...
... will get replaced with the after lines ...
``` ```
** This is how to use the DELETE command: ** This is how to remove lines from a file:
DELETE path/to/filename.ext DELETE path/to/filename.ext
``` ```
@ -55,7 +58,7 @@ DELETE path/to/filename.ext
... that will be deleted ... ... that will be deleted ...
``` ```
** This is how to use the APPEND command: ** This is how to append lines onto the end of a file:
APPEND path/to/filename.ext APPEND path/to/filename.ext
``` ```
@ -63,7 +66,7 @@ APPEND path/to/filename.ext
... at the end of the file ... ... at the end of the file ...
``` ```
** This is how to use the PREPEND command: ** This is how to insert lines at the start of a file:
PREPEND path/to/filename.ext PREPEND path/to/filename.ext
``` ```
@ -91,6 +94,13 @@ path/to/filename.ext
``` ```
''' '''
def find_index(list1, list2):
for i in range(len(list1)):
if list1[i:i+len(list2)] == list2:
return i
return -1
class Coder: class Coder:
fnames = [] fnames = []
def system(self, prompt): def system(self, prompt):
@ -226,7 +236,7 @@ class Coder:
pieces = lines[0].split() pieces = lines[0].split()
cmd = pieces[0] cmd = pieces[0]
if cmd not in ('BEFORE', 'AFTER'): if cmd not in ('BEFORE', 'AFTER', 'APPEND'):
raise ValueError(cmd) raise ValueError(cmd)
if len(pieces) > 1: if len(pieces) > 1:
@ -255,6 +265,22 @@ class Coder:
if cmd == 'BEFORE': if cmd == 'BEFORE':
after_op = ops.pop() after_op = ops.pop()
self.do_before(cmd, fname, op_lines, after_op) self.do_before(cmd, fname, op_lines, after_op)
continue
if cmd == 'APPEND':
self.do_append(cmd, fname, op_lines)
continue
def do_append(self, cmd, fname, op_lines):
if fname not in self.fnames:
raise ValueError(fname)
fname = Path(fname)
content = fname.read_text()
if content[-1] != '\n':
content += '\n'
content += '\n'.join(op_lines)
content += '\n'
fname.write_text(content)
def do_before(self, cmd, fname, op_lines, after_op): def do_before(self, cmd, fname, op_lines, after_op):
after_cmd,after_fname,after_lines = self.parse_op(after_op) after_cmd,after_fname,after_lines = self.parse_op(after_op)
@ -265,45 +291,35 @@ class Coder:
fname = Path(fname) fname = Path(fname)
content = fname.read_text() content = fname.read_text().splitlines()
before = '\n'.join(op_lines) before = [l.strip() for l in op_lines]
after = '\n'.join(after_lines) stripped_content = [l.strip() for l in content]
if before not in content: where = find_index(stripped_content, before)
if where < 0:
raise ValueError(before) raise ValueError(before)
content = content.replace(before, after) new_content = content[:where]
fname.write_text(content) new_content += after_lines
new_content += content[where+len(before):]
new_content = '\n'.join(new_content) + '\n'
fname.write_text(new_content)
coder = Coder() coder = Coder()
coder.file('../easy-chat/chat.css')
coder.update_files('''
BEFORE ../easy-chat/chat.css
```
.chat-box .fa-volume-up {
color: #4CAF50;
}
```
AFTER
```
.chat-box .fa-volume-up {
color: orange;
}
```
''')
sys.exit()
coder.system(prompt_webdev) coder.system(prompt_webdev)
for fname in sys.argv[1:]: for fname in sys.argv[1:]:
coder.file(Path(fname)) coder.file(Path(fname))
coder.run() coder.update_files(Path('tmp.commands').read_text()) ; sys.exit()
coder.run()
#dname = Path('../easy-chat') #dname = Path('../easy-chat')
#coder.file(dname / 'index.html') #coder.file(dname / 'index.html')