让groovy控制台程序拥有彩色输出"面孔"

结合JNA的使用可以让groovy的控制台程序拥有linux终端一般的彩色数据效果。需要jna.jar包放到classpath可以找到的路径下就可以了。
  1. importcom.sun.jna.win32.StdCallLibrary;
  2. importcom.sun.jna.*;
  3. publicinterfaceKernel32extendsStdCallLibrary{
  4. Kernel32INSTANCE=(Kernel32)Native.loadLibrary("kernel32",Kernel32.class);
  5. intGetStdHandle(intstdHand);
  6. booleanSetConsoleTextAttribute(inthConsoleOutput,inttextAtt);
  7. }
  8. defout={color,str->
  9. intptr=Kernel32.INSTANCE.GetStdHandle(-11);
  10. Kernel32.INSTANCE.SetConsoleTextAttribute(ptr,color);
  11. print(str);
  12. Kernel32.INSTANCE.SetConsoleTextAttribute(ptr,3);
  13. }
  14. out(13,"windows?"+Platform.isWindows());
  15. out(14,"X11?"+Platform.isX11());
运行一下就可以看到彩色数据的效果了。其中color的参数的具体颜色,可以打开一个控制台然后查看属性

黑色 是数字 0 以此类推

最后我再这个基础上改了一个数据比对程序,下面是效果图