Skip to content

Instantly share code, notes, and snippets.

@seungwon0
Created May 3, 2011 07:56
Show Gist options
  • Save seungwon0/952973 to your computer and use it in GitHub Desktop.
Save seungwon0/952973 to your computer and use it in GitHub Desktop.
Hexadecimal Converter Plug-in for Synapse
/*
* Copyright (C) 2011 Seungwon Jeong <seungwon0@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Authored by Seungwon Jeong <seungwon0@gmail.com>
*
*/
namespace Synapse
{
public class HexPlugin : Object, Activatable, ItemProvider
{
// a mandatory property
public bool enabled { get; set; default = true; }
public void activate ()
{
}
public void deactivate ()
{
}
static void register_plugin ()
{
DataSink.PluginRegistry.get_default ().register_plugin (
typeof (HexPlugin),
_ ("Hex"), // plugin title
_ ("Hexadecimal Converter"), // description
"accessories-calculator", // icon name
register_plugin // reference to this function
);
}
static construct
{
// register the plugin when the class is constructed
register_plugin ();
}
private Regex regex;
construct
{
try {
regex = new Regex
("^(\\d+|0[0-7]+|0[xX][0-9a-fA-F]+|0[bB][01]+)$",
RegexCompileFlags.OPTIMIZE);
} catch (Error e) {
Utils.Logger.error (this, "Error creating regexp.");
}
}
// an optional method to improve the speed of searches, if you
// return false here, the search method won't be called for
// this query
public bool handles_query (Query query)
{
// we will only search in the "Actions" category (that
// includes "All" as well)
return (QueryFlags.ACTIONS in query.query_type);
}
public async ResultSet? search (Query query) throws SearchError
{
string input = query.query_string;
if (regex.match (input)) {
uint num = 0;
if (input.has_prefix ("0b") || input.has_prefix ("0B"))
for (int i = 2; i < input.length; i++)
num += (input.get (i) - '0') << (input.length - 1 - i);
else if ( input.has_prefix ("0x")
|| input.has_prefix ("0X") )
input.scanf ("%x", out num);
else if (input.has_prefix ("0"))
input.scanf ("%o", out num);
else
input.scanf ("%u", out num);
var results = new ResultSet ();
results.add (new Result (input, num),
Match.Score.ABOVE_AVERAGE);
// make sure this method is called before returning
// any results
query.check_cancellable ();
return results;
}
// make sure this method is called before returning any
// results
query.check_cancellable ();
return null;
}
private class Result : Object, Match
{
// from Match interface
public string title { get; construct set; }
public string description { get; set; }
public string icon_name { get; construct set; }
public bool has_thumbnail { get; construct set; }
public string thumbnail_path { get; construct set; }
public MatchType match_type { get; construct set; }
public Result (string input, uint num)
{
string title;
title = "%u, 0%o, 0x%x, ".printf (num, num, num);
string bin = "";
do {
if (num % 2 == 0)
bin += "0";
else
bin += "1";
num >>= 1;
} while (num != 0);
bin = "0b" + bin.reverse();
title += bin;
Object (match_type: MatchType.TEXT,
title: title,
description: input,
has_thumbnail: false,
icon_name: "accessories-calculator");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment