Skip to content
Chris R edited this page Feb 9, 2017 · 5 revisions

Welcome to the codec wiki!

This is my first effort to release a library I've written for general consumption, so any feedback is welcome.

To Do

See the main README for a list of items that I'd like to add to the module.

Miscellaneous

Formatting Assistance in Atom

Since I write so many packet formats in the Elixir style, I wrote an Atom macro to fix the alignment of a bunch of selected packet format instructions.

For example, this:

<<
  some :: 32-default(5),
  fields :: 8,
  put_together :: 8,
>>

is transformed to this:

<<
  some          :: 32-default(5),
  fields        :: 8,
  put_together  :: 8,
>>

Place the following in your ~/.atom/init.coffee file. After opening a new window, the macro will be available. You can pull up the command palette under the View->Toggle Command Palette item.

atom.commands.add 'atom-text-editor', 'elixir:reformat-bit-layout', ->
  return unless editor = atom.workspace.getActiveTextEditor()

  selection = editor.getLastSelection()
  text = selection.getText()
  arrayOfLines = text.split(/\n/)
  spaces = arrayOfLines[0].match(/^\s*/)[0]
  keywords = []
  formats = []
  keywords.push(line.split(/::/)[0].trim()) for line in arrayOfLines
  formats.push(line.split(/::/)[1]) for line in arrayOfLines
  longestWord = keywords.reduce((a, b) -> if a.length > b.length then a else b )
  tabLength = editor.getTabLength()
  wordPlusSpaces = longestWord.length + tabLength * 2
  newKeywords = []
  newKeywords.push(word + Array(wordPlusSpaces - word.length + 1).join(" ")) for word in keywords
  newLines = []
  newLines.push(spaces + word + "::" + formats.shift()) for word in newKeywords
  editor.insertText(newLines.join("\n"))

Clone this wiki locally