Skip to content

Commit

Permalink
add ask, new, show hn
Browse files Browse the repository at this point in the history
  • Loading branch information
CER10TY committed Sep 17, 2019
1 parent 523bb62 commit c5c6beb
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 38 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to the "hncode2" extension will be documented in this file.

### [0.0.5]

* NEW: Browse Ask, Top, Show and New HN through Context Actions.
* NEW: Go straight to external URL when clicking on story, including Ask and Show HN.
* IN PROGRESS: Show link to comments when expanding a story.
* FIXED: Bad URL when clicking on HN self posts.
* FIXED: No longer show post ID next to post.
* IN PROGRESS: Display 1 to n numbers next to entries.

## [0.0.1]

- Initial release
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
<img src="https://raw.githubusercontent.com/cer10ty/hncode/master/public/images/HNCode.png" alt="HNCode Logo" height="250px" width="250px"></img>
</div>
</br>
<div align="center">
<h1>HNCode²</h1>
<h2>Visual Studio Code Extension to browse Hacker News (in a tree view)</h2>
</div>
</br>

# HNCode²

HNCode² is the successor to [HNCode](https://github.com/CER10TY/hncode), an extension no longer being developed. The idea behind HNCode was to provide a custom webview inside VSCode to browse Hacker News, eventually adding a code style view. However, due to the extreme simplicity of Hacker News itself, it made sense to instead provide it as a Tree View, for more discreet browsing in VSCode. This is HNCode².

## Features

* Browse top stories of HN in a tree view
* Browse Ask HN in a tree view
* Go straight to external URLs or self posts from the tree view
* Browse Top, New, Ask and Show HN in a tree view.
* Go straight to external URLs or self posts from the tree view.
* Expand the items to reveal amount of comments.
* Click on expanded items to go straight to the comments in Hacker News.

## Requirements

Expand All @@ -23,15 +23,25 @@ This extension requires Visual Studio Code 1.38.0 or later to work properly.

This extension contributes the following settings:

* `hncode.limitation`: Set the amount of items to be loaded on the front page.
* `hncode2.limitation`: Set the amount of items to be loaded on the front page.

## Known Issues

* It looks ugly.
* Not all tabs (like jobs, new) are implemented yet.
* Comments are not being pulled yet.
* Active tabs (ie. Ask HN) are not being greyed out yet.

## Release Notes

### 0.0.5

* NEW: Browse Ask, Top, Show and New HN through Context Actions.
* NEW: Go straight to external URL when clicking on story, including Ask and Show HN.
* IN PROGRESS: Show link to comments when expanding a story.
* FIXED: Bad URL when clicking on HN self posts.
* FIXED: No longer show post ID next to post.
* IN PROGRESS: Display 1 to n numbers next to entries.

### 0.0.1

Pre-alpha Release
42 changes: 35 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hncode2",
"displayName": "HNCode²",
"description": "Browse Hacker News in a tree view tab",
"version": "0.0.1",
"version": "0.0.5",
"publisher": "sjohanson",
"author": "Søren Johanson <[email protected]>",
"engines": {
Expand Down Expand Up @@ -39,11 +39,7 @@
"hncode": [
{
"id": "top-stories",
"name": "Top Stories"
},
{
"id": "ask-hn",
"name": "Ask HN"
"name": "Stories"
}
]
},
Expand All @@ -55,14 +51,46 @@
"light": "resources/light/refresh.svg",
"dark": "resources/dark/refresh.svg"
}
},
{
"command": "hncode.ask",
"title": "Ask HN"
},
{
"command": "hncode.top",
"title": "Top Stories"
},
{
"command": "hncode.new",
"title": "New Stories"
},
{
"command": "hncode.show",
"title": "Show HN"
}
],
"menus": {
"view/title": [
{
"command": "hncode.refresh",
"when": "view == top-stories || view == ask-hn",
"when": "view == top-stories",
"group": "navigation"
},
{
"command": "hncode.ask",
"when": "view == top-stories"
},
{
"command": "hncode.top",
"when": "view == top-stories"
},
{
"command": "hncode.new",
"when": "view == top-stories"
},
{
"command": "hncode.show",
"when": "view == top-stories"
}
]
},
Expand Down
24 changes: 24 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ export function getAsk(): Thenable<Array<number>> {
});
}

export function getNew(): Thenable<Array<number>> {
return new Promise<Array<number>>((c, e) => {
restClient.get<Array<number>>('newstories.json?print=pretty').then(response => {
if (response.result) {
c(response.result);
}
}).catch(error => {
e(error.message);
});
});
}

export function getShow(): Thenable<Array<number>> {
return new Promise<Array<number>>((c, e) => {
restClient.get<Array<number>>('showstories.json?print=pretty').then(response => {
if (response.result) {
c(response.result);
}
}).catch(error => {
e(error.message);
});
});
}

export function getStories(stories: Array<number>): Promise<HNData[]> {
return new Promise<HNData[]>(async (c, e) => {
let promises: Array<Promise<HNData>> = [];
Expand Down
18 changes: 7 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@ export async function activate(context: vscode.ExtensionContext) {
getStories(response).then(response => {
const treeDataProvider = new HNTreeDataProvider(response, "top");
vscode.window.registerTreeDataProvider('top-stories', treeDataProvider);
vscode.commands.registerCommand('hncode.refresh', () => treeDataProvider.refresh());
}).catch(error => {
vscode.window.showErrorMessage(error.mmesage);
});
});

// Ask HN
getAsk().then(response => {
getStories(response).then(response => {
const askProvider = new HNTreeDataProvider(response, "ask");
vscode.window.registerTreeDataProvider('ask-hn', askProvider);
vscode.commands.registerCommand('hncode.refresh', () => askProvider.refresh());
/* REGISTER COMMANDS */

vscode.commands.registerCommand('hncode.refresh', () => treeDataProvider.refresh());
vscode.commands.registerCommand('hncode.top', () => treeDataProvider.setIdentifier("top"));
vscode.commands.registerCommand('hncode.ask', () => treeDataProvider.setIdentifier("ask"));
vscode.commands.registerCommand('hncode.new', () => treeDataProvider.setIdentifier("new"));
vscode.commands.registerCommand('hncode.show', () => treeDataProvider.setIdentifier("show"));
}).catch(error => {
vscode.window.showErrorMessage(error.mmesage);
});
Expand Down
44 changes: 34 additions & 10 deletions src/hncodeprovider.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
import * as vscode from 'vscode';
import { HNData } from './interface';
import { getTop, getStories, getAsk } from './api';
import { getTop, getStories, getAsk, getNew, getShow } from './api';

export class HNTreeDataProvider implements vscode.TreeDataProvider<HNData> {

private _onDidChangeTreeData: vscode.EventEmitter<any> = new vscode.EventEmitter<any>();
readonly onDidChangeTreeData: vscode.Event<any> = this._onDidChangeTreeData.event;

constructor(private readonly stories: HNData[], private readonly identifier: string) {
constructor(private readonly stories: HNData[], public identifier: string) {
}

public refresh(): any {
this._onDidChangeTreeData.fire();
}
}

public setIdentifier(newIdentifier: string): any {
this.identifier = newIdentifier;
this.refresh();
}

public getTreeItem(element: HNData): vscode.TreeItem {
return new Story(`${element.id}: ${element.title}`, vscode.TreeItemCollapsibleState.None, element.url, {
let url: vscode.Uri = element.url ? element.url : <vscode.Uri><unknown>(`https://news.ycombinator.com/item?id=${element.id}`);

return new Story(`${element.title}`, vscode.TreeItemCollapsibleState.Collapsed, this.identifier, url, {
command: 'hncode.openurl',
title: '',
arguments: [element.url]
arguments: [url]
});
}

public getChildren(element?: HNData): Thenable<HNData[]> {
return new Promise<HNData[]>((c, e) => {
if (element) {
c(getStories(element.kids));
// Do nothing
} else {
switch(this.identifier) {
case "top":
Expand All @@ -46,6 +53,24 @@ export class HNTreeDataProvider implements vscode.TreeDataProvider<HNData> {
});
});
break;
case "new":
getNew().then(response => {
getStories(response).then(response => {
c(response);
}).catch(error => {
e(error.message);
});
});
break;
case "show":
getShow().then(response => {
getStories(response).then(response => {
c(response);
}).catch(error => {
e(error.message);
});
});
break;
}
}
});
Expand All @@ -55,7 +80,8 @@ export class Story extends vscode.TreeItem {

constructor(
public readonly label: string,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
public readonly cValue: string,
private url?: vscode.Uri,
public readonly command?: vscode.Command
) {
Expand All @@ -65,7 +91,5 @@ export class Story extends vscode.TreeItem {
get tooltip(): string {
return `${this.url}`;
}

contextValue = 'story';

contextValue = this.cValue;
}

0 comments on commit c5c6beb

Please sign in to comment.