grammar Formatting

rule format_string
  (token / literal)+
end

rule literal
  . {
    def value(log_line, color)
      # context-independent...
      self.text_value
    end
  }
end

rule token
  remote_addr / remote_user / time_local / request / status / body_bytes_sent / upstream_response_time / request_time / http_referer / http_user_agent / proxy_addresses
end

rule remote_addr
  '%a' <Node> {
    def value(log_line, color)
      if color && Sickill::Rainbow.enabled
        # 24 = 15 + 9, the extra amount of bytes required for the ANSI escape codes...
        "%24s" % foreground(log_line.remote_address, color)
      else
        "%15s" % log_line.remote_address
      end
    end
  }
end

rule remote_user
  '%u' <Node> {
    def value(log_line, color)
      foreground(log_line.remote_user, color)
    end
  }
end

rule time_local
  '%t' <Node> {
    def value(log_line, color)
      foreground(log_line.to_date_s, color)
    end
  }
end

rule request
  '%r' <Node> {
    def value(log_line, color)
      foreground(log_line.to_request_s, color)
    end
  }
end

rule status
  '%s' <Node> {
    def value(log_line, color)
      foreground(log_line.status, color)
    end
  }
end

rule body_bytes_sent
  '%b' <Node> {
    def value(log_line, color)
      foreground(log_line.body_bytes_sent, color)
    end
  }
end

rule upstream_response_time
  '%<T' <Node> {
    def value(log_line, color)
      if color && Sickill::Rainbow.enabled
        # 15 = 6 + 9, the extra amount of bytes required for the ANSI escape codes...
        "%15s" % foreground(log_line.upstream_response_time, color)
      else
        "%6s" % log_line.upstream_response_time
      end
    end
  }
end

rule request_time
  '%T' <Node> {
    def value(log_line, color)
      if color && Sickill::Rainbow.enabled
        # 15 = 6 + 9, the extra amount of bytes required for the ANSI escape codes...
        "%15s" % foreground(log_line.request_time, color)
      else
        "%6s" % log_line.request_time
      end
    end
  }
end

rule http_referer
  '%R' <Node> {
    def value(log_line, color)
      foreground(log_line.to_referer_s, color)
    end
  }
end

rule http_user_agent
  '%U' <Node> {
    def value(log_line, color)
      foreground(log_line.to_agent_s, color)
    end
  }
end

rule proxy_addresses
  '%p' <Node> {
    def value(log_line, color)
      (log_line.proxy_addresses || []).join(", ").foreground(color)
    end
  }
end

end

# vim:syntax=ruby