Beautify the output

Actually, we have finished the core feature of the tool with the data we needed.

However, it is better to output the result in a more user-friendly way for a command line tool.

Add color

colors is a module for adding colors for command line tools, and here is a simple example:

  1. var colors = require('colors');
  2. console.log('Color'.green);

By adding the name of a color after a string, we could make the output string colorful. For the list of colors it supports, you’d better refer the document on Github or npm.

Format the output style

In the last chapter, we got the result in JSON as below:

  1. {"translation":["测试"],"basic":{"us-phonetic":"tɛst","phonetic":"test","uk-phonetic":"test","explains":["n. 试验;检验","vt. 试验;测试","vi. 试验;测试","n. (Test)人名;(英)特斯特"]},"query":"test","errorCode":0,"web":[{"value":["测试","试验","检验"],"key":"test"},{"value":["测试工程师","测试员","软件测试工程师"],"key":"Test engineer"},{"value":["硬度试验","硬度测试","硬度实验"],"key":"hardness test"}]}

Formatted:

  1. {
  2. "translation":["测试"],
  3. "basic":{
  4. "us-phonetic":"tɛst",
  5. "phonetic":"test",
  6. "uk-phonetic":"test",
  7. "explains":[
  8. "n. 试验;检验",
  9. "vt. 试验;测试",
  10. "vi. 试验;测试",
  11. "n.(Test)人名;(英)特斯特"
  12. ]
  13. },
  14. "query":"test",
  15. "errorCode":0,
  16. "web":[{
  17. "value":["测试","试验","检验"],
  18. "key":"test"
  19. },{
  20. "value":["测试工程师","测试员","软件测试工程师"],
  21. "key":"Test engineer"
  22. },{
  23. "value":["硬度试验","硬度测试","硬度实验"],
  24. "key":"hardness test"
  25. }]
  26. }

A better way to output the result should be with proper new lines, spaces. About this, you can refer the output.js file in the ‘node-translator’.