Skip to content

Commit

Permalink
Merge pull request #32 from freyamade/better-pkg-install
Browse files Browse the repository at this point in the history
Better pkg installation
  • Loading branch information
freyamade authored Sep 27, 2023
2 parents 2fd6ac9 + 06282fb commit 25721d0
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 25 deletions.
42 changes: 22 additions & 20 deletions src/commands/pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ const summary: string = 'Manages extra packages, allows for listing and installa
const help: string = `<p class="green">pkg - ${summary}</p>
<br />
<p>Usage:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span class="yellow">pkg [package_name]</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span class="yellow">pkg [filter]</span></p>
<br />
<p>If <span class="yellow">package_name</span> is omitted, the command will list packages.</p>
<p>If <span class="yellow">package_name</span> is given, it will be used to search the list of packages.</p>
<p>If the search finds more than one package, the filtered list will be returned.</p>
<p>If the search matches the name of a package, it will be installed.</p>`
<p>If <span class="yellow">filter</span> is omitted, the command will list all available packages.</p>
<p>If <span class="yellow">filter</span> is given, it will be used to search the list.</p>
<p>If the search finds exactly one package, that package will be installed.</p>
<p>Otherwise, the list will be output with package names and descriptions.</p>`

const optDef = {}

Expand Down Expand Up @@ -62,24 +62,26 @@ function execute(state: EmulatorState, args: string[]): any {
}
}

// Print out the table
for (let index = 0; index < packages.length; index++) {
const pkgName = packages[index]
// If there's only one package in the list, install it
if (packages.length === 1) {
const pkgName = packages[0];
const pkgDetails = PKGList[pkgName]

// Check if the package name matches the arg, if so install.
if (pkgName === filter) {
if (pkgDetails.installed) {
return {
output: OutputFactory.makeErrorOutput({
source: 'pkg',
type: `Package "${pkgName}" is already installed.`,
}),
}
if (pkgDetails.installed) {
return {
output: OutputFactory.makeErrorOutput({
source: 'pkg',
type: `Package "${pkgName}" is already installed.`,
}),
}
pkgDetails.installed = true
return install(pkgName)
}
pkgDetails.installed = true
return install(pkgName)
}

// Print out the table if multiple are returned
for (let index = 0; index < packages.length; index++) {
const pkgName = packages[index]
const pkgDetails = PKGList[pkgName]

if (pkgDetails.installed) {
messageBody.push(`<tr><td>${pkgName}</td><td>${pkgDetails.summary} (installed)</td></tr>`)
Expand Down
18 changes: 14 additions & 4 deletions src/commands/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@ const optDef = {}

// Define the function
function execute(state: EmulatorState, args: string[]): any {
console.log('execute')
// Iterate through the command mapping to find all the commands in the system, and print out their summary messages
const messageBody: string[] = []
const internCommands: string[] = []
const externCommands: string[] = [] // track external commands separately to display them separately
state.getCommandMapping().forEach((details, name) => {
console.log(name, details)
const message = `<tr><td>${name}</td><td>${details.get('summary')}</td></tr>`
messageBody.push(message)
if (details.get('extern') != true) {
internCommands.push(message)
}
else {
externCommands.push(message)
}
})
const output = `<table class="summary-table">
<tr><th colspan="2"><span class="magenta">freyama.de</span> currently supports the following commands;</th></tr>
${messageBody.sort().join('')}
<tr><th colspan="2"><span class="magenta">fresh</span> has the following built-in commands:</th></tr>
${internCommands.sort().join('')}` + (externCommands.length > 0 ? `
<tr><th colspan="2"><span class="magenta">fresh</span> also has the following installed <span class="yellow">pkg</span> commands:</th></tr>
${externCommands.sort().join('')}` : ``) + `
<tr><th colspan="2">Run <span class="yellow">'help command'</span> for more information on the specified command.</th></tr>
</table>`
return { output: OutputFactory.makeTextOutput(output) }
Expand Down
5 changes: 5 additions & 0 deletions src/file_system/changes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export const Changes = `<p># <span class="yellow underline">Latest Changes</span></p>
<p>## <span class="cyan underline">2023.09.27</span><p>
<p><span class="green">-</span> Improved <span class="yellow">pkg</span> installation.</p>
<p>&nbsp;&nbsp;<span class="green">-</span> Now installs if the given filter finds exactly 1 package.</p>
<p><span class="green">-</span> The <span class="yellow">?</span> command now displays installed packages separately.</p>
<br />
<p>## <span class="cyan underline">2023.09.26</span><p>
<p><span class="green">-</span> Minor update to add the <span class="green">github-user-languages</span> package.</p>
<br />
Expand Down
1 change: 0 additions & 1 deletion src/fresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ export class Fresh {
* Handle tab completion by attempting to autocomplete the current input
*/
private tabComplete() {
// TODO - At a later stage, set up suggestions `this.terminal.suggest(this.state, this.input): string[]`
const suggestions = this.terminal.suggest(this.state, this.input)
if (suggestions.length == 1) {
this.input = this.terminal.autocomplete(this.state, this.input)
Expand Down
1 change: 1 addition & 0 deletions src/pkg/game-of-life/game-of-life.pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ const Life = {
optDef: optDef,
help: help,
summary: summary,
extern: true,
}

// Add the command to the Window
Expand Down
1 change: 1 addition & 0 deletions src/pkg/github-user-languages/github-user-languages.pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const GHUL = {
optDef: optDef,
help: help,
summary: summary,
extern: true,
}

// Add the command to the Window
Expand Down

0 comments on commit 25721d0

Please sign in to comment.