changeset 2471:6c527f28ead0

Append file separator char to directory names in shell filename tab completion Reviewed-by: jkang Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-September/021047.html Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-October/021156.html
author Andrew Azores <aazores@redhat.com>
date Thu, 13 Oct 2016 09:37:20 -0400
parents b8187bde7914
children 54ed75a2852d
files launcher/src/main/java/com/redhat/thermostat/launcher/internal/JLineFileNameCompleter.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/JLineFileNameCompleterTest.java
diffstat 2 files changed, 138 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/JLineFileNameCompleter.java	Tue Oct 04 11:21:55 2016 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/JLineFileNameCompleter.java	Thu Oct 13 09:37:20 2016 -0400
@@ -39,10 +39,30 @@
 import com.redhat.thermostat.common.cli.FileNameTabCompleter;
 import jline.console.completer.FileNameCompleter;
 
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
 public class JLineFileNameCompleter extends JLineCompleterWrapper implements FileNameTabCompleter {
 
     public JLineFileNameCompleter() {
-        super(new FileNameCompleter());
+        super(new ThermostatFileNameCompleter());
     }
 
+    static class ThermostatFileNameCompleter extends FileNameCompleter {
+        @Override
+        protected int matchFiles(final String buffer, final String translated, final File[] files, final List<CharSequence> candidates) {
+            // overridden for test access only
+            return super.matchFiles(buffer, translated, files, candidates);
+        }
+
+        @Override
+        protected CharSequence render(File file, CharSequence name) {
+            // when super.matchFiles calls this method, the 'name' parameter passed is equal to
+            // file.getName() + ' '. For directories, we want to include the File.separator character
+            // after the directory name, to indicate that this is a directory, and remove the whitespace
+            // to allow for more fluid completion of file paths.
+            return file.isDirectory() ? file.getName() + File.separator : name;
+        }
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/JLineFileNameCompleterTest.java	Thu Oct 13 09:37:20 2016 -0400
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2012-2016 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.launcher.internal;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class JLineFileNameCompleterTest {
+
+    private JLineFileNameCompleter.ThermostatFileNameCompleter completer;
+
+    @Before
+    public void setup() {
+        completer = new JLineFileNameCompleter.ThermostatFileNameCompleter();
+    }
+
+    @Test
+    public void testAppendsFileSeparatorToDirectoriesAndNotFiles() {
+        String cwd = File.separator + "foo" + File.separator;
+
+        File file = mock(File.class);
+        when(file.getName()).thenReturn("file");
+        when(file.getAbsolutePath()).thenReturn(cwd + "file");
+        when(file.isFile()).thenReturn(true);
+        when(file.isDirectory()).thenReturn(false);
+
+        File dir = mock(File.class);
+        when(dir.getName()).thenReturn("dir");
+        when(dir.getAbsolutePath()).thenReturn(cwd + "dir");
+        when(dir.isFile()).thenReturn(false);
+        when(dir.isDirectory()).thenReturn(true);
+
+        List<CharSequence> candidates = new ArrayList<>();
+        completer.matchFiles("", cwd, new File[]{file, dir}, candidates);
+
+        assertThat(candidates.size(), is(2));
+        assertTrue(candidates.contains("file "));
+        assertTrue(candidates.contains("dir" + File.separator));
+    }
+
+    @Test
+    public void testAppendsSpaceIfOnlyOneMatch() {
+        String cwd = File.separator + "foo" + File.separator;
+
+        File file = mock(File.class);
+        when(file.getName()).thenReturn("file");
+        when(file.getAbsolutePath()).thenReturn(cwd + "file");
+        when(file.isFile()).thenReturn(true);
+        when(file.isDirectory()).thenReturn(false);
+
+        List<CharSequence> candidates = new ArrayList<>();
+        completer.matchFiles("", cwd, new File[]{file}, candidates);
+
+        assertThat(candidates.size(), is(1));
+        assertTrue(candidates.contains("file "));
+    }
+
+    @Test
+    public void testNoResultsIfNoMatches() {
+        String cwd = File.separator + "foo" + File.separator;
+
+        List<CharSequence> candidates = new ArrayList<>();
+        completer.matchFiles("", cwd, new File[]{}, candidates);
+
+        assertThat(candidates.size(), is(0));
+    }
+
+    @Test
+    public void testReturnsZeroIfFilesNull() {
+        assertThat(completer.matchFiles("", "", null, new ArrayList<CharSequence>()), is(-1));
+    }
+
+}