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.
DO NOT REPLACE ENTIRE FILES!
YOU MUST ONLY RETURN CODE USING THESE COMMANDS:
- BEFORE/AFTER
- DELETE
- APPEND
- 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
```
... a series of lines from ...
... the original file ...
... completely unchanged ...
... include only the sections of the file which need changes! ...
... don't include the entire before file! ...
... include ONLY the sections of the file which need changes! ...
... DO NOT USE THIS TO REPLACE AN ENTIRE FILES CONTENTS ...
```
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
```
@ -55,7 +58,7 @@ DELETE path/to/filename.ext
... 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
```
@ -63,7 +66,7 @@ APPEND path/to/filename.ext
... 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
```
@ -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:
fnames = []
def system(self, prompt):
@ -226,7 +236,7 @@ class Coder:
pieces = lines[0].split()
cmd = pieces[0]
if cmd not in ('BEFORE', 'AFTER'):
if cmd not in ('BEFORE', 'AFTER', 'APPEND'):
raise ValueError(cmd)
if len(pieces) > 1:
@ -255,6 +265,22 @@ class Coder:
if cmd == 'BEFORE':
after_op = ops.pop()
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):
after_cmd,after_fname,after_lines = self.parse_op(after_op)
@ -265,45 +291,35 @@ class Coder:
fname = Path(fname)
content = fname.read_text()
before = '\n'.join(op_lines)
after = '\n'.join(after_lines)
if before not in content:
content = fname.read_text().splitlines()
before = [l.strip() for l in op_lines]
stripped_content = [l.strip() for l in content]
where = find_index(stripped_content, before)
if where < 0:
raise ValueError(before)
content = content.replace(before, after)
fname.write_text(content)
new_content = content[:where]
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.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)
for fname in sys.argv[1:]:
coder.file(Path(fname))
coder.run()
coder.update_files(Path('tmp.commands').read_text()) ; sys.exit()
coder.run()
#dname = Path('../easy-chat')
#coder.file(dname / 'index.html')