Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,092 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-flow", "@babel/preset-env"], | ||
"plugins": ["@babel/plugin-proposal-class-properties"] | ||
"presets": ["@babel/preset-flow", "@babel/preset-env"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
var blessed = require('blessed'), | ||
program = blessed.program(); | ||
|
||
const state = { text: '', mode: 'insert', pos: 0 }; | ||
|
||
program.on('keypress', function(ch, evt) { | ||
if (evt.full === 'enter' || evt.full === 'return') { | ||
return; | ||
} | ||
if (state.mode === 'insert') { | ||
if (evt.full === 'escape') { | ||
state.mode = 'normal'; | ||
program.selData(2, 3); | ||
debug(state); | ||
return; | ||
} | ||
if (evt.full === 'backspace') { | ||
state.pos -= 1; | ||
program.move(state.pos, 0); | ||
state.text = | ||
state.text.slice(0, state.pos) + | ||
state.text.slice(state.pos + 1); | ||
program.deleteChars(1); | ||
debug(state); | ||
return; | ||
} | ||
if (!ch) { | ||
return; | ||
} | ||
state.text = | ||
state.text.slice(0, state.pos) + ch + state.text.slice(state.pos); | ||
if (state.pos < state.text.length) { | ||
program.insertChars(1); | ||
} | ||
program.write(ch); | ||
state.pos += 1; | ||
} else { | ||
if (ch === 'h' || evt.full === 'left') { | ||
if (state.pos > 0) { | ||
state.pos -= 1; | ||
} | ||
} | ||
if (ch === 'l' || evt.full === 'right') { | ||
if (state.pos < state.text.length) { | ||
state.pos += 1; | ||
} | ||
} | ||
if (ch === 'i') { | ||
state.mode = 'insert'; | ||
} | ||
if (ch === 'a') { | ||
if (state.pos < state.text.length) { | ||
state.pos += 1; | ||
} | ||
state.mode = 'insert'; | ||
} | ||
if (ch === 'x') { | ||
program.deleteChars(1); | ||
state.text = | ||
state.text.slice(0, state.pos) + | ||
state.text.slice(state.pos + 1); | ||
} | ||
} | ||
debug(state); | ||
program.move(state.pos, 0); | ||
}); | ||
|
||
const debug = state => { | ||
program.cursorShape(state.mode === 'insert' ? 'line' : 'block'); | ||
program.cursorColor(9); | ||
program.move(0, 5); | ||
program.eraseInLine(2); | ||
program.write(state.text); | ||
program.move(state.pos, 0); | ||
}; | ||
|
||
program.key('q', function(ch, key) { | ||
program.clear(); | ||
program.disableMouse(); | ||
// program.showCursor(); | ||
program.normalBuffer(); | ||
process.exit(0); | ||
}); | ||
|
||
program.on('mouse', function(data) { | ||
if (data.action === 'mousemove') { | ||
program.move(data.x, data.y); | ||
program.bg('red'); | ||
program.write('x'); | ||
program.bg('!red'); | ||
} | ||
}); | ||
|
||
program.alternateBuffer(); | ||
// program.enableMouse(); | ||
// program.hideCursor(); | ||
program.clear(); | ||
|
||
program.move(0, 0); | ||
program.write('m'); | ||
// program.bg('black'); | ||
// program.write('Hello world', 'blue fg'); | ||
// program.setx(((program.cols / 2) | 0) - 4); | ||
// program.down(5); | ||
// program.write('Hi again!'); | ||
// program.bg('!black'); | ||
program.feed(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env node | ||
// @flow | ||
require('@babel/register'); | ||
|
||
const crdt = require('./tree'); | ||
|
||
const noop = (a, b) => a; | ||
|
||
/*:: | ||
import type {CRDT} from './tree' | ||
type Format = {bold?: boolean, underline?: boolean} | ||
*/ | ||
|
||
const chalk = require('chalk'); | ||
const format = (text, format) => { | ||
if (format.bold) { | ||
text = chalk.bold(text); | ||
} | ||
if (format.underline) { | ||
text = chalk.underline(text); | ||
} | ||
return text; | ||
}; | ||
|
||
const state /*:CRDT<Format>*/ = crdt.init('a', []); | ||
console.log(crdt.toString(state), crdt.toDebug(state)); | ||
const d1 = crdt.localInsert(state, 0, 'hello', null); | ||
crdt.apply(state, d1, noop); | ||
console.log(crdt.toString(state), crdt.toDebug(state)); | ||
// console.log(crdt.locForPos(state, 0)); | ||
// console.log(crdt.locForPos(state, 1)); | ||
// console.log(crdt.locForPos(state, 2)); | ||
const d2 = crdt.localInsert(state, 2, 'world', null); | ||
console.log(d2); | ||
crdt.apply(state, d2, noop); | ||
console.log(crdt.toString(state), crdt.toDebug(state)); | ||
crdt.apply(state, crdt.localFormat(state, 2, 2, { bold: true }), (a, b) => | ||
Object.assign({}, a, b), | ||
); | ||
|
||
console.log(crdt.toString(state, format), crdt.toDebug(state)); |
Oops, something went wrong.