Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 81 additions & 19 deletions src/js/components/Terminal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {

import { os, pluginMap, uuidv4, getShortcuts, modCommands } from './terminal-utils';
import { Base, ContainerWrapper, Note } from './styled-elements';

import Bar from '../Bar';
import Content from '../Content/index';
import Tabs from '../Tabs/index';
Expand Down Expand Up @@ -86,6 +85,10 @@ class Terminal extends Component {
method: this.showHelp,
needsInstance: true,
},
search: {
method: this.searchHelp,
needsInstance: true,
},
echo: input => input.slice(1).join(' '),
'edit-line': {
method: this.editLine,
Expand All @@ -105,6 +108,7 @@ class Terminal extends Component {
show: (props.msg && props.msg.length > 0) ? 'show the msg' : false,
clear: 'clear the screen',
help: 'list all the commands',
search: 'Search the web',
echo: false,
'edit-line': false,
};
Expand Down Expand Up @@ -671,23 +675,23 @@ class Terminal extends Component {
(
<span>
{contents.filter(item => typeof item !== 'undefined').map((item) => {
const styles = {
marginRight: 5,
width: 'calc(33% - 5px)',
display: 'inline-block',
};
if (contents.length > 3) {
styles.marginBottom = 5;
}
return (
<span
style={styles}
key={`${item.target}-${item.rating}`}
>
{item.target}
</span>
);
})}
const styles = {
marginRight: 5,
width: 'calc(33% - 5px)',
display: 'inline-block',
};
if (contents.length > 3) {
styles.marginBottom = 5;
}
return (
<span
style={styles}
key={`${item.target}-${item.rating}`}
>
{item.target}
</span>
);
})}
</span>
),
false,
Expand Down Expand Up @@ -874,6 +878,64 @@ class Terminal extends Component {
handleLogging('info', this.printToActive);
};


// List all the commands (state + user defined)
searchHelp = (args, printLine, runCommand, instance) => {
// get the repo name
const repoName = Object.values(args).join(" ");

const query = `
query {
search(query: "${repoName} in:name", type: REPOSITORY, first: 10) {
edges {
node {
... on Repository {
name
url
}
}
}
}
}
`;

try {
fetch("https://api.github.com/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ query }),
}).then(response => {
// waiting for the response to come
// fetch just return promise
// alternative -> use async/await which will wait for the response
// await fetch ()
return response.json();
}).then(result => {
const edges = result.data.search.edges;
if (edges && edges.length > 0) {
const summaryData = [];

edges.forEach(edge => {
const repo = edge.node;
summaryData.push(`${repo.name}: ${repo.url}`);
});

instance.setState(prevState => ({
summary: [...prevState.summary, ...summaryData]
}));

} else {
printLine("❌ No repositories found.");
}
})
} catch (error) {
printLine("⚠️ Error fetching data from GitHub.");
// have meaningful message to be shown
console.error(error);
}
}
// List all the commands (state + user defined)
showHelp = (args, printLine, runCommand, instance) => {
let commands = { ...this.state.commands };
Expand All @@ -896,7 +958,7 @@ class Terminal extends Component {
for (const option of options) {
// eslint-disable-line no-restricted-syntax
if (descriptions[option] !== false) {
printLine(`${option} - ${descriptions[option]}`);
printLine(`${option} - ${descriptions[option]} `);
}
}
};
Expand Down