检查控制台是否支持Java中的ANSI转义码

时间:2016-12-09 09:24:35

标签: java ansi

我正在用Java构建一个程序,它使用ANSI转义码使用不同颜色的菜单。像

这样的东西
"Tide": {
"dataPoints": [
  {
    **"Localtime": "2016-12-09 12:00:00",**
    "time": 1481270400,
    "type": "NORMAL",
    "utctime": "December 09, 2016 08:00:00",
    "Rawtime": "December 09, 2016 00:00:00",
    "height": 1.314371
  },
  {
    "Localtime": "2016-12-09 01:00:00",
    "time": 1481274000,
    "type": "NORMAL",
    "utctime": "December 09, 2016 09:00:00",
    "Rawtime": "December 09, 2016 01:00:00",
    "height": 1.725071
  },
  {
    "Localtime": "2016-12-09 02:00:00",
    "time": 1481277600,
    "type": "NORMAL",
    "utctime": "December 09, 2016 10:00:00",
    "Rawtime": "December 09, 2016 02:00:00",
    "height": 2.540107
  },
  {
    "Localtime": "2016-12-09 03:00:00",
    "time": 1481281200,
    "type": "NORMAL",
    "utctime": "December 09, 2016 11:00:00",
    "Rawtime": "December 09, 2016 03:00:00",
    "height": 3.602393
  },
  {
    "Localtime": "2016-12-09 04:00:00",
    "time": 1481284800,
    "type": "NORMAL",
    "utctime": "December 09, 2016 12:00:00",
    "Rawtime": "December 09, 2016 04:00:00",
    "height": 4.701366
  },
  {
    "Localtime": "2016-12-09 05:00:00",
    "time": 1481288400,
    "type": "NORMAL",
    "utctime": "December 09, 2016 13:00:00",
    "Rawtime": "December 09, 2016 05:00:00",
    "height": 5.60834
  },
  {
    "Localtime": "2016-12-09 06:00:00",
    "time": 1481292000,
    "type": "NORMAL",
    "utctime": "December 09, 2016 14:00:00",
    "Rawtime": "December 09, 2016 06:00:00",
    "height": 6.114084
  },
  {
    "Localtime": "2016-12-09 06:26:09",
    "time": 1481293569,
    "type": "High",
    "utctime": "December 09, 2016 14:26:09",
    "Rawtime": "December 09, 2016 06:26:09",
    "height": 6.17
  },
  {
    "Localtime": "2016-12-09 07:00:00",
    "time": 1481295600,
    "type": "NORMAL",
    "utctime": "December 09, 2016 15:00:00",
    "Rawtime": "December 09, 2016 07:00:00",
    "height": 6.074992
  },
  {
    "Localtime": "2016-12-09 07:14:14",
    "time": 1481296454,
    "type": "Sunrise",
    "utctime": "December 09, 2016 15:14:14",
    "Rawtime": "December 09, 2016 07:14:14",
    "height": 0
  },
  {
    "Localtime": "2016-12-09 08:00:00",
    "time": 1481299200,
    "type": "NORMAL",
    "utctime": "December 09, 2016 16:00:00",
    "Rawtime": "December 09, 2016 08:00:00",
    "height": 5.462676
  },
  {
    "Localtime": "2016-12-09 09:00:00",
    "time": 1481302800,
    "type": "NORMAL",
    "utctime": "December 09, 2016 17:00:00",
    "Rawtime": "December 09, 2016 09:00:00",
    "height": 4.394475
  },
  {
    "Localtime": "2016-12-09 10:00:00",
    "time": 1481306400,
    "type": "NORMAL",
    "utctime": "December 09, 2016 18:00:00",
    "Rawtime": "December 09, 2016 10:00:00",
    "height": 3.11812
  },
  {
    "Localtime": "2016-12-09 11:00:00",
    "time": 1481310000,
    "type": "NORMAL",
    "utctime": "December 09, 2016 19:00:00",
    "Rawtime": "December 09, 2016 11:00:00",
    "height": 1.942714
  },
  {
    "Localtime": "2016-12-09 12:00:00",
    "time": 1481313600,
    "type": "NORMAL",
    "utctime": "December 09, 2016 20:00:00",
    "Rawtime": "December 09, 2016 12:00:00",
    "height": 1.140244
  },
  {
    "Localtime": "2016-12-09 01:00:00",
    "time": 1481317200,
    "type": "NORMAL",
    "utctime": "December 09, 2016 21:00:00",
    "Rawtime": "December 09, 2016 13:00:00",
    "height": 0.862646
  },
  {
    "Localtime": "2016-12-09 01:00:30",
    "time": 1481317230,
    "type": "Low",
    "utctime": "December 09, 2016 21:00:30",
    "Rawtime": "December 09, 2016 13:00:30",
    "height": 0.86
  },
  {
    "Localtime": "2016-12-09 02:00:00",
    "time": 1481320800,
    "type": "NORMAL",
    "utctime": "December 09, 2016 22:00:00",
    "Rawtime": "December 09, 2016 14:00:00",
    "height": 1.111124
  },
  {
    "Localtime": "2016-12-09 03:00:00",
    "time": 1481324400,
    "type": "NORMAL",
    "utctime": "December 09, 2016 23:00:00",
    "Rawtime": "December 09, 2016 15:00:00",
    "height": 1.761986
  },
  {
    "Localtime": "2016-12-09 04:00:00",
    "time": 1481328000,
    "type": "NORMAL",
    "utctime": "December 10, 2016 00:00:00",
    "Rawtime": "December 09, 2016 16:00:00",
    "height": 2.621853
  },
  {
    "Localtime": "2016-12-09 04:51:06",
    "time": 1481331066,
    "type": "Sunset",
    "utctime": "December 10, 2016 00:51:06",
    "Rawtime": "December 09, 2016 16:51:06",
    "height": 0
  },
  {
    "Localtime": "2016-12-09 05:00:00",
    "time": 1481331600,
    "type": "NORMAL",
    "utctime": "December 10, 2016 01:00:00",
    "Rawtime": "December 09, 2016 17:00:00",
    "height": 3.478611
  },
  {
    "Localtime": "2016-12-09 06:00:00",
    "time": 1481335200,
    "type": "NORMAL",
    "utctime": "December 10, 2016 02:00:00",
    "Rawtime": "December 09, 2016 18:00:00",
    "height": 4.134485
  },
  {
    "Localtime": "2016-12-09 07:00:00",
    "time": 1481338800,
    "type": "NORMAL",
    "utctime": "December 10, 2016 03:00:00",
    "Rawtime": "December 09, 2016 19:00:00",
    "height": 4.432216
  },
  {
    "Localtime": "2016-12-09 07:11:15",
    "time": 1481339475,
    "type": "High",
    "utctime": "December 10, 2016 03:11:15",
    "Rawtime": "December 09, 2016 19:11:15",
    "height": 4.44
  },
  {
    "Localtime": "2016-12-09 08:00:00",
    "time": 1481342400,
    "type": "NORMAL",
    "utctime": "December 10, 2016 04:00:00",
    "Rawtime": "December 09, 2016 20:00:00",
    "height": 4.289772
  },
  {
    "Localtime": "2016-12-09 09:00:00",
    "time": 1481346000,
    "type": "NORMAL",
    "utctime": "December 10, 2016 05:00:00",
    "Rawtime": "December 09, 2016 21:00:00",
    "height": 3.739199
  },
  {
    "Localtime": "2016-12-09 10:00:00",
    "time": 1481349600,
    "type": "NORMAL",
    "utctime": "December 10, 2016 06:00:00",
    "Rawtime": "December 09, 2016 22:00:00",
    "height": 2.942309
  },
  {
    "Localtime": "2016-12-09 11:00:00",
    "time": 1481353200,
    "type": "NORMAL",
    "utctime": "December 10, 2016 07:00:00",
    "Rawtime": "December 09, 2016 23:00:00",
    "height": 2.156115
  },
  {
    "Localtime": "2016-12-10 12:00:00",
    "time": 1481356800,
    "type": "NORMAL",
    "utctime": "December 10, 2016 08:00:00",
    "Rawtime": "December 10, 2016 00:00:00",
    "height": 1.649912
  },
  {
    **"Localtime": "2016-12-10 12:34:41",**
    "time": 1481358881,
    "type": "Low",
    "utctime": "December 10, 2016 08:34:41",
    "Rawtime": "December 10, 2016 00:34:41",
    "height": 1.56
  },
  {
    "Localtime": "2016-12-10 01:00:00",
    "time": 1481360400,
    "type": "NORMAL",
    "utctime": "December 10, 2016 09:00:00",
    "Rawtime": "December 10, 2016 01:00:00",
    "height": 1.610976
  },
  {
    "Localtime": "2016-12-10 02:00:00",
    "time": 1481364000,
    "type": "NORMAL",
    "utctime": "December 10, 2016 10:00:00",
    "Rawtime": "December 10, 2016 02:00:00",
    "height": 2.086316
  },
  {
    "Localtime": "2016-12-10 03:00:00",
    "time": 1481367600,
    "type": "NORMAL",
    "utctime": "December 10, 2016 11:00:00",
    "Rawtime": "December 10, 2016 03:00:00",
    "height": 2.983297
  },
  {
    "Localtime": "2016-12-10 04:00:00",
    "time": 1481371200,
    "type": "NORMAL",
    "utctime": "December 10, 2016 12:00:00",
    "Rawtime": "December 10, 2016 04:00:00",
    "height": 4.113031
  },
  {
    "Localtime": "2016-12-10 05:00:00",
    "time": 1481374800,
    "type": "NORMAL",
    "utctime": "December 10, 2016 13:00:00",
    "Rawtime": "December 10, 2016 05:00:00",
    "height": 5.240836
  },
  {
    "Localtime": "2016-12-10 06:00:00",
    "time": 1481378400,
    "type": "NORMAL",
    "utctime": "December 10, 2016 14:00:00",
    "Rawtime": "December 10, 2016 06:00:00",
    "height": 6.122298
  },
  {
    "Localtime": "2016-12-10 07:00:00",
    "time": 1481382000,
    "type": "NORMAL",
    "utctime": "December 10, 2016 15:00:00",
    "Rawtime": "December 10, 2016 07:00:00",
    "height": 6.533787
  },
  {
    "Localtime": "2016-12-10 07:10:54",
    "time": 1481382654,
    "type": "High",
    "utctime": "December 10, 2016 15:10:54",
    "Rawtime": "December 10, 2016 07:10:54",
    "height": 6.54
  },
  {
    "Localtime": "2016-12-10 07:15:15",
    "time": 1481382915,
    "type": "Sunrise",
    "utctime": "December 10, 2016 15:15:15",
    "Rawtime": "December 10, 2016 07:15:15",
    "height": 0
  },
  {
    "Localtime": "2016-12-10 08:00:00",
    "time": 1481385600,
    "type": "NORMAL",
    "utctime": "December 10, 2016 16:00:00",
    "Rawtime": "December 10, 2016 08:00:00",
    "height": 6.318964
  },
  {
    "Localtime": "2016-12-10 09:00:00",
    "time": 1481389200,
    "type": "NORMAL",
    "utctime": "December 10, 2016 17:00:00",
    "Rawtime": "December 10, 2016 09:00:00",
    "height": 5.452142
  },
  {
    "Localtime": "2016-12-10 10:00:00",
    "time": 1481392800,
    "type": "NORMAL",
    "utctime": "December 10, 2016 18:00:00",
    "Rawtime": "December 10, 2016 10:00:00",
    "height": 4.084528
  },
  {
    "Localtime": "2016-12-10 11:00:00",
    "time": 1481396400,
    "type": "NORMAL",
    "utctime": "December 10, 2016 19:00:00",
    "Rawtime": "December 10, 2016 11:00:00",
    "height": 2.527734
  },
  {
    "Localtime": "2016-12-10 12:00:00",
    "time": 1481400000,
    "type": "NORMAL",
    "utctime": "December 10, 2016 20:00:00",
    "Rawtime": "December 10, 2016 12:00:00",
    "height": 1.159965
  },
  {
    "Localtime": "2016-12-10 01:00:00",
    "time": 1481403600,
    "type": "NORMAL",
    "utctime": "December 10, 2016 21:00:00",
    "Rawtime": "December 10, 2016 13:00:00",
    "height": 0.293658
  },
  {
    "Localtime": "2016-12-10 01:50:12",
    "time": 1481406612,
    "type": "Low",
    "utctime": "December 10, 2016 21:50:12",
    "Rawtime": "December 10, 2016 13:50:12",
    "height": 0.07
  },
  {
    "Localtime": "2016-12-10 02:00:00",
    "time": 1481407200,
    "type": "NORMAL",
    "utctime": "December 10, 2016 22:00:00",
    "Rawtime": "December 10, 2016 14:00:00",
    "height": 0.074574
  },
  {
    "Localtime": "2016-12-10 03:00:00",
    "time": 1481410800,
    "type": "NORMAL",
    "utctime": "December 10, 2016 23:00:00",
    "Rawtime": "December 10, 2016 15:00:00",
    "height": 0.462501
  },
  {
    "Localtime": "2016-12-10 04:00:00",
    "time": 1481414400,
    "type": "NORMAL",
    "utctime": "December 11, 2016 00:00:00",
    "Rawtime": "December 10, 2016 16:00:00",
    "height": 1.287361
  },
  {
    "Localtime": "2016-12-10 04:51:14",
    "time": 1481417474,
    "type": "Sunset",
    "utctime": "December 11, 2016 00:51:14",
    "Rawtime": "December 10, 2016 16:51:14",
    "height": 0
  },
  {
    "Localtime": "2016-12-10 05:00:00",
    "time": 1481418000,
    "type": "NORMAL",
    "utctime": "December 11, 2016 01:00:00",
    "Rawtime": "December 10, 2016 17:00:00",
    "height": 2.328355
  },
  {
    "Localtime": "2016-12-10 06:00:00",
    "time": 1481421600,
    "type": "NORMAL",
    "utctime": "December 11, 2016 02:00:00",
    "Rawtime": "December 10, 2016 18:00:00",
    "height": 3.365122
  },
  {
    "Localtime": "2016-12-10 07:00:00",
    "time": 1481425200,
    "type": "NORMAL",
    "utctime": "December 11, 2016 03:00:00",
    "Rawtime": "December 10, 2016 19:00:00",
    "height": 4.192768
  },
  {
    "Localtime": "2016-12-10 08:00:00",
    "time": 1481428800,
    "type": "NORMAL",
    "utctime": "December 11, 2016 04:00:00",
    "Rawtime": "December 10, 2016 20:00:00",
    "height": 4.633698
  },
  {
    "Localtime": "2016-12-10 08:23:35",
    "time": 1481430215,
    "type": "High",
    "utctime": "December 11, 2016 04:23:35",
    "Rawtime": "December 10, 2016 20:23:35",
    "height": 4.67
  },
  {
    "Localtime": "2016-12-10 09:00:00",
    "time": 1481432400,
    "type": "NORMAL",
    "utctime": "December 11, 2016 05:00:00",
    "Rawtime": "December 10, 2016 21:00:00",
    "height": 4.578441
  },
  {
    "Localtime": "2016-12-10 10:00:00",
    "time": 1481436000,
    "type": "NORMAL",
    "utctime": "December 11, 2016 06:00:00",
    "Rawtime": "December 10, 2016 22:00:00",
    "height": 4.046519
  },
  {
    "Localtime": "2016-12-10 11:00:00",
    "time": 1481439600,
    "type": "NORMAL",
    "utctime": "December 11, 2016 07:00:00",
    "Rawtime": "December 10, 2016 23:00:00",
    "height": 3.218655
  },
  {
    **"Localtime": "2016-12-11 12:00:00",**
    "time": 1481443200,
    "type": "NORMAL",
    "utctime": "December 11, 2016 08:00:00",
    "Rawtime": "December 11, 2016 00:00:00",
    "height": 2.395649
  },
  {
    "Localtime": "2016-12-11 01:00:00",
    "time": 1481446800,
    "type": "NORMAL",
    "utctime": "December 11, 2016 09:00:00",
    "Rawtime": "December 11, 2016 01:00:00",
    "height": 1.888912
  },
  {
    "Localtime": "2016-12-11 01:28:53",
    "time": 1481448533,
    "type": "Low",
    "utctime": "December 11, 2016 09:28:53",
    "Rawtime": "December 11, 2016 01:28:53",
    "height": 1.82
  },
  {
    "Localtime": "2016-12-11 02:00:00",
    "time": 1481450400,
    "type": "NORMAL",
    "utctime": "December 11, 2016 10:00:00",
    "Rawtime": "December 11, 2016 02:00:00",
    "height": 1.900692
  },
  {
    "Localtime": "2016-12-11 03:00:00",
    "time": 1481454000,
    "type": "NORMAL",
    "utctime": "December 11, 2016 11:00:00",
    "Rawtime": "December 11, 2016 03:00:00",
    "height": 2.460855
  },
  {
    "Localtime": "2016-12-11 04:00:00",
    "time": 1481457600,
    "type": "NORMAL",
    "utctime": "December 11, 2016 12:00:00",
    "Rawtime": "December 11, 2016 04:00:00",
    "height": 3.443584
  },
  {
    "Localtime": "2016-12-11 05:00:00",
    "time": 1481461200,
    "type": "NORMAL",
    "utctime": "December 11, 2016 13:00:00",
    "Rawtime": "December 11, 2016 05:00:00",
    "height": 4.630794
  },
  {
    "Localtime": "2016-12-11 06:00:00",
    "time": 1481464800,
    "type": "NORMAL",
    "utctime": "December 11, 2016 14:00:00",
    "Rawtime": "December 11, 2016 06:00:00",
    "height": 5.769447
  },
  {
    "Localtime": "2016-12-11 07:00:00",
    "time": 1481468400,
    "type": "NORMAL",
    "utctime": "December 11, 2016 15:00:00",
    "Rawtime": "December 11, 2016 07:00:00",
    "height": 6.600818
  },
  {
    "Localtime": "2016-12-11 07:15:47",
    "time": 1481469347,
    "type": "Sunrise",
    "utctime": "December 11, 2016 15:15:47",
    "Rawtime": "December 11, 2016 07:15:47",
    "height": 0
  },
  {
    "Localtime": "2016-12-11 07:55:45",
    "time": 1481471745,
    "type": "High",
    "utctime": "December 11, 2016 15:55:45",
    "Rawtime": "December 11, 2016 07:55:45",
    "height": 6.89
  },
  {
    "Localtime": "2016-12-11 08:00:00",
    "time": 1481472000,
    "type": "NORMAL",
    "utctime": "December 11, 2016 16:00:00",
    "Rawtime": "December 11, 2016 08:00:00",
    "height": 6.885445
  },
  {
    "Localtime": "2016-12-11 09:00:00",
    "time": 1481475600,
    "type": "NORMAL",
    "utctime": "December 11, 2016 17:00:00",
    "Rawtime": "December 11, 2016 09:00:00",
    "height": 6.458388
  },
  {
    "Localtime": "2016-12-11 10:00:00",
    "time": 1481479200,
    "type": "NORMAL",
    "utctime": "December 11, 2016 18:00:00",
    "Rawtime": "December 11, 2016 10:00:00",
    "height": 5.311423
  },
  {
    "Localtime": "2016-12-11 11:00:00",
    "time": 1481482800,
    "type": "NORMAL",
    "utctime": "December 11, 2016 19:00:00",
    "Rawtime": "December 11, 2016 11:00:00",
    "height": 3.648872
  },
  {
    "Localtime": "2016-12-11 12:00:00",
    "time": 1481486400,
    "type": "NORMAL",
    "utctime": "December 11, 2016 20:00:00",
    "Rawtime": "December 11, 2016 12:00:00",
    "height": 1.85591
  },
  {
    "Localtime": "2016-12-11 01:00:00",
    "time": 1481490000,
    "type": "NORMAL",
    "utctime": "December 11, 2016 21:00:00",
    "Rawtime": "December 11, 2016 13:00:00",
    "height": 0.370124
  },
  {
    "Localtime": "2016-12-11 02:00:00",
    "time": 1481493600,
    "type": "NORMAL",
    "utctime": "December 11, 2016 22:00:00",
    "Rawtime": "December 11, 2016 14:00:00",
    "height": -0.4807
  },
  {
    "Localtime": "2016-12-11 02:38:11",
    "time": 1481495891,
    "type": "Low",
    "utctime": "December 11, 2016 22:38:11",
    "Rawtime": "December 11, 2016 14:38:11",
    "height": -0.63
  },
  {
    "Localtime": "2016-12-11 03:00:00",
    "time": 1481497200,
    "type": "NORMAL",
    "utctime": "December 11, 2016 23:00:00",
    "Rawtime": "December 11, 2016 15:00:00",
    "height": -0.581912
  },
  {
    "Localtime": "2016-12-11 04:00:00",
    "time": 1481500800,
    "type": "NORMAL",
    "utctime": "December 12, 2016 00:00:00",
    "Rawtime": "December 11, 2016 16:00:00",
    "height": -0.025588
  },
  {
    "Localtime": "2016-12-11 04:51:25",
    "time": 1481503885,
    "type": "Sunset",
    "utctime": "December 12, 2016 00:51:25",
    "Rawtime": "December 11, 2016 16:51:25",
    "height": 0
  },
  {
    "Localtime": "2016-12-11 05:00:00",
    "time": 1481504400,
    "type": "NORMAL",
    "utctime": "December 12, 2016 01:00:00",
    "Rawtime": "December 11, 2016 17:00:00",
    "height": 0.977441
  },
  {
    "Localtime": "2016-12-11 06:00:00",
    "time": 1481508000,
    "type": "NORMAL",
    "utctime": "December 12, 2016 02:00:00",
    "Rawtime": "December 11, 2016 18:00:00",
    "height": 2.189733
  },
  {
    "Localtime": "2016-12-11 07:00:00",
    "time": 1481511600,
    "type": "NORMAL",
    "utctime": "December 12, 2016 03:00:00",
    "Rawtime": "December 11, 2016 19:00:00",
    "height": 3.386593
  },
  {
    "Localtime": "2016-12-11 08:00:00",
    "time": 1481515200,
    "type": "NORMAL",
    "utctime": "December 12, 2016 04:00:00",
    "Rawtime": "December 11, 2016 20:00:00",
    "height": 4.351603
  },
  {
    "Localtime": "2016-12-11 09:00:00",
    "time": 1481518800,
    "type": "NORMAL",
    "utctime": "December 12, 2016 05:00:00",
    "Rawtime": "December 11, 2016 21:00:00",
    "height": 4.883885
  }
  // etc
]

问题是我想检查要执行代码的控制台是否支持使用此代码,所以如果没有,请打印没有代码的替代版本。

它将类似于:

System.out.println("\u001B[36m"+"Menu option"+"\u001B[0m");

java中是否有方法可以检查这个?

2 个答案:

答案 0 :(得分:4)

一个简单的纯Java解决方案:

if (System.console() != null && System.getenv().get("TERM") != null) {
    System.out.println("\u001B[36m"+"Menu option"+"\u001B[0m");
} else {
    System.out.println("Menu option");
}

第一个术语用于检查是否连接了终端,第二个术语用于查看是否已定义TERM env var(在Windows上不是)。这不是完美的方法,但是对于我来说效果很好。

答案 1 :(得分:1)

我不知道Java是否有一些特殊的方法可以解决这个问题,但有两种方法可以解决问题本身。使用的常用方法(至少在unix世界中)是从TERM环境变量中获取终端,并从terminfo / termcap数据库中查找它的功能(通常使用{ {3}}或curses)。另一种更粗略的方法是发送设备状态报告代码("\u001B[6n")并检查终端是否响应。这当然有点像黑客,因为支持DSR代码的终端可能不支持颜色。例如。