@@ -71,6 +71,7 @@ private void reset() {
7171
7272 public void updateWith (Properties props ) {
7373 reset ();
74+ boolean cursorPropExists = false ;
7475 for (Map .Entry <Object , Object > entries : props .entrySet ()) {
7576 String key = (String ) entries .getKey ();
7677 String value = (String ) entries .getValue ();
@@ -82,6 +83,7 @@ public void updateWith(Properties props) {
8283 colorIndex = TextStyle .COLOR_INDEX_BACKGROUND ;
8384 } else if (key .equals ("cursor" )) {
8485 colorIndex = TextStyle .COLOR_INDEX_CURSOR ;
86+ cursorPropExists = true ;
8587 } else if (key .startsWith ("color" )) {
8688 try {
8789 colorIndex = Integer .parseInt (key .substring (5 ));
@@ -98,6 +100,27 @@ public void updateWith(Properties props) {
98100
99101 mDefaultColors [colorIndex ] = colorValue ;
100102 }
103+
104+ if (!cursorPropExists )
105+ setCursorColorForBackground ();
106+ }
107+
108+ /**
109+ * If the "cursor" color is not set by user, we need to decide on the appropriate color that will
110+ * be visible on the current terminal background. White will not be visible on light backgrounds
111+ * and black won't be visible on dark backgrounds. So we find the perceived brightness of the
112+ * background color and if its below the threshold (too dark), we use white cursor and if its
113+ * above (too bright), we use black cursor.
114+ */
115+ public void setCursorColorForBackground () {
116+ int backgroundColor = mDefaultColors [TextStyle .COLOR_INDEX_BACKGROUND ];
117+ int brightness = TerminalColors .getPerceivedBrightnessOfColor (backgroundColor );
118+ if (brightness > 0 ) {
119+ if (brightness < 130 )
120+ mDefaultColors [TextStyle .COLOR_INDEX_CURSOR ] = 0xffffffff ;
121+ else
122+ mDefaultColors [TextStyle .COLOR_INDEX_CURSOR ] = 0xff000000 ;
123+ }
101124 }
102125
103126}
0 commit comments