Added diff option

This commit is contained in:
Dimitris Zlatanidis 2023-03-08 22:16:01 +02:00
parent 2cf611dd70
commit 82cfc4c1e9

View file

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
import shutil
import difflib
from pathlib import Path
from slpkg.configs import Configs
@ -122,11 +123,12 @@ class NewConfig(Configs):
def prompt(self):
""" Prompt K, O, R selection for every single file. """
print()
print(f"\n({self.byellow}K{self.endc})eep, ({self.byellow}O{self.endc})verwrite, "
f"({self.byellow}R{self.endc})emove, ({self.byellow}D{self.endc})iff\n")
if self.slpkg_config_new.is_file():
make = input(f'{self.bgreen}{self.slpkg_config_new}{self.endc} - '
f'({self.byellow}K{self.endc}/{self.byellow}O{self.endc}/'
f'{self.byellow}R{self.endc}): ')
f'{self.byellow}R{self.endc}/{self.byellow}D{self.endc}): ')
if make.lower() == 'k':
pass
@ -137,11 +139,13 @@ class NewConfig(Configs):
print() # new line
self.remove_config_file()
print() # new line
if make.lower() == 'd':
self.diff_files(self.slpkg_config_new, self.slpkg_config)
if self.blacklist_config_new.is_file():
make = input(f'{self.bgreen}{self.blacklist_config_new}{self.endc} - '
f'({self.byellow}K{self.endc}/{self.byellow}O{self.endc}/'
f'{self.byellow}R{self.endc}): ')
f'{self.byellow}R{self.endc}/{self.byellow}D{self.endc}): ')
if make.lower() == 'k':
pass
@ -152,3 +156,19 @@ class NewConfig(Configs):
print() # new line
self.remove_blacklist_file()
print() # new line
if make.lower() == 'd':
self.diff_files(self.blacklist_config_new, self.blacklist_config)
@staticmethod
def diff_files(file1, file2):
""" Diff the .new and the current file. """
with open(file1, 'r') as f1:
with open(file2, 'r') as f2:
diff = difflib.context_diff(
f1.readlines(),
f2.readlines(),
fromfile=str(file1),
tofile=str(file2)
)
for line in diff:
print(line, end='')