Console.java revision 1372:7cbb2b5867fd
1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.nashorn.tools.jjs;
27
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.PrintStream;
31import java.util.ArrayList;
32import java.util.Arrays;
33import java.util.Collections;
34import java.util.Iterator;
35import java.util.List;
36import java.util.prefs.BackingStoreException;
37import java.util.prefs.Preferences;
38import jdk.internal.jline.console.ConsoleReader;
39import jdk.internal.jline.console.history.History.Entry;
40import jdk.internal.jline.console.history.MemoryHistory;
41
42class Console implements AutoCloseable {
43    private final ConsoleReader in;
44    private final PersistentHistory history;
45
46    Console(InputStream cmdin, PrintStream cmdout, Preferences prefs) throws IOException {
47        in = new ConsoleReader(cmdin, cmdout);
48        in.setExpandEvents(false);
49        in.setHandleUserInterrupt(true);
50        in.setHistory(history = new PersistentHistory(prefs));
51        Runtime.getRuntime().addShutdownHook(new Thread(()->close()));
52    }
53
54    String readLine(String prompt) throws IOException {
55        return in.readLine(prompt);
56    }
57
58
59    @Override
60    public void close() {
61        history.save();
62    }
63
64    public static class PersistentHistory extends MemoryHistory {
65
66        private final Preferences prefs;
67
68        protected PersistentHistory(Preferences prefs) {
69            this.prefs = prefs;
70            load();
71        }
72
73        private static final String HISTORY_LINE_PREFIX = "HISTORY_LINE_";
74
75        public final void load() {
76            try {
77                List<String> keys = new ArrayList<>(Arrays.asList(prefs.keys()));
78                Collections.sort(keys);
79                for (String key : keys) {
80                    if (!key.startsWith(HISTORY_LINE_PREFIX))
81                        continue;
82                    CharSequence line = prefs.get(key, "");
83                    add(line);
84                }
85            } catch (BackingStoreException ex) {
86                throw new IllegalStateException(ex);
87            }
88        }
89
90        public void save() {
91            Iterator<Entry> entries = iterator();
92            if (entries.hasNext()) {
93                int len = (int) Math.ceil(Math.log10(size()+1));
94                String format = HISTORY_LINE_PREFIX + "%0" + len + "d";
95                while (entries.hasNext()) {
96                    Entry entry = entries.next();
97                    prefs.put(String.format(format, entry.index()), entry.value().toString());
98                }
99            }
100        }
101
102    }
103}
104