简体   繁体   English

如何使用awesome_print为组之间的差异上色?

[英]How do I color the differences between sets with awesome_print?

I am doing a set difference and printing the difference using the awesome_print gem: 我正在做一组差异,并使用awesome_print gem打印差异:

 ap (a | b ) - (a & b)

This prints out the two items that are different. 这将打印出两个不同的项目。

Is there a way to highlight or color the difference between the two? 有没有办法突出或着色两者之间的差异?

I don't see how you can do what you want to achieve. 我看不到您如何才能实现自己想要的目标。 As you may know, Awesome Print allows you to specify the colors of different objects (arrays, hashes, strings, etc.). 如您所知,Awesome Print允许您指定不同对象(数组,哈希,字符串等)的颜色。 The Awesome Print docs are here . 真棒打印文档在这里

Suppose you wished to print a set containing the symbol, :cat, and the string, "dog", with the set "blue", :cat "purplish" and "dog" greenish. 假设您希望打印一个包含符号:cat和字符串“ dog”的集合,且该集合的集为“ blue”,:cat“ purplish”和“ dog”为绿色。 You could do that as follows (it appears the color for arrays is applied to sets, which makes sense): 您可以按以下步骤进行操作(数组的颜色似乎应用于集合,这很有意义):

require 'awesome_print'
require 'set'

set = [:cat, 'dog'].to_set
ap set, options={color: {array: :blue, symbol: :purpleish, string: :greenish}}

and the set would be displayed like this: 该集合将显示如下:

When the two elements are of the same class, however, it appears the best you can do is print them separately, by invoking ap for each: 但是,当两个元素属于同一类时,看来最好的办法是分别打印它们,方法是分别为每个调用ap

set = ['cat', 'dog'].to_set
set.each_with_index { |e,i|
  ap e, options = { color: { string: i.zero? ? :greenish : :red } } }

which displays: 显示:

If you wanted to assign a particular color to each element of a and b , you may want to do something like this: 如果要为ab每个元素分配特定的颜色,则可能需要执行以下操作:

set.each { |e| ap e, options = { color: { string: color_map(e) } } }

where color_map is a method you would write. 其中color_map是您要编写的方法。

You may need to first convert the JSON object to a string (if it is not already a string--I'm not familiar with JSON ). 您可能需要先将JSON对象转换为字符串(如果还不是字符串,我对JSON并不熟悉)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM