<commit_before>SelectListView = require('atom').SelectListView

$ = null
$$ = null
Subscriber = null
_s = null

module.exports =
class SelectWifiView extends SelectListView
  initialize: ->
    super

    {$, $$} = require 'atom'
    {Subscriber} = require 'emissary'

    @cp = require 'child_process'
    _s ?= require 'underscore.string'

    @subscriber = new Subscriber()
    @subscriber.subscribeToCommand atom.workspaceView, 'core:cancel core:close', => @hide()

    @addClass 'overlay from-top'
    @prop 'id', 'spark-ide-select-wifi-view'

    @port = null

  destroy: ->
    @remove()

  show: =>
    if !@hasParent()
      atom.workspaceView.append(this)

      @setItems []
      @setLoading 'Scaning for networks...'
      @listNetworks()
      @focusFilterEditor()

  hide: ->
    if @hasParent()
      @detach()

  viewForItem: (item) ->
    security = null

    switch item.security
      when 0
        security = 'Unsecured'
      when 1
        security = 'WEP'
      when 2
        security = 'WPA'
      when 3
        security = 'WPA2'

    $$ ->
      @li class: 'two-lines', =>
        if !!security
          @div class: 'pull-right', =>
            @kbd class: 'key-binding pull-right', security
        @div item.ssid

  confirmed: (item) ->
    atom.workspaceView.trigger 'spark-ide:enter-wifi-credentials', [@port, item.ssid, item.security]
    @cancel()

  getPlatform: ->
    process.platform

  listNetworks: ->

    @items = [{
      ssid: 'Enter SSID manually',
      security: null,
    }]

    @setItems @items

    switch @getPlatform()
      when 'darwin'
        @listNetworksDarwin()
      when 'win32'
        @listNetworksWindows()
      else
        console.error 'Current platform not supported'

  listNetworksDarwin: ->
    @cp.exec '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I', (error, stdout, stderr) =>
      currentSsid = null
      if stdout != ''
        currentSsid = stdout.match /\sSSID\:\s(.*)/
        if !!currentSsid
          currentSsid = currentSsid[1]

      @cp.exec '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s', (error, stdout, stderr) =>
        regex = /(.*)\s+([0-9a-f\:]{17})\s+([0-9\-]+)\s+([0-9\,\-\+]+)\s+([YN]+)\s+([A-Z\-]+)\s+(.*)/

        networks = []
        for line in stdout.split "\n"
          network = regex.exec line

          if !!network
            notAdded = (networks.length == 0) || networks.reduce (prev, current) ->
              prev && (current.ssid != network[1])

            if notAdded
              if network[7].indexOf('WPA2') > -1
                security = 3
              else if network[7].indexOf('WPA(') > -1
                security = 2
              else if network[7].indexOf('WEP') > -1
                security = 1
              else
                security = 0

              networks.push {
                ssid: network[1],
                bssid: network[2],
                rssi: network[3],
                channel: network[4],
                ht: network[5],
                cc: network[6],
                security_string: network[7],
                security: security
              }

        networks.sort (a, b) ->
          if a.ssid == currentSsid
            return -1000
          if b.ssid == currentSsid
            return 1000

          parseInt(b.rssi) - parseInt(a.rssi)

        @setItems(networks.concat @items)

  listNetworksWindows: ->
    fs = require 'fs-plus'
    @cp.exec 'netsh wlan show interfaces', (error, stdout, stderr) =>
      currentSsid = null
      if stdout != ''
        currentSsid = stdout.match /\sSSID\s+\:\s(.*)/
        if !!currentSsid<commit_msg>Trimming SSID<commit_after>SelectListView = require('atom').SelectListView

$ = null
$$ = null
Subscriber = null
_s = null

module.exports =
class SelectWifiView extends SelectListView
  initialize: ->
    super

    {$, $$} = require 'atom'
    {Subscriber} = require 'emissary'

    @cp = require 'child_process'
    _s ?= require 'underscore.string'

    @subscriber = new Subscriber()
    @subscriber.subscribeToCommand atom.workspaceView, 'core:cancel core:close', => @hide()

    @addClass 'overlay from-top'
    @prop 'id', 'spark-ide-select-wifi-view'

    @port = null

  destroy: ->
    @remove()

  show: =>
    if !@hasParent()
      atom.workspaceView.append(this)

      @setItems []
      @setLoading 'Scaning for networks...'
      @listNetworks()
      @focusFilterEditor()

  hide: ->
    if @hasParent()
      @detach()

  viewForItem: (item) ->
    security = null

    switch item.security
      when 0
        security = 'Unsecured'
      when 1
        security = 'WEP'
      when 2
        security = 'WPA'
      when 3
        security = 'WPA2'

    $$ ->
      @li class: 'two-lines', =>
        if !!security
          @div class: 'pull-right', =>
            @kbd class: 'key-binding pull-right', security
        @div item.ssid

  confirmed: (item) ->
    atom.workspaceView.trigger 'spark-ide:enter-wifi-credentials', [@port, item.ssid, item.security]
    @cancel()

  getPlatform: ->
    process.platform

  listNetworks: ->

    @items = [{
      ssid: 'Enter SSID manually',
      security: null,
    }]

    @setItems @items

    switch @getPlatform()
      when 'darwin'
        @listNetworksDarwin()
      when 'win32'
        @listNetworksWindows()
      else
        console.error 'Current platform not supported'

  listNetworksDarwin: ->
    @cp.exec '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I', (error, stdout, stderr) =>
      currentSsid = null
      if stdout != ''
        currentSsid = stdout.match /\sSSID\:\s(.*)/
        if !!currentSsid
          currentSsid = currentSsid[1]

      @cp.exec '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s', (error, stdout, stderr) =>
        regex = /(.*)\s+([0-9a-f\:]{17})\s+([0-9\-]+)\s+([0-9\,\-\+]+)\s+([YN]+)\s+([A-Z\-]+)\s+(.*)/

        networks = []
        for line in stdout.split "\n"
          network = regex.exec line

          if !!network
            notAdded = (networks.length == 0) || networks.reduce (prev, current) ->
              prev && (current.ssid != network[1])

            if notAdded
              if network[7].indexOf('WPA2') > -1
                security = 3
              else if network[7].indexOf('WPA(') > -1
                security = 2
              else if network[7].indexOf('WEP') > -1
                security = 1
              else
                security = 0

              networks.push {
                ssid: _s.trim(network[1]),
                bssid: network[2],
                rssi: network[3],
                channel: network[4],
                ht: network[5],
                cc: network[6],
                security_string: network[7],
                security: security
              }

        networks.sort (a, b) ->
          if a.ssid == currentSsid
            return -1000
          if b.ssid == currentSsid
            return 1000

          parseInt(b.rssi) - parseInt(a.rssi)

        @setItems(networks.concat @items)

  listNetworksWindows: ->
    fs = require 'fs-plus'
    @cp.exec 'netsh wlan show interfaces', (error, stdout, stderr) =>
      currentSsid = null
      if stdout != ''
        currentSsid = stdout.match /\sSSID\s+\:\s(.*)/
        if !!currentSsid
{'fraction_non_alphanumeric': 0.14162743779421655, 'fraction_numerical': 0.014525891055817081, 'mean_word_length': 1.8611004232397075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5810816', 'n_tokens_mistral': 2434, 'n_tokens_neox': 2266, 'n_words': 506}