├── .gitignore
├── LICENSE.md
├── README.md
├── lib
├── hex-view.coffee
└── hex.coffee
├── menus
└── hex.cson
├── package.json
└── styles
└── hex.less
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | npm-debug.log
3 | node_modules
4 | images
5 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | This software is released under the MIT license:
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of
4 | this software and associated documentation files (the "Software"), to deal in
5 | the Software without restriction, including without limitation the rights to
6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7 | the Software, and to permit persons to whom the Software is furnished to do so,
8 | subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Hex Viewer for [Atom](http://atom.io)
2 |
3 | View the hex dump of any file
4 |
5 | ## Install
6 |
7 | apm install hex
8 |
9 | If you want a shortcut, you can open your `keymap.cson` file and add this:
10 |
11 | '.editor:not(.mini)':
12 | 'shift-ctrl-h': 'hex:view'
13 |
14 | ---
15 |
16 | 
17 |
--------------------------------------------------------------------------------
/lib/hex-view.coffee:
--------------------------------------------------------------------------------
1 | {$, ScrollView} = require 'atom-space-pen-views'
2 | {Disposable} = require 'atom'
3 | path = require 'path'
4 | fs = require 'fs-plus'
5 | entities = null
6 |
7 | module.exports =
8 | class HexView extends ScrollView
9 | @content: ->
10 | @div class: 'hex-view padded pane-item', tabindex: -1, =>
11 | @div class: 'hex-dump', outlet: 'hexDump'
12 |
13 | initialize: ({@filePath}) =>
14 | super
15 |
16 | attached: ->
17 | entities ?= require 'entities'
18 | @hexFile(@filePath)
19 |
20 | @hexDump.css
21 | 'font-family': atom.config.get('editor.fontFamily')
22 | 'font-size': atom.config.get('editor.fontSize')
23 |
24 | hexFile: (filePath) ->
25 | stream = fs.ReadStream(filePath)
26 | stream.on 'data', (chunk) =>
27 | @hex chunk if @hexDump.is(':empty')
28 |
29 | getPath: -> @filePath
30 |
31 | getURI: -> @filePath
32 |
33 | getTitle: -> "#{path.basename(@getPath())} Hex"
34 |
35 | # NOP to remove deprecation. This kind of sucks
36 | onDidChangeTitle: ->
37 | new Disposable()
38 |
39 | onDidChangeModified: ->
40 | new Disposable()
41 |
42 | # Based on "node-hex" by Gabriel Llamas
43 | # https://github.com/gagle/node-hex
44 |
45 | hex: (buffer) =>
46 | bytesPerLine = atom.config.get('hex.bytesPerLine')
47 | rows = Math.ceil(buffer.length / bytesPerLine)
48 | last = buffer.length % bytesPerLine or bytesPerLine
49 | offsetLength = buffer.length.toString(16).length
50 | offsetLength = 6 if offsetLength < 6
51 |
52 | offset = "Offset:"
53 | hex = ""
54 | ascii = ""
55 |
56 | i = 0
57 | while i < bytesPerLine
58 | offset += " #{zero(i, 2)}"
59 | i++
60 |
61 | b = 0
62 | lastBytes = undefined
63 | v = undefined
64 | i = 0
65 |
66 | while i < rows
67 | hex += "#{zero(b, offsetLength)}:"
68 | lastBytes = (if i is rows - 1 then last else bytesPerLine)
69 |
70 | j = 0
71 | while j < lastBytes
72 | hex += " #{zero(buffer[b], 2)}"
73 | b++
74 | j++
75 |
76 | b -= lastBytes
77 |
78 | j = 0
79 | while j < lastBytes
80 | v = buffer[b]
81 | if (v > 31 and v < 127) or v > 159
82 | ascii += entities.encodeHTML(String.fromCharCode(v))
83 | else
84 | ascii += "."
85 | b++
86 | j++
87 |
88 | hex += "
"
89 | ascii += "
"
90 | i++
91 |
92 | @hexDump.append "