forked from cjoh/status-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus-stats-view.coffee
44 lines (36 loc) · 1.35 KB
/
status-stats-view.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{View} = require 'atom-space-pen-views'
{CompositeDisposable} = require 'atom'
DocumentStatsModel = require("./status-stats-model")
module.exports =
class DocumentStatsView extends View
@content: ->
@span class: "status-stats inline-block"
initialize: (_) ->
# SMELL Programming by coincidence.
# We must initialise the model before showing stats
# with @toggle(true)
@model = new DocumentStatsModel()
# When switching panes, updateStats
atom.workspace.onDidChangeActivePaneItem((activePaneItem) =>
@updateStats())
# When the active buffer changes, updateStats
atom.workspace.observeTextEditors((editor) =>
editor.onDidChange(() =>
@updateStats()))
@ensureFirstToggleAfterActivationShowsStats()
ensureFirstToggleAfterActivationShowsStats: ->
# force hiding so that toggle() will show
@hide()
# We need `args...` because we're overriding a jQuery function
toggle: (args...) ->
super(args)
@updateStats()
updateStats: ->
# The active pane might not be an editor.
editor = atom.workspace.getActiveTextEditor()
if editor?
activeDocumentText = editor.getText()
analysis = @model.analyseText(activeDocumentText)
@text("#{analysis.wordCount} words; grade level #{analysis.gradeLevel}; reading ease #{analysis.readingEase}")
else
@text("No text to analyse.")