From 966ab92b6f1451b5b02ab451d38eda949a028ef6 Mon Sep 17 00:00:00 2001 From: Ketmar Dark Date: Sat, 30 Apr 2016 20:50:31 +0300 Subject: [PATCH] most console input line management moved to "iv.cmdcon" --- render.d | 179 ++++++++---------------------------------------------------- xmain_d2d.d | 8 +-- 2 files changed, 26 insertions(+), 161 deletions(-) diff --git a/render.d b/render.d index e5211c9..0db7f8a 100644 --- a/render.d +++ b/render.d @@ -98,54 +98,9 @@ __gshared uint concmdbufpos; private import core.sync.mutex : Mutex; shared static this () { concmdbuf.length = 65536; } -__gshared char[4096] concli = 0; -__gshared uint conclilen = 0; - -__gshared char[4096][128] concmdhistory = void; -__gshared int conhisidx = -1; -shared static this () { foreach (ref hb; concmdhistory) hb[] = 0; } - __gshared int conskiplines = 0; -const(char)[] conhisAt (int idx) { - if (idx < 0 || idx >= concmdhistory.length) return null; - const(char)[] res = concmdhistory.ptr[idx][]; - usize pos = 0; - while (pos < res.length && res.ptr[pos]) ++pos; - return res[0..pos]; -} - - -int conhisFind (const(char)[] cmd) { - while (cmd.length && cmd[$-1] <= 32) cmd = cmd[0..$-1]; - if (cmd.length > concmdhistory.ptr[0].length) cmd = cmd[0..concmdhistory.ptr[0].length]; - if (cmd.length == 0) return -1; - foreach (int idx; 0..cast(int)concmdhistory.length) { - auto c = conhisAt(idx); - while (c.length > 0 && c[$-1] <= 32) c = c[0..$-1]; - if (c == cmd) return idx; - } - return -1; -} - - -void conhisAdd (const(char)[] cmd) { - while (cmd.length && cmd[$-1] <= 32) cmd = cmd[0..$-1]; - if (cmd.length > concmdhistory.ptr[0].length) cmd = cmd[0..concmdhistory.ptr[0].length]; - if (cmd.length == 0) return; - auto idx = conhisFind(cmd); - if (idx >= 0) { - // remove command - foreach (immutable c; idx+1..concmdhistory.length) concmdhistory.ptr[c-1][] = concmdhistory.ptr[c][]; - } - // make room - foreach (immutable c; 1..concmdhistory.length; reverse) concmdhistory.ptr[c][] = concmdhistory.ptr[c-1][]; - concmdhistory.ptr[0][] = 0; - concmdhistory.ptr[0][0..cmd.length] = cmd[]; -} - - void concmdAdd (const(char)[] s) { if (s.length) { if (concmdbuf.length-concmdbufpos < s.length+1) { @@ -157,6 +112,7 @@ void concmdAdd (const(char)[] s) { } } + // `null`: no more void concmdDoAll () { if (concmdbufpos == 0) return; @@ -181,121 +137,19 @@ void concmdDoAll () { void concliChar (char ch) { - __gshared int prevWasEmptyAndTab = 0; - + if (!ch) return; consoleLock(); scope(exit) consoleUnlock(); - //conLastChange = 0; - // autocomplete - if (ch == 9) { - if (conclilen == 0) { - if (++prevWasEmptyAndTab < 2) return; - } else { - prevWasEmptyAndTab = 0; - } - if (conclilen > 0) { - string minPfx = null; - // find longest command - foreach (auto name; conByCommand) { - if (name.length >= conclilen && name.length > minPfx.length && name[0..conclilen] == concli[0..conclilen]) minPfx = name; - } - //conwriteln("longest command: [", minPfx, "]"); - // find longest prefix - foreach (auto name; conByCommand) { - if (name.length < conclilen) continue; - if (name[0..conclilen] != concli[0..conclilen]) continue; - usize pos = 0; - while (pos < name.length && pos < minPfx.length && minPfx.ptr[pos] == name.ptr[pos]) ++pos; - if (pos < minPfx.length) minPfx = minPfx[0..pos]; - } - if (minPfx.length > concli.length) minPfx = minPfx[0..concli.length]; - //conwriteln("longest prefix : [", minPfx, "]"); - if (minPfx.length >= conclilen) { - // wow! - bool doRet = (minPfx.length > conclilen); - conLastChange = 0; - concli[0..minPfx.length] = minPfx[]; - conclilen = cast(uint)minPfx.length; - if (conclilen < concli.length && conHasCommand(minPfx)) { - concli.ptr[conclilen++] = ' '; - doRet = true; - } - if (doRet) return; - } - } - // nope, print all available commands - bool needDelimiter = true; - foreach (auto name; conByCommand) { - if (conclilen > 0) { - if (name.length < conclilen) continue; - if (name[0..conclilen] != concli[0..conclilen]) continue; - } - if (needDelimiter) { conwriteln("----------------"); needDelimiter = false; } - conwriteln(name); - } - return; - } - // process other keys - prevWasEmptyAndTab = 0; - // remove last char - if (ch == 8) { - if (conclilen > 0) { conLastChange = 0; --conclilen; } - return; - } - // execute command - if (ch == 13) { - if (conskiplines) { conskiplines = 0; conLastChange = 0; } - if (conclilen > 0) { - conLastChange = 0; - conhisidx = -1; - conhisAdd(concli[0..conclilen]); - concmdAdd(concli[0..conclilen]); - conclilen = 0; - } - return; - } - // ^Y - if (ch == 25) { - if (conclilen > 0) { conLastChange = 0; conclilen = 0; } - return; - } - // up - if (ch == '\x01') { - ++conhisidx; - auto cmd = conhisAt(conhisidx); - if (cmd.length == 0) { - --conhisidx; - } else { - concli[0..cmd.length] = cmd[]; - conclilen = cast(uint)cmd.length; - conLastChange = 0; - } - return; - } - // down - if (ch == '\x02') { - --conhisidx; - auto cmd = conhisAt(conhisidx); - if (cmd.length == 0 && conhisidx < -1) { - ++conhisidx; - } else { - concli[0..cmd.length] = cmd[]; - conclilen = cast(uint)cmd.length; - conLastChange = 0; - } - return; - } - // page up - if (ch == '\x03') { + if (ch == ConInputChar.PageUp) { int lnx = (rConsoleHeight-4)/conCharHeight-2; if (lnx < 1) lnx = 1; conskiplines += lnx; conLastChange = 0; return; } - // page down - if (ch == '\x04') { + + if (ch == ConInputChar.PageDown) { if (conskiplines > 0) { int lnx = (rConsoleHeight-4)/conCharHeight-2; if (lnx < 1) lnx = 1; @@ -304,12 +158,23 @@ void concliChar (char ch) { } return; } - // other - if (ch < ' ' || ch > 127) return; - if (ch == '`' && conclilen == 0) { concmd("r_console ona"); return; } - if (conclilen >= concli.length) return; - concli.ptr[conclilen++] = ch; - conLastChange = 0; + + if (ch == ConInputChar.Return) { + if (conskiplines) { conskiplines = 0; conLastChange = 0; } + auto s = conInputBuffer; + if (s.length > 0) { + concmdAdd(s); + conInputBufferClear(true); // add to history + conLastChange = 0; + } + return; + } + + if (ch == '`' && conInputBuffer.length == 0) { concmd("r_console ona"); return; } + + auto pcc = conInputLastChange(); + conAddInputChar(ch); + if (pcc != conInputLastChange()) conLastChange = 0; } diff --git a/xmain_d2d.d b/xmain_d2d.d index 71945c7..686aebf 100644 --- a/xmain_d2d.d +++ b/xmain_d2d.d @@ -232,10 +232,10 @@ void main (string[] args) { plrKeyUpDown(0, PLK_JUMP, false); plrKeyUpDown(0, PLK_FIRE, false); plrKeyUpDown(0, PLK_USE, false); - if (event.pressed && event.key == Key.Up) postChar('\x01'); - else if (event.pressed && event.key == Key.Down) postChar('\x02'); - else if (event.pressed && event.key == Key.PageUp) postChar('\x03'); - else if (event.pressed && event.key == Key.PageDown) postChar('\x04'); + if (event.pressed && event.key == Key.Up) postChar(ConInputChar.Up); + else if (event.pressed && event.key == Key.Down) postChar(ConInputChar.Down); + else if (event.pressed && event.key == Key.PageUp) postChar(ConInputChar.PageUp); + else if (event.pressed && event.key == Key.PageDown) postChar(ConInputChar.PageDown); else postKeyEvent(event); } }, -- 2.11.4.GIT