The devto module
Tags: go, showdev, tutorial
Published: 2019-09-20 17:53:43

:warning: The module is up and awaiting for review :warning:

Here is the source code

{% github https://github.com/wtfutil/wtf/pull/644 %}

## Thoughts on the process

There is a lack of explicit information on how to create modules, so if you are eager to create a module you will need to read tons and tons of code.

Another thing that I found tricky was properly configuring the keyboard integration, but there are amazing modules with this feature that you can copy and modify.

Make sure to read the `CONTRIBUTING.md` file before starting, it will allow you to plan the development with all the requirements.

Finally, please update the docs, if you take a look at the source code, you will find a lot of good modules that have poor or no documentation at all.

## The process

1. Get familiar with Go Modules as it is the dependency management system used by `wtfutil/wtf`.

2. Check the existing modules so that you can get ideas and also so that you understand how everything works.

3. If you're going to install some modules this is the time, for the current module, I just used the API client we built along this series.
{% github VictorAvelar/devto-api-go %}

4. Go to the modules directory and create a sub directory to start coding, for this module it is `modules/devto`

## The files

It will be possible to have everything in one file, but I like to split the code so that the filename refers to the content.

After checking how the `hackernews` module works (which heavily inspired this whole series) I have a good idea of what I need.

In `keyboard.go` we are going to place all the keyboard bindings for our module, we need to be able to navigate from one history to another, and also we need to be able to open the content in our default browser.

So to make things easier, I will put here a screenshot from the module documentation:

![Keyboard bindings](https://thepracticaldev.s3.amazonaws.com/i/smmryolxwlvpljkexlwx.png)

Code wise it is not that amazing, here you have the fragment that accomplishes this functionality

```go
func (widget *Widget) initializeKeyboardControls() {
	widget.InitializeCommonControls(widget.Refresh)

	widget.SetKeyboardKey(tcell.KeyDown, widget.Next, "Select next item")
	widget.SetKeyboardKey(tcell.KeyUp, widget.Prev, "Select previous item")
	widget.SetKeyboardKey(tcell.KeyEnter, widget.openStory, "Open story in browser")
	widget.SetKeyboardKey(tcell.KeyEsc, widget.Unselect, "Clear selection")
}
```

In `settings.go` you grab the values from the user's `config.yaml` file and put it into your Widget.

![Settings](https://thepracticaldev.s3.amazonaws.com/i/48mkcifly63jo5teqiv2.png)

And here is the yaml to use this module:

```yaml
devto:
  enabled: true
  numberOfArticles: 10
  position:
    top: 1
    left: 1
    height: 1
    width: 2
  contentTag: "showdev" 
  contentUsername: "victoravelar"
  contentState: "rising"
```

And finally the `widget.go`, TBH this is just the hackernews widget file with some customization to make it work with the dev.to API.

## Conclusions

It was not as hard as expected, and I hope a lot of people is able to get the articles from this community straight to their terminal.

Do your own and please let the world know.