Class: Greeenboii::GistManager

Inherits:
Object
  • Object
show all
Defined in:
lib/greeenboii.rb

Instance Method Summary collapse

Constructor Details

#initializeGistManager

Returns a new instance of GistManager.



424
425
426
427
# File 'lib/greeenboii.rb', line 424

def initialize
  Dotenv.load
  ensure_connection
end

Instance Method Details

#add_gistObject



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/greeenboii.rb', line 436

def add_gist
  unless ENV['TURSO_DATABASE_URL'] && ENV['TURSO_AUTH_TOKEN']
    CLI::UI.puts(CLI::UI.fmt("{{red:✗}} Cloud credentials required"))
    return
  end

  title = CLI::UI.ask("Enter a title for this gist:")
  return if title.empty?

  url = CLI::UI.ask("Enter GitHub Gist URL:")
  return if url.empty? || !url.match?(/https:\/\/gist\.github\.com\//)

  description = CLI::UI.ask("Enter a description (optional):", default: "")
  tags = CLI::UI.ask("Enter tags (comma separated):", default: "")

  # Extract gist ID from URL
  gist_id = url.split('/').last
  created_at = Time.now.strftime("%Y-%m-%d %H:%M:%S")

  CLI::UI::Spinner.spin("Saving gist...") do |spinner|
    begin
      # First create table if needed
      create_table_result = turso_execute(
        "CREATE TABLE IF NOT EXISTS gists (gist_id TEXT PRIMARY KEY, title TEXT NOT NULL, url TEXT NOT NULL, description TEXT, tags TEXT, created_at TEXT)"
      )

      # Then insert the data
      insert_result = turso_execute(
        "INSERT OR REPLACE INTO gists (gist_id, title, url, description, tags, created_at) VALUES (?, ?, ?, ?, ?, ?)",
        [gist_id, title, url, description, tags, created_at]
      )

      spinner.update_title("Gist saved successfully")
    rescue => e
      spinner.update_title("Error saving gist: #{e.message}")
    end
  end
end

#delete_gistObject



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'lib/greeenboii.rb', line 588

def delete_gist
  list_gists

  return if !@gists || @gists.empty?

  gist_id = CLI::UI.ask("Enter gist ID to delete:")
  return if gist_id.empty?

  CLI::UI::Spinner.spin("Deleting gist...") do |spinner|
    begin
      result = turso_execute("DELETE FROM gists WHERE gist_id = ?", [gist_id])

      # Try to determine if rows were affected
      affected_rows = get_affected_rows(result)

      if affected_rows > 0
        spinner.update_title("Gist deleted successfully")
      else
        spinner.update_title("Gist not found")
      end
    rescue => e
      spinner.update_title("Error deleting gist: #{e.message}")
    end
  end
end

#list_gistsObject



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/greeenboii.rb', line 475

def list_gists
  unless ENV['TURSO_DATABASE_URL'] && ENV['TURSO_AUTH_TOKEN']
    CLI::UI.puts(CLI::UI.fmt("{{red:✗}} Cloud credentials required"))
    return
  end

  CLI::UI::Spinner.spin("Fetching gists...") do |spinner|
    begin
      # Create table if needed
      create_table_result = turso_execute(
        "CREATE TABLE IF NOT EXISTS gists (gist_id TEXT PRIMARY KEY, title TEXT NOT NULL, url TEXT NOT NULL, description TEXT, tags TEXT, created_at TEXT)"
      )

      # Get all gists
      result = turso_execute(
        "SELECT gist_id, title, url, description, tags, created_at FROM gists ORDER BY created_at DESC"
      )

      @gists = extract_rows(result)

      if @gists.empty?
        spinner.update_title("No gists found")
      else
        spinner.update_title("Found #{@gists.length} gists")
      end
    rescue => e
      spinner.update_title("Error fetching gists: #{e.message}")
    end
  end

  return if !@gists || @gists.empty?

  CLI::UI.puts("\nYour Gists:")
  @gists.each do |gist|
    gist_id, title, url, description, tags, created_at = gist
    CLI::UI.puts(CLI::UI.fmt("{{cyan:#{gist_id}}}: {{bold:#{title}}} - #{url}"))
    CLI::UI.puts(CLI::UI.fmt("   Description: #{description}")) if description && !description.empty?
    CLI::UI.puts(CLI::UI.fmt("   Tags: #{tags}")) if tags && !tags.empty?
    CLI::UI.puts(CLI::UI.fmt("   Created: #{created_at}"))
    CLI::UI.puts("")
  end
end

#manage_credentialsObject



614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/greeenboii.rb', line 614

def manage_credentials
  CLI::UI::Frame.divider("{{v}} Cloud Credentials")

  current_url = ENV['TURSO_DATABASE_URL'] || "Not set"
  current_token = ENV['TURSO_AUTH_TOKEN'] ? "[Hidden]" : "Not set"

  CLI::UI.puts(CLI::UI.fmt("Current settings:"))
  CLI::UI.puts(CLI::UI.fmt("Database URL: {{cyan:#{current_url}}}"))
  CLI::UI.puts(CLI::UI.fmt("Auth Token: {{cyan:#{current_token}}}"))
  CLI::UI.puts("")

  CLI::UI::Prompt.ask("Credential Options:") do |handler|
    handler.option("Update Database URL") do
      url = CLI::UI.ask("Enter Turso Database URL:")
      update_env_file('TURSO_DATABASE_URL', url) unless url.empty?
    end

    handler.option("Update Auth Token") do
      token = CLI::UI.ask("Enter Turso Auth Token:")
      update_env_file('TURSO_AUTH_TOKEN', token) unless token.empty?
    end

    handler.option("Test Connection") do
      test_connection
    end

    handler.option("Back") { return }
  end
end

#open_gistObject



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/greeenboii.rb', line 559

def open_gist
  list_gists

  return if !@gists || @gists.empty?

  gist_id = CLI::UI.ask("Enter gist ID to open:")
  return if gist_id.empty?

  # Find the matching gist
  gist = @gists.find { |g| g[0] == gist_id }

  unless gist
    CLI::UI.puts(CLI::UI.fmt("{{red:✗}} Gist not found"))
    return
  end

  url = gist[2] # URL is at index 2

  if RUBY_PLATFORM.match?(/mswin|mingw|cygwin/)
    system("start #{url}")
  elsif RUBY_PLATFORM.match?(/darwin/)
    system("open #{url}")
  elsif RUBY_PLATFORM.match?(/linux/)
    system("xdg-open #{url}")
  else
    CLI::UI.puts(CLI::UI.fmt("{{yellow:⚠}} Couldn't determine how to open URL on your platform. URL: #{url}"))
  end
end

#search_gistsObject



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/greeenboii.rb', line 518

def search_gists
  unless ENV['TURSO_DATABASE_URL'] && ENV['TURSO_AUTH_TOKEN']
    CLI::UI.puts(CLI::UI.fmt("{{red:✗}} Cloud credentials required"))
    return
  end

  term = CLI::UI.ask("Enter search term:")
  return if term.empty?

  CLI::UI::Spinner.spin("Searching gists...") do |spinner|
    begin
      result = turso_execute(
        "SELECT gist_id, title, url, description, tags, created_at FROM gists WHERE title LIKE ? OR description LIKE ? OR tags LIKE ? ORDER BY created_at DESC",
        ["%#{term}%", "%#{term}%", "%#{term}%"]
      )

      @search_results = extract_rows(result)

      if @search_results.empty?
        spinner.update_title("No matching gists found")
      else
        spinner.update_title("Found #{@search_results.length} matching gists")
      end
    rescue => e
      spinner.update_title("Search error: #{e.message}")
    end
  end

  return if !@search_results || @search_results.empty?

  CLI::UI.puts(CLI::UI.fmt("\n{{bold:Search Results:}}"))
  @search_results.each do |gist|
    gist_id, title, url, description, tags, created_at = gist
    CLI::UI.puts(CLI::UI.fmt("{{cyan:#{gist_id}}}: {{bold:#{title}}} - #{url}"))
    CLI::UI.puts(CLI::UI.fmt("   Description: #{description}")) if description && !description.empty?
    CLI::UI.puts(CLI::UI.fmt("   Tags: #{tags}")) if tags && !tags.empty?
    CLI::UI.puts(CLI::UI.fmt("   Created: #{created_at}"))
    CLI::UI.puts("")
  end
end

#show_menuObject



689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'lib/greeenboii.rb', line 689

def show_menu
  CLI::UI::Frame.divider("{{v}} Gist Manager")
  loop do
    CLI::UI::Prompt.ask("Gist Manager Options:") do |handler|
      handler.option("Add Gist")          { add_gist }
      handler.option("List Gists")        { list_gists }
      handler.option("Search Gists")      { search_gists }
      handler.option("Open Gist")         { open_gist }
      handler.option("Delete Gist")       { delete_gist }
      handler.option("Cloud Settings")    { manage_credentials }
      handler.option("Exit")              { return }
    end
  end
end