1- HeaderView = require ' ./header-view'
2- ScriptOptionsView = require ' ./script-options-view'
3-
41{View , $$ } = require ' atom-space-pen-views'
52
3+ HeaderView = require ' ./header-view'
4+ {MessagePanelView } = require ' atom-message-panel'
65AnsiFilter = require ' ansi-to-html'
76stripAnsi = require ' strip-ansi'
87linkPaths = require ' ./link-paths'
98_ = require ' underscore'
109
1110# Runs a portion of a script through an interpreter and displays it line by line
1211module .exports =
13- class ScriptView extends View
14- @results : " "
15-
16- @ content: ->
17- @ div =>
18- @ subview ' headerView' , new HeaderView ()
12+ class ScriptView extends MessagePanelView
13+ constructor : ->
14+ @ansiFilter = new AnsiFilter
1915
20- # Display layout and outlets
21- css = ' tool-panel panel panel-bottom padding script-view
22- native-key-bindings'
23- @ div class : css, outlet : ' script' , tabindex : - 1 , =>
24- @ div class : ' panel-body padded output' , outlet : ' output'
16+ @headerView = new HeaderView
2517
26- initialize : (serializeState ) ->
27- @ansiFilter = new AnsiFilter
18+ super (title : @headerView , rawTitle : true , closeMethod : ' destroy' )
2819
29- linkPaths . listen @
20+ @ addClass ( ' script-view ' )
3021
31- serialize : ->
22+ linkPaths . listen @body
3223
3324 setHeaderAndShowExecutionTime : (returnCode , executionTime ) =>
3425 if (executionTime? )
@@ -45,30 +36,33 @@ class ScriptView extends View
4536 # Display window and load message
4637
4738 # First run, create view
48- atom . workspace . addBottomPanel ( item : this ) unless @ hasParent ()
39+ @ attach ( ) unless @ hasParent ()
4940
5041 # Close any existing process and start a new one
5142 @ stop ()
5243
53- @headerView . title . text title
54- @headerView . setStatus ' start'
44+ @ setHeaderTitle title
45+ @ setHeaderStatus ' start'
5546
5647 # Get script view ready
57- @output . empty ()
48+ @ clear ()
5849
59- # Remove the old script results
60- @results = " "
50+ removePanel : ->
51+ @ stop ()
52+ @ detach ()
53+ # the 'close' method from MessagePanelView actually destroys the panel
54+ ScriptView .__super__ .close .apply (this )
6155
56+ # This is triggered when hitting the 'close' button on the panel
57+ # We are not actually closing the panel here since we want to trigger
58+ # 'script:close-view' which will eventually remove the panel via 'removePanel'
6259 close : ->
63- @ stop ()
64- if @ hasParent ()
65- grandParent = @script .parent ().parent ()
66- @ detach ()
67- grandParent .remove ()
60+ workspaceView = atom .views .getView (atom .workspace )
61+ atom .commands .dispatch workspaceView, ' script:close-view'
6862
6963 stop : ->
7064 @ display ' stdout' , ' ^C'
71- @headerView . setStatus ' kill'
65+ @ setHeaderStatus ' kill'
7266
7367 createGitHubIssueLink : (argType , lang ) ->
7468 title = " Add #{ argType} support for #{ lang} "
@@ -89,7 +83,7 @@ class ScriptView extends View
8983 @ handleError (err)
9084
9185 showUnableToRunError : (command ) ->
92- @output . append $$ ->
86+ @ add $$ ->
9387 @ h1 ' Unable to run'
9488 @ pre _ .escape command
9589 @ h2 ' Did you start Atom from the command line?'
@@ -115,9 +109,9 @@ class ScriptView extends View
115109
116110 handleError : (err ) ->
117111 # Display error and kill process
118- @headerView . title . text ' Error'
119- @headerView . setStatus ' err'
120- @output . append err
112+ @ setHeaderTitle ' Error'
113+ @ setHeaderStatus ' err'
114+ @ add ( err)
121115 @ stop ()
122116
123117 setHeaderStatus : (status ) ->
@@ -127,35 +121,31 @@ class ScriptView extends View
127121 @headerView .title .text title
128122
129123 display : (css , line ) ->
130- @results += line
131-
132124 if atom .config .get (' script.escapeConsoleOutput' )
133125 line = _ .escape (line)
134126
135127 line = @ansiFilter .toHtml (line)
136128 line = linkPaths (line)
137129
138- padding = parseInt (@output .css (' padding-bottom' ))
139- scrolledToEnd =
140- @script .scrollBottom () == (padding + @output .trueHeight ())
141-
142- lessThanFull = @output .trueHeight () <= @script .trueHeight ()
130+ {clientHeight , scrollTop , scrollHeight } = @body [0 ]
131+ # indicates that the panel is scrolled to the bottom, thus we know that
132+ # we are not interfering with the user's manual scrolling
133+ atEnd = scrollTop >= (scrollHeight - clientHeight)
143134
144- @output . append $$ ->
135+ @ add $$ ->
145136 @ pre class : " line #{ css} " , =>
146137 @ raw line
147138
148- if atom .config .get (' script.scrollWithOutput' )
149- if lessThanFull or scrolledToEnd
150- # Scroll down in a polling loop 'cause
151- # we don't know when the reflow will finish.
152- # See: http://stackoverflow.com/q/5017923/407845
153- do @ checkScrollAgain 5
139+ if atom .config .get (' script.scrollWithOutput' ) and atEnd
140+ # Scroll down in a polling loop 'cause
141+ # we don't know when the reflow will finish.
142+ # See: http://stackoverflow.com/q/5017923/407845
143+ do @ checkScrollAgain 5
154144
155145 scrollTimeout : null
156146 checkScrollAgain : (times ) ->
157147 =>
158- @script .scrollToBottom ()
148+ @body .scrollToBottom ()
159149
160150 clearTimeout @scrollTimeout
161151 if times > 1
0 commit comments