Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight changed parts in "Correct Texts" #34

Closed
sbraz opened this issue Aug 26, 2016 · 5 comments
Closed

Highlight changed parts in "Correct Texts" #34

sbraz opened this issue Aug 26, 2016 · 5 comments

Comments

@sbraz
Copy link
Contributor

sbraz commented Aug 26, 2016

Sometimes it's hard to see what changed, especially when it's only whitespace. You could probably take advantage of difflib's SequenceMatcher to do this. I looked around but I don't see another library which would make it easier.
Feel free to re-use my code if it helps:
highlight

#!/usr/bin/env python3

import sys
import difflib

import colorama

def highlight_diff(a, b, a_delim_start, a_delim_end, b_delim_start, b_delim_end):
  s = difflib.SequenceMatcher(a=a, b=b)
  matching_block = s.get_matching_blocks()
  previous_index_a, previous_index_b = (0, 0)
  highlighted_a, highlighted_b = ("", "")
  for block in matching_block:
    highlighted_a += (a_delim_start + a[previous_index_a:block.a] +
            a_delim_end + a[block.a:block.a + block.size])
    previous_index_a = block.a + block.size
    highlighted_b += (b_delim_start + b[previous_index_b:block.b] +
            b_delim_end + b[block.b:block.b + block.size])
    previous_index_b = block.b + block.size
  return highlighted_a, highlighted_b

if __name__ == "__main__":
    a, b = sys.argv[1:]
    print("\n".join(highlight_diff(a, b, colorama.Style.BRIGHT + colorama.Fore.RED,
        colorama.Style.RESET_ALL, colorama.Style.BRIGHT + colorama.Fore.GREEN,
        colorama.Style.RESET_ALL)))
@otsaloma
Copy link
Owner

This could be very useful. I haven't used difflib, but if it works well enough, the bigger task is probably doing the GTK+ styling -- probably with the markup property of Gtk.CellRendererText and CSS classes (<span class="gaupol-diff-added">...</span> maybe?), somehow accounting for light vs. dark themes.

I don't have any immediate plans to do this. If someone's interested in contributing, this should be doable without needing to be familiar with or learning to understand Gaupol's codebase as a whole.

@sbraz
Copy link
Contributor Author

sbraz commented Aug 27, 2016

I would help but I never work with GUIs so I wouldn't know where to start.

@otsaloma
Copy link
Owner

highlight

@otsaloma
Copy link
Owner

Done now. Used SequenceMatcher.get_opcodes so that added, removed and changed parts are colored different. Colors are done with opacity, so should hopefully work about OK with different themes. Colors are also configurable by editing the config file. Not tested much yet, so open to feedback.

@sbraz
Copy link
Contributor Author

sbraz commented Jan 20, 2019

Great, thanks! I'm going to try using the git version and I'll let you know if I run into issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants