Merge pull request #49 from ueokande/fix-console-tokenizer

Fix console tokenizer
jh-changes
Shin'ya Ueoka 7 years ago committed by GitHub
commit 1418890824
  1. 58
      src/shared/commands.js

@ -1,20 +1,21 @@
import * as tabs from 'background/tabs'; import * as tabs from 'background/tabs';
import * as histories from 'background/histories'; import * as histories from 'background/histories';
const normalizeUrl = (string, searchConfig) => { const normalizeUrl = (args, searchConfig) => {
let concat = args.join(' ');
try { try {
return new URL(string).href; return new URL(concat).href;
} catch (e) { } catch (e) {
if (string.includes('.') && !string.includes(' ')) { if (concat.includes('.') && !concat.includes(' ')) {
return 'http://' + string; return 'http://' + concat;
} }
let query = encodeURI(string); let query = encodeURI(concat);
let template = searchConfig.engines[ let template = searchConfig.engines[
searchConfig.default searchConfig.default
]; ];
for (let key in searchConfig.engines) { for (let key in searchConfig.engines) {
if (string.startsWith(key + ' ')) { if (args[0] === key) {
query = encodeURI(string.replace(key + ' ', '')); query = args.slice(1).join(' ');
template = searchConfig.engines[key]; template = searchConfig.engines[key];
} }
} }
@ -87,40 +88,57 @@ const getOpenCompletions = (command, keywords, searchConfig) => {
}); });
}; };
const doCommand = (name, remaining, settings) => { const doCommand = (line, settings) => {
let words = line.trim().split(/ +/);
let name = words.shift();
switch (name) { switch (name) {
case 'o': case 'o':
case 'open': case 'open':
return openCommand(normalizeUrl(remaining, settings.search)); return openCommand(normalizeUrl(words, settings.search));
case 't': case 't':
case 'tabopen': case 'tabopen':
return tabopenCommand(normalizeUrl(remaining, settings.search)); return tabopenCommand(normalizeUrl(words, settings.search));
case 'w': case 'w':
case 'winopen': case 'winopen':
return winopenCommand(normalizeUrl(remaining, settings.search)); return winopenCommand(normalizeUrl(words, settings.search));
case 'b': case 'b':
case 'buffer': case 'buffer':
return bufferCommand(remaining); return bufferCommand(words);
case '':
return Promise.resolve();
} }
throw new Error(name + ' command is not defined'); throw new Error(name + ' command is not defined');
}; };
const getCompletions = (command, keywords, settings) => { const getCompletions = (line, settings) => {
switch (command) { let typedWords = line.trim().split(/ +/);
let typing = '';
if (!line.endsWith(' ')) {
typing = typedWords.pop();
}
if (typedWords.length === 0) {
return Promise.resolve([]);
}
let name = typedWords.shift();
let keywords = typedWords.concat(typing).join(' ');
switch (name) {
case 'o': case 'o':
case 'open': case 'open':
case 't': case 't':
case 'tabopen': case 'tabopen':
case 'w': case 'w':
case 'winopen': case 'winopen':
return getOpenCompletions(command, keywords, settings.search); return getOpenCompletions(name, keywords, settings.search);
case 'b': case 'b':
case 'buffer': case 'buffer':
return tabs.getCompletions(keywords).then((gotTabs) => { return tabs.getCompletions(keywords).then((gotTabs) => {
let items = gotTabs.map((tab) => { let items = gotTabs.map((tab) => {
return { return {
caption: tab.title, caption: tab.title,
content: command + ' ' + tab.title, content: name + ' ' + tab.title,
url: tab.url, url: tab.url,
icon: tab.favIconUrl icon: tab.favIconUrl
}; };
@ -137,15 +155,11 @@ const getCompletions = (command, keywords, settings) => {
}; };
const exec = (line, settings) => { const exec = (line, settings) => {
let name = line.split(' ')[0]; return doCommand(line, settings);
let remaining = line.replace(name + ' ', '');
return doCommand(name, remaining, settings);
}; };
const complete = (line, settings) => { const complete = (line, settings) => {
let command = line.split(' ', 1)[0]; return getCompletions(line, settings);
let keywords = line.replace(command + ' ', '');
return getCompletions(command, keywords, settings);
}; };
export { exec, complete }; export { exec, complete };