Search in sources :

Example 1 with Termios

use of suite.ansi.Termios in project suite by stupidsing.

the class DevMain method run.

private void run() {
    String input = FileUtil.read("src/main/java/suite/dev/DevMain.java");
    Text inputText = text(IRope.ropeList(input));
    try (Termios termios = new Termios(libc)) {
        termios.clear();
        Keyboard keyboard = new Keyboard(libc);
        Sink<State> redraw = state -> state.apply((st, undo, redo, text, oc, cc) -> cc.apply((cx, cy) -> oc.apply((ox, oy) -> {
            String[] lines = // 
            Ints_.range(// 
            viewSizeY).map(// 
            screenY -> text.get(ox, oy + screenY, viewSizeX).replace('\t', ' ')).toArray(String.class);
            termios.cursor(false);
            for (int screenY = 0; screenY < viewSizeY; screenY++) {
                termios.gotoxy(0, screenY);
                termios.puts(lines[screenY]);
            }
            termios.gotoxy(cx - ox, cy - oy);
            termios.cursor(true);
            return null;
        })));
        State state0 = new State(null, null, inputText, c(0, 0), c(0, 0));
        redraw.sink(state0);
        FixieFun3<VK, Character, State, State> mutate = (vk, ch, state) -> // 
        state.apply((st, undo, redo, text, oc, cc) -> oc.apply((ox, oy) -> cc.apply((cx, cy) -> {
            int ci = text.index(cx, cy);
            if (vk == VK.LEFT_)
                return st.cursor(ci - 1);
            else if (vk == VK.RIGHT)
                return st.cursor(ci + 1);
            else if (vk == VK.UP___)
                return st.cursor(cx, cy - 1);
            else if (vk == VK.DOWN_)
                return st.cursor(cx, cy + 1);
            else if (vk == VK.PGUP_)
                return st.cursor(cx, cy - viewSizeY);
            else if (vk == VK.PGDN_)
                return st.cursor(cx, cy + viewSizeY);
            else if (vk == VK.HOME_)
                return st.cursor(text.startOfLine(ci));
            else if (vk == VK.END__)
                return st.cursor(text.endOfLine(ci));
            else if (vk == VK.CTRL_HOME_)
                return st.cursor(0);
            else if (vk == VK.CTRL_END__)
                return st.cursor(text.length());
            else if (vk == VK.CTRL_LEFT_)
                return st.cursor(text.scanNext(ci, -1, ch_ -> !Character.isJavaIdentifierPart(ch_)));
            else if (vk == VK.CTRL_RIGHT)
                return st.cursor(text.scanNext(ci, 1, ch_ -> !Character.isJavaIdentifierPart(ch_)));
            else if (vk == VK.CTRL_UP___)
                return st.offset(ox, oy - 1).cursor(cx, cy - 1);
            else if (vk == VK.CTRL_DOWN_)
                return st.offset(ox, oy + 1).cursor(cx, cy + 1);
            else if (vk == VK.F7___)
                return st.offset(ox, max(cy - viewSizeY + 1, 0));
            else if (vk == VK.F8___)
                return st.offset(ox, min(cy, text.nLines()));
            else if (vk == VK.ALT_J____) {
                int index = text.endOfLine(ci);
                Text text1 = text.splice(index, index + 1, empty);
                return st.text(text1).cursor(index);
            } else if (vk == VK.BKSP_) {
                int index = ci;
                return 0 < index ? st.splice(index - 1, index, empty) : st;
            } else if (vk == VK.ALT_UP___) {
                int i1 = text.startOfLine(ci);
                if (0 < i1) {
                    int i0 = text.prevLine(i1);
                    int i2 = text.nextLine(i1);
                    return st.splice(i2, i2, text.text.subList(i0, i1)).splice(i0, i1, empty);
                } else
                    return st;
            } else if (vk == VK.ALT_DOWN_) {
                int i0 = text.startOfLine(ci);
                int i1 = text.nextLine(i0);
                if (i1 < text.length()) {
                    int i2 = text.nextLine(i1);
                    return st.splice(i1, i2, empty).splice(i0, i0, text.text.subList(i1, i2));
                } else
                    return st;
            } else if (vk == VK.DEL__)
                return st.splice(1, empty);
            else if (vk == VK.CTRL_K____)
                return st.splice(ci, text.endOfLine(ci), empty);
            else if (vk == VK.CTRL_U____)
                return st.splice(text.startOfLine(ci), ci, empty);
            else if (vk == VK.CTRL_D____)
                return st.splice(text.startOfLine(ci), text.nextLine(ci), empty);
            else if (vk == VK.CTRL_Y____)
                return redo != null ? redo : st;
            else if (vk == VK.CTRL_Z____) {
                State undo1 = undo != null ? undo : st;
                return new State(undo1.undo, st, undo1.text, oc, undo1.cursorCoord);
            } else if (vk == VK.CTRL_C____)
                return Fail.t();
            else if (ch != null)
                if (ch == 13) {
                    int i0 = text.startOfLine(ci);
                    int ix = i0;
                    char ch_;
                    while ((ch_ = text.at(ix)) == ' ' || ch_ == '\t') ix++;
                    return st.splice(0, IRope.ropeList("\n").concat(text.text.subList(i0, ix)));
                } else
                    return st.splice(0, IRope.ropeList(Character.toString(ch)));
            else
                return st;
        }))).apply((st, undo, redo, text, oc, cc) -> oc.apply((ox, oy) -> cc.apply((cx, cy) -> {
            IntIntPair cc_ = text.coord(sat(text.index(cx, cy), 0, text.length()));
            return st.cursor(cc_.t0, cc_.t1);
        }))).apply((st, undo, redo, text, oc, cc) -> oc.apply((ox, oy) -> cc.apply((cx, cy) -> {
            int x0 = Math.max(0, cx - viewSizeX + 1);
            int y0 = Math.max(0, cy - viewSizeY + 1);
            int ox_ = sat(ox, x0, cx);
            int oy_ = sat(oy, y0, cy);
            return st.offset(ox_, oy_);
        })));
        keyboard.loop(signal -> // 
        signal.fold(state0, // 
        (state, pair_) -> pair_.map((vk, ch) -> mutate.apply(vk, ch, state))).wire(redraw));
    }
}
Also used : LibcJna(suite.ansi.LibcJna) Termios(suite.ansi.Termios) Friends.min(suite.util.Friends.min) Native(com.sun.jna.Native) IntIntPair(suite.primitive.adt.pair.IntIntPair) Predicate(java.util.function.Predicate) IRope(suite.immutable.IRope) Friends.max(suite.util.Friends.max) IntSink(suite.primitive.IntPrimitives.IntSink) Keyboard(suite.ansi.Keyboard) IRopeList(suite.immutable.IRope.IRopeList) FixieFun6(suite.adt.pair.Fixie_.FixieFun6) VK(suite.ansi.Keyboard.VK) FixieFun3(suite.adt.pair.Fixie_.FixieFun3) Sink(suite.util.FunUtil.Sink) FileUtil(suite.os.FileUtil) Chars_(suite.primitive.Chars_) IntsBuilder(suite.primitive.Ints.IntsBuilder) Ints_(suite.primitive.Ints_) IntMutable(suite.primitive.IntMutable) Fail(suite.util.Fail) Keyboard(suite.ansi.Keyboard) VK(suite.ansi.Keyboard.VK) Termios(suite.ansi.Termios) IntIntPair(suite.primitive.adt.pair.IntIntPair)

Aggregations

Native (com.sun.jna.Native)1 Predicate (java.util.function.Predicate)1 FixieFun3 (suite.adt.pair.Fixie_.FixieFun3)1 FixieFun6 (suite.adt.pair.Fixie_.FixieFun6)1 Keyboard (suite.ansi.Keyboard)1 VK (suite.ansi.Keyboard.VK)1 LibcJna (suite.ansi.LibcJna)1 Termios (suite.ansi.Termios)1 IRope (suite.immutable.IRope)1 IRopeList (suite.immutable.IRope.IRopeList)1 FileUtil (suite.os.FileUtil)1 Chars_ (suite.primitive.Chars_)1 IntMutable (suite.primitive.IntMutable)1 IntSink (suite.primitive.IntPrimitives.IntSink)1 IntsBuilder (suite.primitive.Ints.IntsBuilder)1 Ints_ (suite.primitive.Ints_)1 IntIntPair (suite.primitive.adt.pair.IntIntPair)1 Fail (suite.util.Fail)1 Friends.max (suite.util.Friends.max)1 Friends.min (suite.util.Friends.min)1