use of suite.util.FunUtil.Sink 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));
}
}
use of suite.util.FunUtil.Sink in project suite by stupidsing.
the class LazyIbTreeExtentFilePersister method gc.
@Override
public Map<Extent, Extent> gc(List<Extent> roots, int back) {
synchronized (writeLock) {
int end = nPages;
int start = max(0, end - back);
Set<Extent> isInUse = new HashSet<>();
Sink<List<Extent>> use = extents_ -> {
for (Extent extent : extents_) if (start <= extent.start)
isInUse.add(extent);
};
use.sink(roots);
List<Extent> extents = extentFile.scan(start, end);
for (Extent extent : Read.from(extents).reverse()) if (isInUse.contains(extent))
use.sink(Read.from(loadSlot(extent).pairs).map(Pair::second).toList());
Map<Extent, Extent> map = new HashMap<>();
if (!extents.isEmpty()) {
int pointer = extents.get(0).start;
for (Extent extent0 : extents) if (isInUse.contains(extent0)) {
PersistSlot<T> ps0 = loadSlot(extent0);
List<Pair<T, Extent>> pairs0 = ps0.pairs;
List<Pair<T, Extent>> pairsx = Read.from(pairs0).map(Pair.map1(p -> map.getOrDefault(p, p))).toList();
PersistSlot<T> psx = new PersistSlot<>(pairsx);
Extent extentx = saveSlot(pointer, psx);
pointer = extentx.end;
map.put(extent0, extentx);
}
nPages = pointer;
slotsByExtent.clear();
}
return map;
}
}
use of suite.util.FunUtil.Sink in project suite by stupidsing.
the class SewingProverImpl method save.
private Restore save(Runtime rt) {
Cps cps0 = rt.cps;
Env env0 = rt.env;
Node query0 = rt.query;
IList<Trampoline> cutPoint0 = rt.cutPoint;
IList<Trampoline> rems0 = rt.rems;
int pit0 = rt.trail.getPointInTime();
Sink<Node> handler0 = rt.handler;
return rt_ -> {
rt_.cps = cps0;
rt_.env = env0;
rt_.query = query0;
rt_.cutPoint = cutPoint0;
rt_.rems = rems0;
rt_.trail.unwind(pit0);
rt_.handler = handler0;
};
}
use of suite.util.FunUtil.Sink in project suite by stupidsing.
the class DblFunUtil method suck.
/**
* Sucks data from a sink and produce into a source.
*/
public static DblSource suck(Sink<DblSink> fun) {
NullableSyncQueue<Double> queue = new NullableSyncQueue<>();
DblSink enqueue = c -> enqueue(queue, c);
Thread thread = Thread_.startThread(() -> {
try {
fun.sink(enqueue);
} finally {
enqueue(queue, EMPTYVALUE);
}
});
return () -> {
try {
return queue.take();
} catch (InterruptedException ex) {
thread.interrupt();
return Fail.t(ex);
}
};
}
use of suite.util.FunUtil.Sink in project suite by stupidsing.
the class FltFunUtil method suck.
/**
* Sucks data from a sink and produce into a source.
*/
public static FltSource suck(Sink<FltSink> fun) {
NullableSyncQueue<Float> queue = new NullableSyncQueue<>();
FltSink enqueue = c -> enqueue(queue, c);
Thread thread = Thread_.startThread(() -> {
try {
fun.sink(enqueue);
} finally {
enqueue(queue, EMPTYVALUE);
}
});
return () -> {
try {
return queue.take();
} catch (InterruptedException ex) {
thread.interrupt();
return Fail.t(ex);
}
};
}
Aggregations